2zw

reap child

37274b0937831c01aab9f5679fe6b285210b4fcb

SM <seb.michalk@gmail.com>

2026-01-09 21:16:11 +0000

 src/bar.zig | 19 +++++++++++++++----
 1 file changed, 15 insertions(+), 4 deletions(-)

diff --git a/src/bar.zig b/src/bar.zig
index 12e1e46..cb2ee02 100644
--- a/src/bar.zig
+++ b/src/bar.zig
@@ -21,6 +21,7 @@
 // SOFTWARE.
 
 const std = @import("std");
+const os = std.os;
 const builtin = @import("builtin");
 const ArrayList = std.array_list.Managed;
 const C = @import("c.zig").C;
@@ -492,10 +493,7 @@ fn readbat_openbsd(alloc: std.mem.Allocator) !u8 {
     child.stdout_behavior = .Pipe;
     child.stderr_behavior = .Ignore;
     try child.spawn();
-
-    defer {
-        _ = child.wait() catch {};
-    }
+    defer reapChild(child.id);
 
     const stdout_file = child.stdout orelse return error.MissingStdout;
     defer stdout_file.close();
@@ -515,6 +513,19 @@ fn readbat_openbsd(alloc: std.mem.Allocator) !u8 {
     return error.Invalid;
 }
 
+fn reapChild(pid: std.posix.pid_t) void {
+    var status: c_int = 0;
+    while (true) {
+        const rc = os.system.waitpid(pid, &status, 0);
+        switch (std.posix.errno(rc)) {
+            .SUCCESS => return,
+            .INTR => continue,
+            .CHILD => return,
+            else => return,
+        }
+    }
+}
+
 fn parsePercent(text: []const u8) !u8 {
     const number_text = std.mem.trim(u8, text, &std.ascii.whitespace);
     if (number_text.len == 0) return error.Invalid;