Local code intelligence for developers and AI agents. Connect code, docs and infrastructure in a persistent graph, with CBM Lens, MCP tools, agent integrations and change-aware test selection.
C aiderastclaude-codecode-analysiscode-intelligencecodexcursorcypherdeveloper-toolsgemini-cligraph-visualizationkilocodeknowledge-graphmcpmcp-servermodel-context-protocolopencodesqlitetree-sitterwindsurf
fix(subprocess): close inherited descriptors with kernel range operation
Martin Vogel91ba8a3aa922
diff --git a/src/foundation/subprocess.c b/src/foundation/subprocess.cindex 2e08da328..3d2a14f11 100644--- a/src/foundation/subprocess.c+++ b/src/foundation/subprocess.c@@ -30,6 +30,9 @@#include <spawn.h>#endifextern char **environ;+#ifdef __linux__+#include <sys/syscall.h> /* SYS_close_range: raw syscall, see cbm_posix_close_fds_from */+#endif#include <sys/stat.h>#include <sys/wait.h>#include <unistd.h>@@ -1069,6 +1072,64 @@ static void cbm_posix_reset_child_signals(void) {(void)sigprocmask(SIG_SETMASK, &empty, NULL);}+/* Close every descriptor >= lowfd in the fork+exec child (#1484).+ *+ * The portable answer is a close() per possible descriptor, which costs+ * O(RLIMIT_NOFILE) syscalls: at nofile=524288 that is ~0.8 s between fork and+ * exec for EVERY spawn, independent of how many descriptors are actually open.+ * The kernel can do the same in one call:+ * - Linux >= 5.9: close_range(lowfd, ~0U, 0). Issued as a raw syscall, not+ * through the libc wrapper: the static musl release build has no wrapper+ * and the glibc wrapper only exists from 2.34, while the syscall number is+ * 436 on the supported x86-64 and AArch64 targets. Other architectures+ * use their header's number, or the loop if no number is available.+ * An older kernel answers+ * ENOSYS (or EPERM under a seccomp filter that predates it) and we fall back.+ * - FreeBSD/OpenBSD/NetBSD/DragonFly: closefrom(lowfd).+ * - otherwise (and on fallback): the bounded close() loop.+ * macOS never reaches this on its primary path: posix_spawn's+ * CLOEXEC_DEFAULT does the closing (see cbm_posix_spawn_apple).+ *+ * Runs between fork and exec, so it is async-signal-safe: raw syscalls and+ * close() only, no allocation, no stdio. Returns the strategy that did the+ * work so the test seam can assert which one ran. */+#if defined(__linux__) && !defined(SYS_close_range) && (defined(__x86_64__) || defined(__aarch64__))+#define SYS_close_range 436+#endif++#ifdef CBM_ENABLE_TEST_SEAMS+static bool g_force_close_range_enosys = false;+#endif++static cbm_fd_close_strategy_t cbm_posix_close_fds_from(int lowfd, long max_fd) {+#if defined(__linux__) && defined(SYS_close_range)+ bool range_unavailable = false;+#ifdef CBM_ENABLE_TEST_SEAMS+ range_unavailable = g_force_close_range_enosys;+#endif+ if (!range_unavailable && syscall(SYS_close_range, (unsigned int)lowfd, ~0U, 0U) == 0) {+ return CBM_FD_CLOSE_RANGE;+ }+#elif defined(__FreeBSD__) || defined(__OpenBSD__) || defined(__NetBSD__) || defined(__DragonFly__)+ (void)max_fd;+ closefrom(lowfd);+ return CBM_FD_CLOSEFROM;+#endif+ for (int fd = lowfd; fd < max_fd; fd++) {+ (void)close(fd);+ }+ return CBM_FD_CLOSE_LOOP;+}++#ifdef CBM_ENABLE_TEST_SEAMS+void cbm_subprocess_force_close_range_enosys_for_testing(bool force) {+ g_force_close_range_enosys = force;+}+cbm_fd_close_strategy_t cbm_subprocess_close_fds_from_for_testing(int lowfd, long max_fd) {+ return cbm_posix_close_fds_from(lowfd, max_fd);+}+#endif+/* fork+exec child setup. On Apple this runs ONLY for the exec-failure* fallback (see cbm_posix_spawn_apple), which preserves the documented* "bogus binary => child exits 127" contract across platforms. */@@ -1090,9 +1151,7 @@ static void cbm_posix_child_exec(cbm_subprocess_t *process, int input, int outpuif (output > STDERR_FILENO) {(void)close(output);}- for (int fd = STDERR_FILENO + 1; fd < max_fd; fd++) {- (void)close(fd);- }+ (void)cbm_posix_close_fds_from(STDERR_FILENO + 1, max_fd);if (process->envp) {environ = process->envp; /* execvp passes environ to the new image */}diff --git a/src/foundation/subprocess.h b/src/foundation/subprocess.hindex 333509fb6..22981b501 100644--- a/src/foundation/subprocess.h+++ b/src/foundation/subprocess.h@@ -193,4 +193,21 @@ void cbm_subprocess_force_spawn_eagain_for_testing(int attempts);int cbm_subprocess_pending_spawn_eagain_for_testing(void);#endif+/* How the POSIX fork+exec child closed its inherited descriptors (#1484). */+typedef enum {+ CBM_FD_CLOSE_RANGE = 1, /* Linux close_range(2): one syscall */+ CBM_FD_CLOSEFROM, /* BSD closefrom(3): one call */+ CBM_FD_CLOSE_LOOP /* close() per descriptor up to _SC_OPEN_MAX: O(RLIMIT_NOFILE) */+} cbm_fd_close_strategy_t;++#if defined(CBM_ENABLE_TEST_SEAMS) && !defined(_WIN32)+/* Run the child's close-inherited-descriptors step in the CALLING process for+ * descriptors >= lowfd (callers pick a lowfd above everything they need) and+ * report which strategy did the work. Test builds only. */+cbm_fd_close_strategy_t cbm_subprocess_close_fds_from_for_testing(int lowfd, long max_fd);+/* Make the close_range fast path behave as if the kernel lacked it (ENOSYS),+ * so the fallback loop is exercised deterministically. Test builds only. */+void cbm_subprocess_force_close_range_enosys_for_testing(bool force);+#endif+#endif /* CBM_SUBPROCESS_H */diff --git a/tests/test_subprocess.c b/tests/test_subprocess.cindex fb9788f76..d8bee0997 100644--- a/tests/test_subprocess.c+++ b/tests/test_subprocess.c@@ -27,6 +27,12 @@#include <sys/time.h>#include <sys/wait.h>#include <unistd.h>+#ifdef __linux__+#include <sys/syscall.h>+#if !defined(SYS_close_range) && (defined(__x86_64__) || defined(__aarch64__))+#define SYS_close_range 436+#endif+#endif#else#include <windows.h>#include "../src/foundation/win_utf8.h"@@ -1012,6 +1018,94 @@ TEST(subprocess_posix_child_closes_unrelated_descriptors) {#endif}+/* #1484: the child's close-inherited-descriptors step must not cost+ * O(RLIMIT_NOFILE) syscalls. The seam runs the exact child routine in this+ * process against two descriptors parked at the TOP of the descriptor table+ * (nothing else lives there), and reports which strategy did the work. On+ * Linux that must be close_range(2) whenever the kernel has it -- the per-fd+ * loop is the bug. The ENOSYS-injected leg proves the fallback still closes. */+#ifndef _WIN32+static bool subprocess_fd_is_closed(int fd) {+ return fcntl(fd, F_GETFD) == -1 && errno == EBADF;+}++static cbm_fd_close_strategy_t subprocess_expected_close_strategy(void) {+#if defined(__linux__) && defined(SYS_close_range)+ /* Probe the kernel on an empty range: an old kernel (< 5.9) or a seccomp+ * filter without it answers ENOSYS/EPERM, and then the loop is correct. */+ long probe = syscall(SYS_close_range, ~0U, ~0U, 0U);+ return (probe == 0 || errno == EINVAL) ? CBM_FD_CLOSE_RANGE : CBM_FD_CLOSE_LOOP;+#elif defined(__FreeBSD__) || defined(__OpenBSD__) || defined(__NetBSD__) || defined(__DragonFly__)+ return CBM_FD_CLOSEFROM;+#else+ return CBM_FD_CLOSE_LOOP;+#endif+}++/* Park two non-CLOEXEC descriptors near the top of the table; returns the lower+ * of the two and hands back the original (low) descriptor in *keep_low. The+ * kernel may cap the table below _SC_OPEN_MAX (macOS: kern.maxfilesperproc),+ * so the start point halves until F_DUPFD accepts it. */+static int subprocess_park_high_fds(int *keep_low) {+ char path[] = "/tmp/cbm-subprocess-highfd-XXXXXX";+ int low = cbm_mkstemp(path);+ if (low < 0) {+ return -1;+ }+ (void)unlink(path);+ long top = sysconf(_SC_OPEN_MAX);+ if (top <= 0 || top > 1048576L) {+ top = 1048576L;+ }+ int high = -1;+ for (long base = top - 4; high < 0 && base > 64; base /= 2) {+ high = fcntl(low, F_DUPFD, (int)base);+ }+ int higher = high >= 0 ? fcntl(low, F_DUPFD, high + 1) : -1;+ if (higher != high + 1) {+ (void)close(low);+ if (high >= 0) {+ (void)close(high);+ }+ if (higher >= 0) {+ (void)close(higher);+ }+ return -1;+ }+ *keep_low = low;+ return high;+}+#endif++TEST(subprocess_child_close_fds_uses_one_syscall_not_rlimit_loop) {+#ifdef _WIN32+ SKIP_PLATFORM("POSIX fork+exec descriptor closing; Windows uses a handle allow-list");+#else+ for (int inject_enosys = 0; inject_enosys <= 1; inject_enosys++) {+ int keep_low = -1;+ int high = subprocess_park_high_fds(&keep_low);+ ASSERT_TRUE(high > STDERR_FILENO);+ cbm_subprocess_force_close_range_enosys_for_testing(inject_enosys != 0);+ /* The loop bound covers exactly the parked pair; close_range has none. */+ cbm_fd_close_strategy_t used =+ cbm_subprocess_close_fds_from_for_testing(high, (long)high + 2);+ cbm_subprocess_force_close_range_enosys_for_testing(false);+ bool closed = subprocess_fd_is_closed(high) && subprocess_fd_is_closed(high + 1);+ bool low_survived = fcntl(keep_low, F_GETFD) >= 0;+ (void)close(keep_low);++ ASSERT_TRUE(closed);+ ASSERT_TRUE(low_survived); /* only descriptors >= lowfd are touched */+ cbm_fd_close_strategy_t expected = subprocess_expected_close_strategy();+ if (inject_enosys && expected == CBM_FD_CLOSE_RANGE) {+ expected = CBM_FD_CLOSE_LOOP; /* kernel "lacks" it: the fallback must run */+ }+ ASSERT_EQ((int)used, (int)expected);+ }+ PASS();+#endif+}+TEST(subprocess_root_exit_drains_surviving_descendant) {#ifdef _WIN32SKIP_PLATFORM("POSIX process-group descendant probe; native Windows coverage pending");@@ -1396,6 +1490,7 @@ SUITE(subprocess) {RUN_TEST(subprocess_poll_log_delivery_is_bounded_and_terminal_is_lossless);RUN_TEST(subprocess_final_log_drain_error_is_terminal_and_preserves_classification);RUN_TEST(subprocess_posix_child_closes_unrelated_descriptors);+ RUN_TEST(subprocess_child_close_fds_uses_one_syscall_not_rlimit_loop);RUN_TEST(subprocess_root_exit_drains_surviving_descendant);RUN_TEST(subprocess_strip_git_repo_env_is_per_child);RUN_TEST(popen_git_strips_repo_env_and_reports_exit_status);