Skip to content

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.

fix(windows): enforce index worker memory budgets

Subgurim3507d9555d73
diff --git a/src/foundation/subprocess.c b/src/foundation/subprocess.c
index 196165cea..a12bd19c4 100644
--- a/src/foundation/subprocess.c
+++ b/src/foundation/subprocess.c
@@ -411,6 +411,7 @@ struct cbm_subprocess {
void *log_ud;
int quiet_timeout_ms;
int cancel_grace_ms;
+ size_t memory_limit_bytes;
bool delete_log_on_exit;
long tail_pos;
@@ -525,6 +526,7 @@ static cbm_subprocess_t *cbm_subprocess_copy_opts(const cbm_proc_opts_t *opts) {
if (process->cancel_grace_ms > CBM_SUBPROCESS_MAX_CANCEL_GRACE_MS) {
process->cancel_grace_ms = CBM_SUBPROCESS_MAX_CANCEL_GRACE_MS;
}
+ process->memory_limit_bytes = opts->memory_limit_bytes;
process->delete_log_on_exit = opts->delete_log_on_exit;
atomic_init(&process->lifecycle, CBM_SUBPROCESS_ACTIVE);
cbm_subprocess_result_init(&process->result);
@@ -658,6 +660,10 @@ static int cbm_subprocess_spawn_win(cbm_subprocess_t *process) {
JOBOBJECT_EXTENDED_LIMIT_INFORMATION limits;
ZeroMemory(&limits, sizeof(limits));
limits.BasicLimitInformation.LimitFlags = JOB_OBJECT_LIMIT_KILL_ON_JOB_CLOSE;
+ if (process->memory_limit_bytes > 0) {
+ limits.BasicLimitInformation.LimitFlags |= JOB_OBJECT_LIMIT_JOB_MEMORY;
+ limits.JobMemoryLimit = (SIZE_T)process->memory_limit_bytes;
+ }
HANDLE job = CreateJobObjectW(NULL, NULL);
if (!job ||
!SetInformationJobObject(job, JobObjectExtendedLimitInformation, &limits, sizeof(limits))) {
diff --git a/src/foundation/subprocess.h b/src/foundation/subprocess.h
index 62592c592..df3fba844 100644
--- a/src/foundation/subprocess.h
+++ b/src/foundation/subprocess.h
@@ -68,6 +68,8 @@ typedef struct {
* ms with no new completed log line */
int cancel_grace_ms; /* graceful tree-termination window; <= 0 uses the finite
* CBM_SUBPROCESS_DEFAULT_CANCEL_GRACE_MS */
+ size_t memory_limit_bytes; /* Windows-only hard commit limit for the entire Job Object;
+ * 0 => no OS-enforced memory limit */
bool delete_log_on_exit; /* unlink log_file after reaping */
} cbm_proc_opts_t;
diff --git a/src/mcp/index_supervisor.c b/src/mcp/index_supervisor.c
index b5257dfc1..000bdafb0 100644
--- a/src/mcp/index_supervisor.c
+++ b/src/mcp/index_supervisor.c
@@ -695,6 +695,7 @@ int cbm_index_worker_start_with_log(const char *args_json, size_t memory_budget_
options.on_log_line = NULL;
options.log_ud = NULL;
options.quiet_timeout_ms = worker_quiet_timeout_ms();
+ options.memory_limit_bytes = memory_budget_bytes;
options.delete_log_on_exit = false;
if (cbm_subprocess_spawn(&options, &handle->process) != 0) {
(void)cbm_unlink(handle->response_path);
diff --git a/tests/test_main.c b/tests/test_main.c
index 6c23050ea..d4e7f5899 100644
--- a/tests/test_main.c
+++ b/tests/test_main.c
@@ -33,6 +33,7 @@ int tf_skip_count = 0;
#include <signal.h>
#ifdef _WIN32
#include <winsock2.h> /* #798 follow-up: socket-isolation re-exec probe */
+#include <windows.h>
#else
#include <unistd.h>
#ifdef __APPLE__
@@ -50,6 +51,29 @@ int tf_skip_count = 0;
* unwind after either production containment or the test's verified backstop. */
#define TF_BLOCKING_GIT_MARKER_ENV "CBM_TEST_RUNTIME_BLOCKING_GIT_PID_FILE"
+/* Native child for subprocess_windows_job_object_enforces_memory_limit. The
+ * fixed-size commit keeps the RED path bounded: without a Job memory limit it
+ * succeeds and exits 0; with the limit it is denied and exits with the sentinel
+ * code expected by the parent test. */
+static int tf_maybe_run_windows_memory_limit_probe(int argc, char **argv) {
+#ifdef _WIN32
+ if (argc == 2 && argv && strcmp(argv[1], "__cbm_windows_memory_limit_probe") == 0) {
+ const SIZE_T allocation_size = (SIZE_T)512U * 1024U * 1024U;
+ void *allocation =
+ VirtualAlloc(NULL, allocation_size, MEM_RESERVE | MEM_COMMIT, PAGE_READWRITE);
+ if (!allocation) {
+ return 73;
+ }
+ (void)VirtualFree(allocation, 0, MEM_RELEASE);
+ return 0;
+ }
+#else
+ (void)argc;
+ (void)argv;
+#endif
+ return -1;
+}
+
#ifdef _WIN32
static bool tf_invoked_as_windows_git_module(void) {
/* CreateProcessW authenticates the executable with lpApplicationName, but
@@ -892,6 +916,10 @@ int main(int argc, char **argv) {
(void)cbm_setenv("CBM_TEST_BUILD_FINGERPRINT",
"0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef", 1);
}
+ int memory_limit_probe_rc = tf_maybe_run_windows_memory_limit_probe(argc, argv);
+ if (memory_limit_probe_rc >= 0) {
+ return memory_limit_probe_rc;
+ }
int blocking_git_rc = tf_maybe_run_blocking_git_probe(argc, argv);
if (blocking_git_rc >= 0) {
return blocking_git_rc;
diff --git a/tests/test_subprocess.c b/tests/test_subprocess.c
index 1554821c6..43d06daa8 100644
--- a/tests/test_subprocess.c
+++ b/tests/test_subprocess.c
@@ -744,6 +744,30 @@ TEST(subprocess_windows_job_object_cancellation_quiesces_descendant_tree) {
#endif
}
+TEST(subprocess_windows_job_object_enforces_memory_limit) {
+#ifndef _WIN32
+ SKIP_PLATFORM("native Windows Job Object memory-limit probe");
+#else
+ char self_path[32768];
+ DWORD self_length = GetModuleFileNameA(NULL, self_path, (DWORD)sizeof(self_path));
+ ASSERT_TRUE(self_length > 0 && self_length < sizeof(self_path));
+ const char *argv[] = {self_path, "__cbm_windows_memory_limit_probe", NULL};
+ cbm_proc_opts_t opts = {0};
+ opts.bin = self_path;
+ opts.argv = argv;
+ opts.memory_limit_bytes = (size_t)512U * 1024U * 1024U;
+ opts.quiet_timeout_ms = 5000;
+
+ cbm_proc_result_t result = {0};
+ ASSERT_EQ(cbm_subprocess_run(&opts, &result), 0);
+ ASSERT_EQ(result.outcome, CBM_PROC_EXIT_NONZERO);
+ ASSERT_EQ(result.exit_code, 73);
+ ASSERT_TRUE(result.tree_quiesced);
+ ASSERT_FALSE(result.supervision_failed);
+ PASS();
+#endif
+}
+
TEST(subprocess_cancel_grace_is_hard_capped) {
#ifdef _WIN32
SKIP_PLATFORM("POSIX process-group grace cap probe; native Windows coverage pending");
@@ -1228,6 +1252,7 @@ SUITE(subprocess) {
RUN_TEST(subprocess_cancel_is_idempotent_and_kills_ignoring_tree);
RUN_TEST(subprocess_quiet_timeout_kills_ignoring_tree);
RUN_TEST(subprocess_windows_job_object_cancellation_quiesces_descendant_tree);
+ RUN_TEST(subprocess_windows_job_object_enforces_memory_limit);
RUN_TEST(subprocess_cancel_grace_is_hard_capped);
RUN_TEST(subprocess_poll_log_delivery_is_bounded_and_terminal_is_lossless);
RUN_TEST(subprocess_final_log_drain_error_is_terminal_and_preserves_classification);