Skip to content

Commit

Permalink
fix(libsinsp): Don't loop forever on container api
Browse files Browse the repository at this point in the history
Add an escape clause on the container lookup path to make sure we're not just constantly getting timeouts.

/kind bug
/area libsinsp

```release-note
NONE
```

Signed-off-by: Nathan Baker <[email protected]>
  • Loading branch information
nathan-b authored and poiana committed Sep 15, 2023
1 parent 7c0b0b2 commit e2cdf1e
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -672,6 +672,7 @@ bool docker_async_source::parse(const docker_lookup_request& request, sinsp_cont
}
/* FALLTHRU */
case docker_connection::docker_response::RESP_ERROR:
case docker_connection::docker_response::RESP_TIMEOUT:
g_logger.format(sinsp_logger::SEV_DEBUG,
"docker_async (%s): Url fetch failed, returning false",
request.container_id.c_str());
Expand Down
3 changes: 2 additions & 1 deletion userspace/libsinsp/container_engine/docker/connection.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@ class docker_connection {
enum docker_response {
RESP_OK = 0,
RESP_BAD_REQUEST = 1,
RESP_ERROR = 2
RESP_ERROR = 2,
RESP_TIMEOUT = 3
};

docker_connection();
Expand Down
17 changes: 17 additions & 0 deletions userspace/libsinsp/container_engine/docker/connection_linux.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ limitations under the License.
#include "sinsp_int.h"

namespace {
const uint32_t max_allowed_timeouts = 5;

size_t docker_curl_write_callback(const char *ptr, size_t size, size_t nmemb, std::string *json)
{
Expand Down Expand Up @@ -107,6 +108,7 @@ docker_connection::docker_response docker_connection::get_docker(const docker_lo
return docker_response::RESP_ERROR;
}

uint32_t num_timeouts = 0;
while(true)
{
int still_running;
Expand Down Expand Up @@ -141,6 +143,21 @@ docker_connection::docker_response docker_connection::get_docker(const docker_lo
ASSERT(false);
return docker_response::RESP_ERROR;
}
if(numfds == 0)
{
// Operation timed out
if(++num_timeouts >= max_allowed_timeouts)
{
g_logger.format(sinsp_logger::SEV_WARNING,
"docker_async (%s): Max timeouts exceeded",
url.c_str());
return docker_response::RESP_TIMEOUT;
}
g_logger.format(sinsp_logger::SEV_DEBUG,
"docker_async (%s): Operation timed out %d times",
url.c_str(),
num_timeouts);
}
}

if(curl_multi_remove_handle(m_curlm, curl) != CURLM_OK)
Expand Down

0 comments on commit e2cdf1e

Please sign in to comment.