@@ -20,21 +20,21 @@ pub fn main() !void {
20
20
const child_path = it .next () orelse unreachable ;
21
21
22
22
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().
34
26
{
35
- try os .enableInheritance (pipe [pipe_rd ]);
36
27
defer os .close (pipe [pipe_rd ]);
37
28
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 ]);
38
38
try child_proc .spawn ();
39
39
}
40
40
@@ -43,7 +43,6 @@ pub fn main() !void {
43
43
std .debug .assert (! is_inheritable );
44
44
45
45
var file_out = std.fs.File { .handle = pipe [pipe_wr ] };
46
- defer file_out .close ();
47
46
const file_out_writer = file_out .writer ();
48
47
try file_out_writer .writeAll ("test123\x17 " ); // ETB = \x17
49
48
const ret_val = try child_proc .wait ();
0 commit comments