Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions changelog.d/+group-alive-test-speed.misc.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
The test that `proc.groupAlive` stays true while signals arrive sends one signal a millisecond, as the `state.lockExclusive` one does, instead of back to back, which made it take a minute or more on several CPUs and minutes under QEMU. It runs until ten of its checks have caught a signal, so it fails rather than passes when none lands.
29 changes: 26 additions & 3 deletions src/proc.zig
Original file line number Diff line number Diff line change
Expand Up @@ -542,7 +542,11 @@ test "a set cancel flag kills the child" {
try std.testing.expectError(error.Canceled, run(gpa, "/bin/sh", &.{ "sh", "-c", "sleep 30" }, no_env, .{ .cancel = &flag }));
}

fn onUsr1Proc(_: linux.SIG) callconv(.c) void {}
var usr1_caught = std.atomic.Value(u32).init(0);

fn onUsr1Proc(_: linux.SIG) callconv(.c) void {
_ = usr1_caught.fetchAdd(1, .monotonic);
}

test "groupAlive stays true for a live group while signals arrive" {
const handler: linux.Sigaction = .{ .handler = .{ .handler = &onUsr1Proc }, .mask = linux.sigemptyset(), .flags = 0 };
Expand All @@ -552,10 +556,19 @@ test "groupAlive stays true for a live group while signals arrive" {
const Stop = struct {
var go = std.atomic.Value(bool).init(true);
fn buzz() void {
// This thread must not catch SIGUSR1, or the signal may interrupt
// it instead of the scan.
var blocked = linux.sigemptyset();
linux.sigaddset(&blocked, .USR1);
_ = linux.sigprocmask(linux.SIG.BLOCK, &blocked, null);
while (go.load(.acquire)) _ = linux.kill(linux.getpid(), .USR1);
// Without the pause a signal is always pending, the scan takes
// one after every system call, and the checks below take a
// minute on more than one CPU.
while (go.load(.acquire)) {
_ = linux.kill(linux.getpid(), .USR1);
var ts: linux.timespec = .{ .sec = 0, .nsec = std.time.ns_per_ms };
_ = linux.nanosleep(&ts, null);
}
}
};
Stop.go.store(true, .release);
Expand All @@ -565,7 +578,17 @@ test "groupAlive stays true for a live group while signals arrive" {
buzz.join();
}
const pgid: linux.pid_t = @intCast(linux.getpgid(0));
for (0..50) |_| try std.testing.expect(groupAlive(pgid));
// At least 50 checks, and on until 10 have caught a signal mid-scan, so
// the test fails rather than passes when no signal lands.
const until = nowMs() + 10_000;
var checks: usize = 0;
var interrupted: usize = 0;
while (checks < 50 or (interrupted < 10 and nowMs() < until)) : (checks += 1) {
const caught = usr1_caught.load(.monotonic);
try std.testing.expect(groupAlive(pgid));
if (usr1_caught.load(.monotonic) != caught) interrupted += 1;
}
try std.testing.expect(interrupted >= 10);
}

test "run refuses a relative program" {
Expand Down
Loading