From 0eed7ec9d51fe8c0dd22b72da0130ad9d31877d0 Mon Sep 17 00:00:00 2001 From: Timon Kruiper Date: Wed, 23 Sep 2020 11:41:31 +0200 Subject: [PATCH 1/2] Eventloop: Fix deadlock in linux event loop implementation A simple empty main with evented-io would not quit, because some threads were still waiting to be resumed (by the os). The os.write to the eventfd only wakes up one thread and thus there are multiple writes needed to wake up all the other threads. --- lib/std/event/loop.zig | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/lib/std/event/loop.zig b/lib/std/event/loop.zig index 2600b337b3..bc2eab3c90 100644 --- a/lib/std/event/loop.zig +++ b/lib/std/event/loop.zig @@ -687,9 +687,14 @@ pub const Loop = struct { switch (builtin.os.tag) { .linux => { - // writing 8 bytes to an eventfd cannot fail - const amt = os.write(self.os_data.final_eventfd, &wakeup_bytes) catch unreachable; - assert(amt == wakeup_bytes.len); + // writing to the eventfd will only wake up one thread, thus multiple writes + // are needed to wakeup all the threads + var i: usize = 0; + while (i < self.extra_threads.len + 1) : (i += 1) { + // writing 8 bytes to an eventfd cannot fail + const amt = os.write(self.os_data.final_eventfd, &wakeup_bytes) catch unreachable; + assert(amt == wakeup_bytes.len); + } return; }, .macosx, .freebsd, .netbsd, .dragonfly => { From bbff6bd6754bbddef03e163406d09c748a479073 Mon Sep 17 00:00:00 2001 From: Timon Kruiper Date: Wed, 23 Sep 2020 18:38:28 +0200 Subject: [PATCH 2/2] Eventloop: Enable basic event loop test, fixed by previous commit Closes #4922 --- lib/std/event/loop.zig | 5 ----- 1 file changed, 5 deletions(-) diff --git a/lib/std/event/loop.zig b/lib/std/event/loop.zig index bc2eab3c90..c547f50365 100644 --- a/lib/std/event/loop.zig +++ b/lib/std/event/loop.zig @@ -1257,11 +1257,6 @@ test "std.event.Loop - basic" { // https://github.com/ziglang/zig/issues/1908 if (builtin.single_threaded) return error.SkipZigTest; - if (true) { - // https://github.com/ziglang/zig/issues/4922 - return error.SkipZigTest; - } - var loop: Loop = undefined; try loop.initMultiThreaded(); defer loop.deinit();