From 97f0b868574cde72246c0da03db14b9d0ffa591e Mon Sep 17 00:00:00 2001 From: Leonardo Alminana Date: Fri, 24 Sep 2021 17:46:34 -0300 Subject: [PATCH] upstream: added a flag to delay connection disposal Signed-off-by: Leonardo Alminana --- include/fluent-bit/flb_upstream_conn.h | 9 +++++++++ src/flb_upstream.c | 8 ++++++++ 2 files changed, 17 insertions(+) diff --git a/include/fluent-bit/flb_upstream_conn.h b/include/fluent-bit/flb_upstream_conn.h index 0a9b6220788..138202a438d 100644 --- a/include/fluent-bit/flb_upstream_conn.h +++ b/include/fluent-bit/flb_upstream_conn.h @@ -55,6 +55,15 @@ struct flb_upstream_conn { */ int net_error; + /* If this flag is set, then destroy_conn will ignore this connection, this + * helps mitigate issues caused by flb_upstream_conn_timeouts marking a connection + * to be dropped and the event loop manager function destroying that connection + * at the end of the cycle while the connection coroutine is still suspended which + * causes the outer functions to access invalid memory when handling the error amongst + * other things. + */ + int busy_flag; + /* Timestamps */ time_t ts_assigned; time_t ts_created; diff --git a/src/flb_upstream.c b/src/flb_upstream.c index d2bd91072dd..3c6a2384ad3 100644 --- a/src/flb_upstream.c +++ b/src/flb_upstream.c @@ -446,6 +446,11 @@ static inline int prepare_destroy_conn_safe(struct flb_upstream_conn *u_conn) static int destroy_conn(struct flb_upstream_conn *u_conn) { + /* Delay the destruction of busy connections */ + if (u_conn->busy_flag) { + return 0; + } + #ifdef FLB_HAVE_TLS if (u_conn->tls_session) { flb_tls_session_destroy(u_conn->tls, u_conn); @@ -476,6 +481,7 @@ static struct flb_upstream_conn *create_conn(struct flb_upstream *u) conn->u = u; conn->fd = -1; conn->net_error = -1; + conn->busy_flag = FLB_TRUE; /* retrieve the event loop */ evl = flb_engine_evl_get(); @@ -525,6 +531,7 @@ static struct flb_upstream_conn *create_conn(struct flb_upstream *u) flb_debug("[upstream] connection #%i failed to %s:%i", conn->fd, u->tcp_host, u->tcp_port); prepare_destroy_conn_safe(conn); + conn->busy_flag = FLB_FALSE; return NULL; } @@ -535,6 +542,7 @@ static struct flb_upstream_conn *create_conn(struct flb_upstream *u) /* Invalidate timeout for connection */ conn->ts_connect_timeout = -1; + conn->busy_flag = FLB_FALSE; return conn; }