From 36c390bf1484a9a549759663a2bbb9bbe82f6706 Mon Sep 17 00:00:00 2001 From: Matthew Lugg Date: Thu, 19 Mar 2026 16:48:24 +0000 Subject: [PATCH] Zcu: correctly handle tid when main thread recurses Resolves: https://codeberg.org/ziglang/zig/issues/31380 --- src/Zcu/PerThread.zig | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/src/Zcu/PerThread.zig b/src/Zcu/PerThread.zig index 60b04bb38e8601b8b593ccd2b20ae9f107272039..88dcbe2431fac65e7c3923253eaf2eacc9518771 100644 --- a/src/Zcu/PerThread.zig +++ b/src/Zcu/PerThread.zig @@ -69,19 +69,26 @@ pub const Id = if (InternPool.single_threaded) enum { /// will likely involve significant changes to the `InternPool` implementation. var available_tids: std.ArrayList(Id) = .empty; threadlocal var recursive_depth: usize = 0; - threadlocal var recursive_tid: Id = .main; + threadlocal var recursive_tid: Id = undefined; pub fn allocate(arena: Allocator, n: usize) Allocator.Error!void { assert(available_tids.items.len == 0); try available_tids.ensureTotalCapacityPrecise(arena, n - 1); for (1..n) |tid| available_tids.appendAssumeCapacity(@enumFromInt(tid)); + switch (build_options.io_mode) { + .threaded => { + // Called from the main thread, so mark ourselves as such. + recursive_depth = 1; + recursive_tid = .main; + }, + .evented => {}, + } } pub fn acquire(io: std.Io) Id { switch (build_options.io_mode) { .threaded => { recursive_depth += 1; if (recursive_depth > 1) { - assert(recursive_tid != .main); return recursive_tid; } }, @@ -106,7 +113,6 @@ pub const Id = if (InternPool.single_threaded) enum { assert(recursive_tid == tid); recursive_depth -= 1; if (recursive_depth > 0) return; - recursive_tid = .main; }, .evented => {}, } -- 2.54.0