Skip to content

Commit 4ea2324

Browse files
authored
Merge pull request #2860 from DataDog/glopes/conn-refused
appsec ext: retry on connection refused
2 parents d1de56e + 421c75f commit 4ea2324

1 file changed

Lines changed: 9 additions & 5 deletions

File tree

appsec/src/extension/network.c

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,8 @@ int dd_conn_init( // NOLINT(readability-function-cognitive-complexity)
8585
res = connect(
8686
conn->socket, (struct sockaddr *)&conn->addr, sizeof(conn->addr));
8787
if (res == -1) {
88-
if (errno == EINPROGRESS) {
88+
int errno_copy = errno;
89+
if (errno_copy == EINPROGRESS) {
8990
struct pollfd pfds[] = {
9091
{.fd = conn->socket, .events = POLLIN | POLLOUT}};
9192
struct timespec now;
@@ -123,8 +124,8 @@ int dd_conn_init( // NOLINT(readability-function-cognitive-complexity)
123124
}
124125
// else good
125126
} else {
126-
if (errno == ENOENT) {
127-
// the socket does not exist. Retry
127+
if (errno_copy == ENOENT || errno_copy == ECONNREFUSED) {
128+
// the socket does not exist or is not being listened on. Retry
128129
struct timespec now;
129130
clock_gettime(CLOCK_MONOTONIC, &now);
130131
long time_left = _timespec_delta_ms(&deadline, &now);
@@ -134,8 +135,10 @@ int dd_conn_init( // NOLINT(readability-function-cognitive-complexity)
134135
return dd_error;
135136
}
136137

137-
mlog(dd_log_debug,
138-
"Socket does not exist. Waiting 100 ms for next retry");
138+
mlog(dd_log_debug, "Socket %s. Waiting %d ms for next retry",
139+
errno_copy == ENOENT ? "does not exist"
140+
: "is not being listened on",
141+
CONNECT_RETRY_PAUSE);
139142
int ret = usleep(CONNECT_RETRY_PAUSE * 1000); // NOLINT
140143
if (ret == 0) {
141144
goto try_again;
@@ -146,6 +149,7 @@ int dd_conn_init( // NOLINT(readability-function-cognitive-complexity)
146149
}
147150

148151
dd_conn_destroy(conn);
152+
errno = errno_copy; // restore for mlog_err
149153
mlog_err(
150154
dd_log_info, "Failed connecting to helper (connect() call)");
151155
return dd_error;

0 commit comments

Comments
 (0)