From 312902931f0d69d2519f8c9074a610cdc338f29b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lo=C3=AFc=20Gr=C3=A9goire?= Date: Sun, 2 Aug 2026 21:45:47 -0400 Subject: [PATCH] ps: fix stack freeing bug The thread could still be running on its stack whenever the stack ends up getting reaped, which would free and reuse the stack before it stops being used. Lock and unlock the thread to wait until it finishes switching. --- src/proc/thread.zig | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/src/proc/thread.zig b/src/proc/thread.zig index 77d0a89..3dc9349 100644 --- a/src/proc/thread.zig +++ b/src/proc/thread.zig @@ -25,6 +25,11 @@ fn reap_thread(obj: *anyopaque, _: ?*anyopaque) void { // Bleh const entry: *rtl.List.Entry = @fieldParentPtr("next", link); const ketd: *ke.Thread = @fieldParentPtr("runq_link", entry); + + // Wait until the thread finishes switching off its stack. + const ipl = ketd.lock.acquire(); + ketd.lock.release(ipl); + const td: *Thread = @fieldParentPtr("kern", ketd); psp.turnstile_zone.destroy(td.kern.turnstile);