authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2024-05-27 15:22:18-07:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2024-05-27 20:56:49-07:00
logaca7feb8fac2fae8f0b79a2cfac2a248bcd8451b
tree269729d1065e96d544ddc3a7cd1fd803b1f91c44
parentbb1f4d2bdafe669ef251d93c0aa13a9cbaf2ecf2

std.Progress: fix race condition with setIpcFd

The update thread was sometimes reading the special state and then incorrectly getting 0 for the file descriptor, making it hang since it tried to read from stdin.

1 files changed, 19 insertions(+), 7 deletions(-)

lib/std/Progress.zig+19-7
......@@ -85,6 +85,7 @@ pub const Node = struct {
8585 estimated_total_count: u32,
8686 name: [max_name_len]u8,
8787
88 /// Not thread-safe.
8889 fn getIpcFd(s: Storage) ?posix.fd_t {
8990 return if (s.estimated_total_count == std.math.maxInt(u32)) switch (@typeInfo(posix.fd_t)) {
9091 .Int => @bitCast(s.completed_count),
......@@ -93,15 +94,21 @@ pub const Node = struct {
9394 } else null;
9495 }
9596
97 /// Thread-safe.
9698 fn setIpcFd(s: *Storage, fd: posix.fd_t) void {
97 s.estimated_total_count = std.math.maxInt(u32);
98 s.completed_count = switch (@typeInfo(posix.fd_t)) {
99 const integer: u32 = switch (@typeInfo(posix.fd_t)) {
99100 .Int => @bitCast(fd),
100101 .Pointer => @intFromPtr(fd),
101102 else => @compileError("unsupported fd_t of " ++ @typeName(posix.fd_t)),
102103 };
104 // `estimated_total_count` max int indicates the special state that
105 // causes `completed_count` to be treated as a file descriptor, so
106 // the order here matters.
107 @atomicStore(u32, &s.completed_count, integer, .seq_cst);
108 @atomicStore(u32, &s.estimated_total_count, std.math.maxInt(u32), .seq_cst);
103109 }
104110
111 /// Not thread-safe.
105112 fn byteSwap(s: *Storage) void {
106113 s.completed_count = @byteSwap(s.completed_count);
107114 s.estimated_total_count = @byteSwap(s.estimated_total_count);
......@@ -208,7 +215,9 @@ pub const Node = struct {
208215 pub fn setEstimatedTotalItems(n: Node, count: usize) void {
209216 const index = n.index.unwrap() orelse return;
210217 const storage = storageByIndex(index);
211 @atomicStore(u32, &storage.estimated_total_count, std.math.lossyCast(u32, count), .monotonic);
218 // Avoid u32 max int which is used to indicate a special state.
219 const saturated = @min(std.math.maxInt(u32) - 1, count);
220 @atomicStore(u32, &storage.estimated_total_count, saturated, .monotonic);
212221 }
213222
214223 /// Thread-safe.
......@@ -243,10 +252,13 @@ pub const Node = struct {
243252 }
244253 }
245254
246 /// Posix-only. Used by `std.process.Child`.
255 /// Posix-only. Used by `std.process.Child`. Thread-safe.
247256 pub fn setIpcFd(node: Node, fd: posix.fd_t) void {
248257 const index = node.index.unwrap() orelse return;
249 assert(fd != -1);
258 assert(fd >= 0);
259 assert(fd != posix.STDOUT_FILENO);
260 assert(fd != posix.STDIN_FILENO);
261 assert(fd != posix.STDERR_FILENO);
250262 storageByIndex(index).setIpcFd(fd);
251263 }
252264
......@@ -582,8 +594,8 @@ fn serialize(serialized_buffer: *Serialized.Buffer) Serialized {
582594 while (begin_parent != .unused) {
583595 const dest_storage = &serialized_buffer.storage[serialized_len];
584596 @memcpy(&dest_storage.name, &storage_ptr.name);
585 dest_storage.completed_count = @atomicLoad(u32, &storage_ptr.completed_count, .monotonic);
586 dest_storage.estimated_total_count = @atomicLoad(u32, &storage_ptr.estimated_total_count, .monotonic);
597 dest_storage.completed_count = @atomicLoad(u32, &storage_ptr.completed_count, .seq_cst);
598 dest_storage.estimated_total_count = @atomicLoad(u32, &storage_ptr.estimated_total_count, .seq_cst);
587599 const end_parent = @atomicLoad(Node.Parent, parent_ptr, .seq_cst);
588600 if (begin_parent == end_parent) {
589601 any_ipc = any_ipc or (dest_storage.getIpcFd() != null);