authorgravatar for kcbanner@gmail.comCasey Banner <kcbanner@gmail.com> 2022-12-06 23:15:54-05:00
committergravatar for noreply@github.comGitHub <noreply@github.com> 2022-12-06 23:15:54-05:00
log8ccb9a6ad327a4d7fbc321b33d4aa66a27a1f5ee
tree13556a64c4a9af587f12605085b4c880334772e3
parent14416b522e697997f21dd634bfdc70fbff59471b
signature Signed by PGP key 4AEE18F83AFDEB23

cache: Fix LockViolation during C compilation paths (#13591)

- C compilation flows didn't hold an exclusive lock on the cache manifest file when writing to it in all cases - On windows, explicitly unlock the file lock before closing it

3 files changed, 23 insertions(+), 1 deletions(-)

src/Cache.zig+18-1
......@@ -232,6 +232,12 @@ pub const Lock = struct {
232232 manifest_file: fs.File,
233233
234234 pub fn release(lock: *Lock) void {
235 if (builtin.os.tag == .windows) {
236 // Windows does not guarantee that locks are immediately unlocked when
237 // the file handle is closed. See LockFileEx documentation.
238 lock.manifest_file.unlock();
239 }
240
235241 lock.manifest_file.close();
236242 lock.* = undefined;
237243 }
......@@ -554,7 +560,10 @@ pub const Manifest = struct {
554560 return false;
555561 }
556562
557 try self.downgradeToSharedLock();
563 if (self.want_shared_lock) {
564 try self.downgradeToSharedLock();
565 }
566
558567 return true;
559568 }
560569
......@@ -866,11 +875,13 @@ pub const Manifest = struct {
866875 const manifest_file = self.manifest_file.?;
867876 try manifest_file.downgradeLock();
868877 }
878
869879 self.have_exclusive_lock = false;
870880 }
871881
872882 fn upgradeToExclusiveLock(self: *Manifest) !void {
873883 if (self.have_exclusive_lock) return;
884 assert(self.manifest_file != null);
874885
875886 // WASI does not currently support flock, so we bypass it here.
876887 // TODO: If/when flock is supported on WASI, this check should be removed.
......@@ -892,6 +903,7 @@ pub const Manifest = struct {
892903 const lock: Lock = .{
893904 .manifest_file = self.manifest_file.?,
894905 };
906
895907 self.manifest_file = null;
896908 return lock;
897909 }
......@@ -901,6 +913,11 @@ pub const Manifest = struct {
901913 /// Don't forget to call `writeManifest` before this!
902914 pub fn deinit(self: *Manifest) void {
903915 if (self.manifest_file) |file| {
916 if (builtin.os.tag == .windows) {
917 // See Lock.release for why this is required on Windows
918 file.unlock();
919 }
920
904921 file.close();
905922 }
906923 for (self.files.items) |*file| {
src/Compilation.zig+4
......@@ -3565,6 +3565,7 @@ pub fn cImport(comp: *Compilation, c_src: []const u8) !CImportResult {
35653565 const cimport_zig_basename = "cimport.zig";
35663566
35673567 var man = comp.obtainCObjectCacheManifest();
3568 man.want_shared_lock = false;
35683569 defer man.deinit();
35693570
35703571 man.hash.add(@as(u16, 0xb945)); // Random number to distinguish translate-c from compiling C objects
......@@ -3678,6 +3679,7 @@ pub fn cImport(comp: *Compilation, c_src: []const u8) !CImportResult {
36783679 // possible we had a hit and the manifest is dirty, for example if the file mtime changed but
36793680 // the contents were the same, we hit the cache but the manifest is dirty and we need to update
36803681 // it to prevent doing a full file content comparison the next time around.
3682 man.want_shared_lock = true;
36813683 man.writeManifest() catch |err| {
36823684 log.warn("failed to write cache manifest for C import: {s}", .{@errorName(err)});
36833685 };
......@@ -3852,6 +3854,7 @@ fn updateCObject(comp: *Compilation, c_object: *CObject, c_obj_prog_node: *std.P
38523854 }
38533855
38543856 var man = comp.obtainCObjectCacheManifest();
3857 man.want_shared_lock = false;
38553858 defer man.deinit();
38563859
38573860 man.hash.add(comp.clang_preprocessor_mode);
......@@ -4147,6 +4150,7 @@ fn updateCObject(comp: *Compilation, c_object: *CObject, c_obj_prog_node: *std.P
41474150 // possible we had a hit and the manifest is dirty, for example if the file mtime changed but
41484151 // the contents were the same, we hit the cache but the manifest is dirty and we need to update
41494152 // it to prevent doing a full file content comparison the next time around.
4153 man.want_shared_lock = true;
41504154 man.writeManifest() catch |err| {
41514155 log.warn("failed to write cache manifest when compiling '{s}': {s}", .{ c_object.src.src_path, @errorName(err) });
41524156 };
src/main.zig+1
......@@ -3505,6 +3505,7 @@ fn cmdTranslateC(comp: *Compilation, arena: Allocator, enable_cache: bool) !void
35053505 const translated_zig_basename = try std.fmt.allocPrint(arena, "{s}.zig", .{comp.bin_file.options.root_name});
35063506
35073507 var man: Cache.Manifest = comp.obtainCObjectCacheManifest();
3508 man.want_shared_lock = false;
35083509 defer if (enable_cache) man.deinit();
35093510
35103511 man.hash.add(@as(u16, 0xb945)); // Random number to distinguish translate-c from compiling C objects