Skip to content

Commit 0b46c27

Browse files
committed
zig test: release Compilation Cache locks
before executing child process. This fixes a deadlock when the test wanted to obtain the same lock on compiler_rt.o that was held by the process building the test binary itself.
1 parent e54fd25 commit 0b46c27

1 file changed

Lines changed: 12 additions & 3 deletions

File tree

src/main.zig

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1769,7 +1769,8 @@ fn buildOutputType(
17691769
}) catch |err| {
17701770
fatal("unable to create compilation: {}", .{@errorName(err)});
17711771
};
1772-
defer comp.destroy();
1772+
var comp_destroyed = false;
1773+
defer if (!comp_destroyed) comp.destroy();
17731774

17741775
if (show_builtin) {
17751776
return std.io.getStdOut().writeAll(try comp.generateBuiltinZigSource(arena));
@@ -1845,9 +1846,10 @@ fn buildOutputType(
18451846
if (runtime_args_start) |i| {
18461847
try argv.appendSlice(all_args[i..]);
18471848
}
1848-
// We do not execve for tests because if the test fails we want to print the error message and
1849-
// invocation below.
1849+
// We do not execve for tests because if the test fails we want to print
1850+
// the error message and invocation below.
18501851
if (std.process.can_execv and arg_mode == .run and !watch) {
1852+
// execv releases the locks; no need to destroy the Compilation here.
18511853
const err = std.process.execv(gpa, argv.items);
18521854
const cmd = try argvCmd(arena, argv.items);
18531855
fatal("the following command failed to execve with '{s}':\n{s}", .{ @errorName(err), cmd });
@@ -1859,6 +1861,13 @@ fn buildOutputType(
18591861
child.stdout_behavior = .Inherit;
18601862
child.stderr_behavior = .Inherit;
18611863

1864+
if (!watch) {
1865+
// Here we release all the locks associated with the Compilation so
1866+
// that whatever this child process wants to do won't deadlock.
1867+
comp.destroy();
1868+
comp_destroyed = true;
1869+
}
1870+
18621871
const term = try child.spawnAndWait();
18631872
switch (arg_mode) {
18641873
.run => {

0 commit comments

Comments
 (0)