Commit 0eed7ec9d5

Timon Kruiper <timonkruiper@gmail.com>
2020-09-23 11:41:31
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.
1 parent 58ee5f4
Changed files (1)
lib
std
event
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 => {