From 621e1d7b1e560901e2a5cbdca39f173f6592a004 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alex=20R=C3=B8nne=20Petersen?= Date: Sat, 6 Dec 2025 09:53:20 +0100 Subject: [PATCH] Compilation: fix appendFileSystemInput race between main thread and C object workers Co-authored-by: Matthew Lugg --- src/Compilation.zig | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/src/Compilation.zig b/src/Compilation.zig index 138755773877254786b45dcb5a9cbc4bbaba6805..c40e0259552858315593284b3cb6acf4ecf6c8ea 100644 --- 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); } } -- 2.54.0