From 6b9125cbe662d530160e0732c856aa0da86894c0 Mon Sep 17 00:00:00 2001 From: Doug Coleman Date: Sun, 21 Dec 2025 18:17:04 -0600 Subject: [PATCH] build_runner: fix race condition in dispatch_deps capacity reservation Move ensureUnusedCapacity inside the mutex call to prevent a race condition where other worker threads could append to memory_blocked_steps between checking the length and iterating. repro branch: https://codeberg.org/erg/zig/src/branch/fix-30014-repro check out that branch, and depending on if you have the fix-30014 patch or not, it will either race condition or succeed Fixes #30014 --- lib/compiler/build_runner.zig | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/lib/compiler/build_runner.zig b/lib/compiler/build_runner.zig index 82d58e4592ac396bd1623f1b572d6706c46427d4..eed48e79adfa839ebdcd395c297ae1f47761c6a1 100644 --- a/lib/compiler/build_runner.zig +++ b/lib/compiler/build_runner.zig @@ -1410,12 +1410,13 @@ fn workerMakeOneStep( if (s.max_rss != 0) { var dispatch_deps: std.ArrayList(*Step) = .empty; defer dispatch_deps.deinit(gpa); - dispatch_deps.ensureUnusedCapacity(gpa, run.memory_blocked_steps.items.len) catch @panic("OOM"); { run.max_rss_mutex.lockUncancelable(io); defer run.max_rss_mutex.unlock(io); + dispatch_deps.ensureUnusedCapacity(gpa, run.memory_blocked_steps.items.len) catch @panic("OOM"); + // Give the memory back to the scheduler. run.claimed_rss -= s.max_rss; // Avoid kicking off too many tasks that we already know will not have -- 2.54.0