authorgravatar for alex@alexrp.comAlex Rønne Petersen <alex@alexrp.com> 2025-12-06 09:53:20+01:00
committergravatar for alex@alexrp.comAlex Rønne Petersen <alex@alexrp.com> 2025-12-06 09:54:43+01:00
log621e1d7b1e560901e2a5cbdca39f173f6592a004
tree68c4c96378dc74ba129f459d6fcecfb807e6747d
parent28c5cc390c909db06782eb2366d315c5da501a5f
signaturebadge-check Signed by SSH key SHA256:7B/LJ7bpR1eX8aCXSr4mtd5M45VMPKcx9zY8e95b5QM

Compilation: fix appendFileSystemInput race between main thread and C object workers

Co-authored-by: Matthew Lugg <mlugg@mlugg.co.uk>

1 files changed, 5 insertions(+), 4 deletions(-)

src/Compilation.zig+5-4
......@@ -3231,6 +3231,7 @@ pub fn update(comp: *Compilation, main_progress_node: std.Progress.Node) UpdateE
32313231 }
32323232}
32333233
3234/// Thread-safe. Assumes that `comp.mutex` is *not* already held by the caller.
32343235pub fn appendFileSystemInput(comp: *Compilation, path: Compilation.Path) Allocator.Error!void {
32353236 const gpa = comp.gpa;
32363237 const fsi = comp.file_system_inputs orelse return;
......@@ -3251,6 +3252,10 @@ pub fn appendFileSystemInput(comp: *Compilation, path: Compilation.Path) Allocat
32513252 .{ @tagName(path.root), want_prefix_dir, path.sub_path },
32523253 );
32533254
3255 // There may be concurrent calls to this function from C object workers and/or the main thread.
3256 comp.mutex.lock();
3257 defer comp.mutex.unlock();
3258
32543259 try fsi.ensureUnusedCapacity(gpa, path.sub_path.len + 3);
32553260 if (fsi.items.len > 0) fsi.appendAssumeCapacity(0);
32563261 fsi.appendAssumeCapacity(prefix);
......@@ -6443,10 +6448,6 @@ fn updateCObject(comp: *Compilation, c_object: *CObject, c_obj_prog_node: std.Pr
64436448 return error.InvalidDepFile;
64446449 },
64456450 };
6446
6447 // There may be concurrent calls to `appendFileSystemInput` from other C objects.
6448 comp.mutex.lock();
6449 defer comp.mutex.unlock();
64506451 try comp.appendFileSystemInput(input_path);
64516452 }
64526453 }