From 6b594943ba7cab40055dd0f39bba86c9152c5f5d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Juan=20Jos=C3=A9=20Arboleda?= Date: Fri, 27 Mar 2026 17:38:10 -0500 Subject: [PATCH] unix: handle cloexec failure in kqueue init MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Refactor uv__kqueue_init to use a temporary fd variable and properly handle errors from uv__cloexec. If setting CLOEXEC fails, close the kqueue fd and return the error instead of leaking the descriptor. Fixes: https://github.com/libuv/libuv/issues/5081 Signed-off-by: Juan José Arboleda --- src/unix/kqueue.c | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/src/unix/kqueue.c b/src/unix/kqueue.c index da19e3bc8..9971f2abd 100644 --- a/src/unix/kqueue.c +++ b/src/unix/kqueue.c @@ -50,12 +50,20 @@ int uv__kqueue_init(uv_loop_t* loop) { - loop->backend_fd = kqueue(); - if (loop->backend_fd == -1) + int fd; + int err; + + fd = kqueue(); + if (fd == -1) return UV__ERR(errno); - uv__cloexec(loop->backend_fd, 1); + err = uv__cloexec(fd, 1); + if (err) { + uv__close(fd); + return err; + } + loop->backend_fd = fd; return 0; }