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