From ae9a6befa802a10fb08fbe100ee96dd03ecb9787 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Witold=20Kr=C4=99cicki?= Date: Tue, 20 Oct 2020 12:55:58 +0200 Subject: [PATCH] Don't crash if isc_uv_export returns an error in accept_connection. isc_uv_export can return an error - e.g. EMFILE (from dup), handle this nicely. --- lib/isc/netmgr/tcp.c | 19 +++++++++++++++---- 1 file changed, 15 insertions(+), 4 deletions(-) diff --git a/lib/isc/netmgr/tcp.c b/lib/isc/netmgr/tcp.c index 483bd6d12f..7aef685058 100644 --- a/lib/isc/netmgr/tcp.c +++ b/lib/isc/netmgr/tcp.c @@ -16,6 +16,7 @@ #include #include #include +#include #include #include #include @@ -933,7 +934,9 @@ accept_connection(isc_nmsocket_t *ssock, isc_quota_t *quota) { if (r != 0) { result = isc__nm_uverr2result(r); uv_close((uv_handle_t *)uvstream, free_uvtcpt); - isc_quota_detach("a); + if (quota != NULL) { + isc_quota_detach("a); + } return (result); } @@ -942,6 +945,17 @@ accept_connection(isc_nmsocket_t *ssock, isc_quota_t *quota) { event = isc__nm_get_ievent(ssock->mgr, netievent_tcpchildaccept); /* Duplicate the server socket */ + r = isc_uv_export((uv_stream_t *)uvstream, &event->streaminfo); + if (r != 0) { + result = isc_errno_toresult(errno); + uv_close((uv_handle_t *)uvstream, free_uvtcpt); + if (quota != NULL) { + isc_quota_detach("a); + } + isc__nm_put_ievent(ssock->mgr, event); + return (result); + } + isc_nmsocket_t *csock = isc_mem_get(ssock->mgr->mctx, sizeof(isc_nmsocket_t)); isc__nmsocket_init(csock, ssock->mgr, isc_nm_tcpsocket, ssock->iface); @@ -954,9 +968,6 @@ accept_connection(isc_nmsocket_t *ssock, isc_quota_t *quota) { event->sock = csock; event->quota = quota; - r = isc_uv_export((uv_stream_t *)uvstream, &event->streaminfo); - RUNTIME_CHECK(r == 0); - uv_close((uv_handle_t *)uvstream, free_uvtcpt); if (w == isc_nm_tid()) {