Skip to content

Commit 577a528

Browse files
Leonardo Alminanaedsiper
authored andcommitted
network: fixed corner case in async dns lookup where c-ares would resolve the query locally and return without creating a socket
There was a corner case where c-ares would resolve a name locally (in the reported case it was a ipv4 address) and dispatch the result callback immediately without creating a socket or sending a query (obvious). In that case, the lookup routine would hang after yielding waiting for the event loop handler to resume it after receiving the result. This patch selectively calls yield only when a socket was created after the c-ares api call is issued. This fix could fail if we tried to force c-ares to use TCP sockets and keep them alive and reuse them but in that case the whole thing fails so I don't think that is something to worry about (well, don't do that of course). Signed-off-by: Leonardo Alminana <[email protected]>
1 parent bca22bd commit 577a528

File tree

2 files changed

+8
-3
lines changed

2 files changed

+8
-3
lines changed

include/fluent-bit/flb_network.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ struct flb_dns_lookup_result_event {
6666

6767
struct flb_dns_lookup_context {
6868
struct mk_event response_event; /* c-ares socket event */
69-
// struct flb_dns_lookup_result_event result_event; /* result signaling event */
69+
int ares_socket_created;
7070
void *ares_channel;
7171
int result_code;
7272
int finished;

src/flb_network.c

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -566,6 +566,8 @@ static int flb_net_ares_sock_create_callback(ares_socket_t socket_fd,
566566

567567
context = (struct flb_dns_lookup_context *) userdata;
568568

569+
context->ares_socket_created = 1;
570+
569571
context->response_event.mask = MK_EVENT_EMPTY;
570572
context->response_event.status = MK_EVENT_NONE;
571573
context->response_event.data = &context->response_event;
@@ -613,6 +615,7 @@ struct flb_dns_lookup_context *flb_net_dns_lookup_context_create(struct mk_event
613615
return NULL;
614616
}
615617

618+
context->ares_socket_created = 0;
616619
context->event_loop = event_loop;
617620
context->coroutine = coroutine;
618621
context->finished = 0;
@@ -660,9 +663,11 @@ int flb_net_getaddrinfo(const char *node, const char *service, struct addrinfo *
660663
ares_getaddrinfo(lookup_context->ares_channel, node, service, &ares_hints,
661664
flb_net_getaddrinfo_callback, lookup_context);
662665

663-
flb_coro_yield(coroutine, FLB_FALSE);
666+
if (1 == lookup_context->ares_socket_created) {
667+
flb_coro_yield(coroutine, FLB_FALSE);
668+
}
664669

665-
if(0 == lookup_context->result_code) {
670+
if (0 == lookup_context->result_code) {
666671
*res = lookup_context->result;
667672
}
668673

0 commit comments

Comments
 (0)