@@ -20,21 +20,21 @@ pub fn main() !void {
2020 const child_path = it .next () orelse unreachable ;
2121
2222 var pipe = try child_process .portablePipe ();
23-
24- // write read side of pipe to string + add to spawn command
25- var buf : [os .handleCharSize ]u8 = comptime [_ ]u8 {0 } ** os .handleCharSize ;
26- const s_handle = try os .handleToString (pipe [pipe_rd ], & buf );
27- var child_proc = ChildProcess .init (
28- &.{ child_path , s_handle },
29- gpa ,
30- );
31-
32- // enabling of file inheritance directly before and closing directly after spawn
33- // less time to leak => better
23+ defer os .close (pipe [pipe_wr ]);
24+ var child_proc : ChildProcess = undefined ;
25+ // spawn block ensures read end of pipe always closed + shortly closed after spawn().
3426 {
35- try os .enableInheritance (pipe [pipe_rd ]);
3627 defer os .close (pipe [pipe_rd ]);
3728
29+ var buf : [os .handleCharSize ]u8 = comptime [_ ]u8 {0 } ** os .handleCharSize ;
30+ const s_handle = os .handleToString (pipe [pipe_rd ], & buf ) catch unreachable ;
31+ child_proc = ChildProcess .init (
32+ &.{ child_path , s_handle },
33+ gpa ,
34+ );
35+
36+ // less time to leak read end of pipe => better
37+ try os .enableInheritance (pipe [pipe_rd ]);
3838 try child_proc .spawn ();
3939 }
4040
@@ -43,7 +43,6 @@ pub fn main() !void {
4343 std .debug .assert (! is_inheritable );
4444
4545 var file_out = std.fs.File { .handle = pipe [pipe_wr ] };
46- defer file_out .close ();
4746 const file_out_writer = file_out .writer ();
4847 try file_out_writer .writeAll ("test123\x17 " ); // ETB = \x17
4948 const ret_val = try child_proc .wait ();
0 commit comments