Commit a9ecb26c34

Andrew Kelley <superjoe30@gmail.com>
2017-09-17 03:07:02
std.os.ChildProcess: fix fd leak
1 parent 21a55d8
Changed files (1)
std/os/child_process.zig
@@ -65,6 +65,7 @@ pub const ChildProcess = struct {
         defer restore_SIGCHLD();
 
         if (self.term) |term| {
+            self.cleanupStreams();
             return term;
         }
         const ret = posix.kill(self.pid, posix.SIGTERM);
@@ -87,6 +88,7 @@ pub const ChildProcess = struct {
         defer restore_SIGCHLD();
 
         if (self.term) |term| {
+            self.cleanupStreams();
             return term;
         }
 
@@ -119,9 +121,9 @@ pub const ChildProcess = struct {
     }
 
     fn cleanupStreams(self: &ChildProcess) {
-        if (self.stdin) |stdin| { stdin.close(); self.allocator.free(stdin); }
-        if (self.stdout) |stdout| { stdout.close(); self.allocator.free(stdout); }
-        if (self.stderr) |stderr| { stderr.close(); self.allocator.free(stderr); }
+        if (self.stdin) |stdin| { stdin.close(); self.allocator.free(stdin); self.stdin = null; }
+        if (self.stdout) |stdout| { stdout.close(); self.allocator.free(stdout); self.stdout = null; }
+        if (self.stderr) |stderr| { stderr.close(); self.allocator.free(stderr); self.stderr = null; }
     }
 
     fn cleanupAfterWait(self: &ChildProcess, status: i32) -> %Term {