repos / zmx

session persistence for terminal processes
git clone https://github.com/neurosnap/zmx.git

commit
1f5f0b8
parent
c6ee93c
author
Eric Bower
date
2025-11-25 21:03:32 -0500 EST
fix: cpu spike from unhandled events in our daemon and client loops
1 files changed,  +6, -3
M src/main.zig
+6, -3
 1@@ -385,7 +385,7 @@ fn clientLoop(_: *Cfg, client_sock_fd: i32) !void {
 2         };
 3 
 4         // Handle stdin -> socket (Input)
 5-        if (poll_fds.items[0].revents & (posix.POLL.IN | posix.POLL.HUP | posix.POLL.ERR) != 0) {
 6+        if (poll_fds.items[0].revents & (posix.POLL.IN | posix.POLL.HUP | posix.POLL.ERR | posix.POLL.NVAL) != 0) {
 7             var buf: [4096]u8 = undefined;
 8             const n_opt: ?usize = posix.read(stdin_fd, &buf) catch |err| blk: {
 9                 if (err == error.WouldBlock) break :blk null;
10@@ -512,7 +512,10 @@ fn daemonLoop(daemon: *Daemon, server_sock_fd: i32, pty_fd: i32) !void {
11             return err;
12         };
13 
14-        if (poll_fds.items[0].revents & posix.POLL.IN != 0) {
15+        if (poll_fds.items[0].revents & (posix.POLL.ERR | posix.POLL.HUP | posix.POLL.NVAL) != 0) {
16+            std.log.err("server socket error revents={}", .{poll_fds.items[0].revents});
17+            should_exit = true;
18+        } else if (poll_fds.items[0].revents & posix.POLL.IN != 0) {
19             const client_fd = try posix.accept(server_sock_fd, null, null, posix.SOCK.NONBLOCK | posix.SOCK.CLOEXEC);
20             const client = try daemon.alloc.create(Client);
21             client.* = Client{
22@@ -525,7 +528,7 @@ fn daemonLoop(daemon: *Daemon, server_sock_fd: i32, pty_fd: i32) !void {
23             try daemon.clients.append(daemon.alloc, client);
24         }
25 
26-        if (poll_fds.items[1].revents & (posix.POLL.IN | posix.POLL.HUP | posix.POLL.ERR) != 0) {
27+        if (poll_fds.items[1].revents & (posix.POLL.IN | posix.POLL.HUP | posix.POLL.ERR | posix.POLL.NVAL) != 0) {
28             // Read from PTY
29             var buf: [4096]u8 = undefined;
30             const n_opt: ?usize = posix.read(pty_fd, &buf) catch |err| blk: {