[PATCH] Resolver: make TCP write timer event cancelable.

Aleksei Bavshin A.Bavshin at F5.com
Thu Jun 2 03:49:50 UTC 2022

Updated patch, with maybe too verbose description.
For the record, I did check a few similar modules I'm aware of (nginx-upstream-dynamic-servers, ngx_upstream_jdomain) and haven't found any affected code. So, the effect on third-party modules is hypothetical.

# HG changeset patch
# User Aleksei Bavshin <a.bavshin at f5.com>
# Date 1654139843 25200
#      Wed Jun 01 20:17:23 2022 -0700
# Node ID 488f7a926ba54303c930b0b1e19c2b27e0af5079
# Parent  40926829be83986a3a5a5f941d2014000a0acd2e
Resolver: make TCP write timer event cancelable.

Similar to 70e65bf8dfd7, the change is made to ensure that the ability to
cancel resolver tasks is fully controlled by the caller. As mentioned in the
referenced commit, it is safe to make this timer cancelable because resolve
tasks can have their own timeouts that are not cancelable.

The scenario where this may become a problem is a periodic background resolve
task (not tied to a specific request or a client connection), which receives a
response with short TTL, large enough to warrant fallback to a TCP query.
With each event loop wakeup, we either have a previously set write timer
instance or schedule a new one. The non-cancelable write timer can delay or
block graceful shutdown of a worker even if the ngx_resolver_ctx_t->cancelable
flag is set by the API user, and there are no other tasks or connections.

We use the resolver API in this way to maintain the list of upstream server
addresses specified with the 'resolve' parameter, and there could be third-party
modules implementing similar logic.

diff --git a/src/core/ngx_resolver.c b/src/core/ngx_resolver.c
--- a/src/core/ngx_resolver.c
+++ b/src/core/ngx_resolver.c
@@ -1389,6 +1389,7 @@ ngx_resolver_send_tcp_query(ngx_resolver
         rec->tcp->data = rec;
         rec->tcp->write->handler = ngx_resolver_tcp_write;
+        rec->tcp->write->cancelable = 1;
         rec->tcp->read->handler = ngx_resolver_tcp_read;
         rec->tcp->read->resolver = 1;

More information about the nginx-devel mailing list