Skip to content

Commit aa6b9a3

Browse files
Add refcounted signal masking for Unix platforms
Implements a global UnixSignalMaskRegistry that tracks how many Signal instances are active for each signal type (SIGINT/SIGTERM). Signal masking now only happens when the first instance is created, and unmasking only happens when the last instance is destroyed. This ensures that: - Multiple Signal instances for the same type don't redundantly mask - The process signal mask is properly restored on cleanup - No signals remain permanently blocked after deinit The registry uses a mutex-protected refcount array and integrates with both LinuxImpl (signalfd) and KqueueImpl (EVFILT_SIGNAL). Co-authored-by: Lukáš Lalinský <[email protected]>
1 parent 090e757 commit aa6b9a3

1 file changed

Lines changed: 77 additions & 14 deletions

File tree

‎src/signal.zig‎

Lines changed: 77 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,66 @@ pub const Signal = struct {
5656
}
5757
};
5858

59+
// Unix signal mask registry for reference counting
60+
const UnixSignalMaskRegistry = if (builtin.os.tag == .linux or
61+
builtin.os.tag == .macos or
62+
builtin.os.tag == .freebsd or
63+
builtin.os.tag == .netbsd or
64+
builtin.os.tag == .openbsd)
65+
struct {
66+
var mutex: std.Thread.Mutex = .{};
67+
var refcounts: [2]usize = .{ 0, 0 }; // Index 0 = SIGINT, 1 = SIGTERM
68+
69+
fn indexForSignal(signal_type: SignalType) usize {
70+
return switch (signal_type) {
71+
.int => 0,
72+
.term => 1,
73+
};
74+
}
75+
76+
fn incrementAndMaskIfNeeded(signal_type: SignalType) !void {
77+
mutex.lock();
78+
defer mutex.unlock();
79+
80+
const idx = indexForSignal(signal_type);
81+
if (refcounts[idx] == 0) {
82+
// First instance for this signal type - mask it
83+
const signum: c_int = switch (signal_type) {
84+
.int => std.posix.SIG.INT,
85+
.term => std.posix.SIG.TERM,
86+
};
87+
88+
var mask = std.posix.sigemptyset();
89+
std.posix.sigaddset(&mask, @intCast(signum));
90+
std.posix.sigprocmask(std.posix.SIG.BLOCK, &mask, null);
91+
}
92+
refcounts[idx] += 1;
93+
}
94+
95+
fn decrementAndUnmaskIfNeeded(signal_type: SignalType) void {
96+
mutex.lock();
97+
defer mutex.unlock();
98+
99+
const idx = indexForSignal(signal_type);
100+
if (refcounts[idx] > 0) {
101+
refcounts[idx] -= 1;
102+
if (refcounts[idx] == 0) {
103+
// Last instance for this signal type - unmask it
104+
const signum: c_int = switch (signal_type) {
105+
.int => std.posix.SIG.INT,
106+
.term => std.posix.SIG.TERM,
107+
};
108+
109+
var mask = std.posix.sigemptyset();
110+
std.posix.sigaddset(&mask, @intCast(signum));
111+
std.posix.sigprocmask(std.posix.SIG.UNBLOCK, &mask, null);
112+
}
113+
}
114+
}
115+
}
116+
else
117+
void;
118+
59119
// Linux implementation using signalfd
60120
const LinuxImpl = struct {
61121
signal_type: SignalType,
@@ -97,17 +157,17 @@ const LinuxImpl = struct {
97157
// Double-check after acquiring lock
98158
if (self.fd != -1) return;
99159

160+
// Increment refcount and mask signal if this is the first instance
161+
try UnixSignalMaskRegistry.incrementAndMaskIfNeeded(self.signal_type);
162+
100163
const signum: c_int = switch (self.signal_type) {
101164
.int => std.posix.SIG.INT,
102165
.term => std.posix.SIG.TERM,
103166
};
104167

105-
// Block the signal so it doesn't kill the process
168+
// Create signalfd
106169
var mask = std.posix.sigemptyset();
107170
std.posix.sigaddset(&mask, @intCast(signum));
108-
std.posix.sigprocmask(std.posix.SIG.BLOCK, &mask, null);
109-
110-
// Create signalfd
111171
self.fd = try std.posix.signalfd(-1, &mask, std.posix.SOCK.CLOEXEC | std.posix.SOCK.NONBLOCK);
112172
}
113173

@@ -118,6 +178,9 @@ const LinuxImpl = struct {
118178
if (self.fd != -1) {
119179
std.posix.close(self.fd);
120180
self.fd = -1;
181+
182+
// Decrement refcount and unmask signal if this is the last instance
183+
UnixSignalMaskRegistry.decrementAndUnmaskIfNeeded(self.signal_type);
121184
}
122185
}
123186
};
@@ -164,21 +227,21 @@ const KqueueImpl = struct {
164227
// Double-check after acquiring lock
165228
if (self.initialized) return;
166229

167-
const signum: c_int = switch (self.signal_type) {
168-
.int => std.posix.SIG.INT,
169-
.term => std.posix.SIG.TERM,
170-
};
171-
172-
// Block the signal so it doesn't kill the process
173-
var mask = std.posix.sigemptyset();
174-
std.posix.sigaddset(&mask, @intCast(signum));
175-
std.posix.sigprocmask(std.posix.SIG.BLOCK, &mask, null);
230+
// Increment refcount and mask signal if this is the first instance
231+
try UnixSignalMaskRegistry.incrementAndMaskIfNeeded(self.signal_type);
176232

177233
self.initialized = true;
178234
}
179235

180236
fn deinit(self: *KqueueImpl) void {
181-
_ = self;
237+
self.mutex.lock();
238+
defer self.mutex.unlock();
239+
240+
if (self.initialized) {
241+
// Decrement refcount and unmask signal if this is the last instance
242+
UnixSignalMaskRegistry.decrementAndUnmaskIfNeeded(self.signal_type);
243+
self.initialized = false;
244+
}
182245
}
183246
};
184247

0 commit comments

Comments
 (0)