From: Thomas Abraham Date: Mon, 30 Jun 2025 18:03:56 +0000 (-0400) Subject: ensure file descriptors 0-2 are in use before using libuv X-Git-Tag: v9.21.12~17^2~1 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=5cfdbeba72092e3e5f48f4b071084585efc5219f;p=thirdparty%2Fbind9.git ensure file descriptors 0-2 are in use before using libuv libuv expects file descriptors <= STDERR_FILENO are in use. otherwise, it may abort when closing a file descriptor it opened. See https://github.com/libuv/libuv/pull/4559 Closes #5226 --- diff --git a/lib/isc/uv.c b/lib/isc/uv.c index f0dcd51212d..c0361746cae 100644 --- a/lib/isc/uv.c +++ b/lib/isc/uv.c @@ -130,6 +130,21 @@ isc__uv_free(void *ptr) { void isc__uv_initialize(void) { + /* + * Ensure the first 3 file descriptors are open + * otherwise, libuv may use one and trigger abort + * when closing it. + * + * See https://github.com/libuv/libuv/pull/4559 + */ + do { + int fd = open("/dev/null", O_RDWR, 0); + RUNTIME_CHECK(fd >= 0); + if (fd > STDERR_FILENO) { + close(fd); + break; + } + } while (true); #if UV_VERSION_HEX >= UV_VERSION(1, 38, 0) int r; isc_mem_create("uv", &isc__uv_mctx);