zig

fork of https://codeberg.org/ziglang/zig
Log | Files | Refs | README | LICENSE

commit 621e1d7b1e560901e2a5cbdca39f173f6592a004 (tree)
parent 28c5cc390c909db06782eb2366d315c5da501a5f
Author: Alex Rønne Petersen <alex@alexrp.com>
Date:   Sat,  6 Dec 2025 09:53:20 +0100

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

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

Diffstat:
Msrc/Compilation.zig | 9+++++----
1 file changed, 5 insertions(+), 4 deletions(-)

diff --git a/src/Compilation.zig b/src/Compilation.zig @@ -3231,6 +3231,7 @@ pub fn update(comp: *Compilation, main_progress_node: std.Progress.Node) UpdateE } } +/// Thread-safe. Assumes that `comp.mutex` is *not* already held by the caller. pub fn appendFileSystemInput(comp: *Compilation, path: Compilation.Path) Allocator.Error!void { const gpa = comp.gpa; const fsi = comp.file_system_inputs orelse return; @@ -3251,6 +3252,10 @@ pub fn appendFileSystemInput(comp: *Compilation, path: Compilation.Path) Allocat .{ @tagName(path.root), want_prefix_dir, path.sub_path }, ); + // There may be concurrent calls to this function from C object workers and/or the main thread. + comp.mutex.lock(); + defer comp.mutex.unlock(); + try fsi.ensureUnusedCapacity(gpa, path.sub_path.len + 3); if (fsi.items.len > 0) fsi.appendAssumeCapacity(0); fsi.appendAssumeCapacity(prefix); @@ -6443,10 +6448,6 @@ fn updateCObject(comp: *Compilation, c_object: *CObject, c_obj_prog_node: std.Pr return error.InvalidDepFile; }, }; - - // There may be concurrent calls to `appendFileSystemInput` from other C objects. - comp.mutex.lock(); - defer comp.mutex.unlock(); try comp.appendFileSystemInput(input_path); } }