diff --git a/std/os.zig b/std/os.zig index b89b2a9ce2..d641cf29c9 100644 --- a/std/os.zig +++ b/std/os.zig @@ -2959,6 +2959,10 @@ pub const Thread = struct { /// Returns the handle of this thread. /// On Linux and POSIX, this is the same as Id. + /// On Linux, it is possible that the thread spawned with `spawnThread` + /// finishes executing entirely before the clone syscall completes. In this + /// case, this function will return 0 rather than the no-longer-existing thread's + /// pid. pub fn handle(self: Thread) Handle { return self.data.handle; } @@ -2977,7 +2981,7 @@ pub const Thread = struct { } else switch (builtin.os) { builtin.Os.linux => { while (true) { - const pid_value = @atomicLoad(i32, &self.data.handle, builtin.AtomicOrder.SeqCst); + const pid_value = @atomicLoad(i32, &self.data.handle, .SeqCst); if (pid_value == 0) break; const rc = linux.futex_wait(&self.data.handle, linux.FUTEX_WAIT, pid_value, null); switch (linux.getErrno(rc)) { diff --git a/std/os/test.zig b/std/os/test.zig index ab21ea1568..0f34e5279c 100644 --- a/std/os/test.zig +++ b/std/os/test.zig @@ -49,7 +49,9 @@ test "std.os.Thread.getCurrentId" { switch (builtin.os) { builtin.Os.windows => expect(os.Thread.getCurrentId() != thread_current_id), else => { - expect(thread_current_id == thread_id); + // If the thread completes very quickly, then thread_id can be 0. See the + // documentation comments for `std.os.Thread.handle`. + expect(thread_id == 0 or thread_current_id == thread_id); }, } }