d2f388
From 33064cd077cf6fa386f0a5a840c2161868da7b3a Mon Sep 17 00:00:00 2001
d2f388
From: =?UTF-8?q?Ond=C5=99ej=20Sur=C3=BD?= <ondrej@isc.org>
d2f388
Date: Tue, 8 Feb 2022 12:42:34 +0100
d2f388
Subject: [PATCH] Run .closehandle_cb asynchrounosly in nmhandle_detach_cb()
d2f388
d2f388
When sock->closehandle_cb is set, we need to run nmhandle_detach_cb()
d2f388
asynchronously to ensure correct order of multiple packets processing in
d2f388
the isc__nm_process_sock_buffer().  When not run asynchronously, it
d2f388
would cause:
d2f388
d2f388
  a) out-of-order processing of the return codes from processbuffer();
d2f388
d2f388
  b) stack growth because the next TCP DNS message read callback will
d2f388
     be called from within the current TCP DNS message read callback.
d2f388
d2f388
The sock->closehandle_cb is set to isc__nm_resume_processing() for TCP
d2f388
sockets which calls isc__nm_process_sock_buffer().  If the read callback
d2f388
(called from isc__nm_process_sock_buffer()->processbuffer()) doesn't
d2f388
attach to the nmhandle (f.e. because it wants to drop the processing or
d2f388
we send the response directly via uv_try_write()), the
d2f388
isc__nm_resume_processing() (via .closehandle_cb) would call
d2f388
isc__nm_process_sock_buffer() recursively.
d2f388
d2f388
The below shortened code path shows how the stack can grow:
d2f388
d2f388
 1: ns__client_request(handle, ...);
d2f388
 2: isc_nm_tcpdns_sequential(handle);
d2f388
 3: ns_query_start(client, handle);
d2f388
 4:   query_lookup(qctx);
d2f388
 5:     query_send(qctcx->client);
d2f388
 6:       isc__nmhandle_detach(&client->reqhandle);
d2f388
 7:         nmhandle_detach_cb(&handle);
d2f388
 8:           sock->closehandle_cb(sock); // isc__nm_resume_processing
d2f388
 9:             isc__nm_process_sock_buffer(sock);
d2f388
10:               processbuffer(sock); // isc__nm_tcpdns_processbuffer
d2f388
11:                 isc_nmhandle_attach(req->handle, &handle);
d2f388
12:                 isc__nm_readcb(sock, req, ISC_R_SUCCESS);
d2f388
13:                   isc__nm_async_readcb(NULL, ...);
d2f388
14:                     uvreq->cb.recv(...); // ns__client_request
d2f388
d2f388
Instead, if 'sock->closehandle_cb' is set, we need to run detach the
d2f388
handle asynchroniously in 'isc__nmhandle_detach', so that on line 8 in
d2f388
the code flow above does not start this recursion. This ensures the
d2f388
correct order when processing multiple packets in the function
d2f388
'isc__nm_process_sock_buffer()' and prevents the stack growth.
d2f388
d2f388
When not run asynchronously, the out-of-order processing leaves the
d2f388
first TCP socket open until all requests on the stream have been
d2f388
processed.
d2f388
d2f388
If the pipelining is disabled on the TCP via `keep-response-order`
d2f388
configuration option, named would keep the first socket in lingering
d2f388
CLOSE_WAIT state when the client sends an incomplete packet and then
d2f388
closes the connection from the client side.
d2f388
d2f388
(cherry picked from commit afee2b5a7bc933a2d987907fc327a9f118fdbd17)
d2f388
---
d2f388
 lib/isc/netmgr/netmgr.c | 6 +++++-
d2f388
 1 file changed, 5 insertions(+), 1 deletion(-)
d2f388
d2f388
diff --git a/lib/isc/netmgr/netmgr.c b/lib/isc/netmgr/netmgr.c
d2f388
index 3283eb6e4f..0ed3182fb6 100644
d2f388
--- a/lib/isc/netmgr/netmgr.c
d2f388
+++ b/lib/isc/netmgr/netmgr.c
d2f388
@@ -1746,8 +1746,12 @@ isc__nmhandle_detach(isc_nmhandle_t **handlep FLARG) {
d2f388
 	handle = *handlep;
d2f388
 	*handlep = NULL;
d2f388
 
d2f388
+	/*
d2f388
+	 * If the closehandle_cb is set, it needs to run asynchronously to
d2f388
+	 * ensure correct ordering of the isc__nm_process_sock_buffer().
d2f388
+	 */
d2f388
 	sock = handle->sock;
d2f388
-	if (sock->tid == isc_nm_tid()) {
d2f388
+	if (sock->tid == isc_nm_tid() && sock->closehandle_cb == NULL) {
d2f388
 		nmhandle_detach_cb(&handle FLARG_PASS);
d2f388
 	} else {
d2f388
 		isc__netievent_detach_t *event =
d2f388
-- 
d2f388
2.34.1
d2f388