Skip to content

Commit b332c84

Browse files
Use pthread_sigmask on macOS/BSD for thread-safe signal masking
On macOS and BSD systems, sigprocmask() only affects the calling thread, not the entire process. In multi-threaded applications, pthread_sigmask() should be used instead to ensure proper signal mask behavior across all threads. This fixes a test failure in net.test.tcpConnectToHost on macOS where the multi-threaded runtime's signal mask state was causing issues. Co-authored-by: Lukáš Lalinský <[email protected]>
1 parent aa6b9a3 commit b332c84

1 file changed

Lines changed: 19 additions & 2 deletions

File tree

‎src/signal.zig‎

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,16 @@ const UnixSignalMaskRegistry = if (builtin.os.tag == .linux or
8787

8888
var mask = std.posix.sigemptyset();
8989
std.posix.sigaddset(&mask, @intCast(signum));
90-
std.posix.sigprocmask(std.posix.SIG.BLOCK, &mask, null);
90+
91+
// Use pthread_sigmask on macOS/BSD for proper multi-threaded behavior
92+
if (builtin.os.tag == .macos or builtin.os.tag == .freebsd or
93+
builtin.os.tag == .netbsd or builtin.os.tag == .openbsd)
94+
{
95+
const result = std.c.pthread_sigmask(std.posix.SIG.BLOCK, &mask, null);
96+
if (result != 0) return error.SignalMaskFailed;
97+
} else {
98+
std.posix.sigprocmask(std.posix.SIG.BLOCK, &mask, null);
99+
}
91100
}
92101
refcounts[idx] += 1;
93102
}
@@ -108,7 +117,15 @@ const UnixSignalMaskRegistry = if (builtin.os.tag == .linux or
108117

109118
var mask = std.posix.sigemptyset();
110119
std.posix.sigaddset(&mask, @intCast(signum));
111-
std.posix.sigprocmask(std.posix.SIG.UNBLOCK, &mask, null);
120+
121+
// Use pthread_sigmask on macOS/BSD for proper multi-threaded behavior
122+
if (builtin.os.tag == .macos or builtin.os.tag == .freebsd or
123+
builtin.os.tag == .netbsd or builtin.os.tag == .openbsd)
124+
{
125+
_ = std.c.pthread_sigmask(std.posix.SIG.UNBLOCK, &mask, null);
126+
} else {
127+
std.posix.sigprocmask(std.posix.SIG.UNBLOCK, &mask, null);
128+
}
112129
}
113130
}
114131
}

0 commit comments

Comments
 (0)