@@ -27,10 +27,13 @@ pub fn main() !void {
2727 .bInheritHandle = windows .TRUE ,
2828 .lpSecurityDescriptor = null ,
2929 };
30- // create pipe and enable inheritance for the read end, which will be given to the child
31- try child_process .windowsMakeAsyncPipe (& pipe [pipe_rd ], & pipe [pipe_wr ], & saAttr , .parent_to_child );
30+ // create pipe without inheritance
31+ try child_process .windowsMakeAsyncPipe (& pipe [pipe_rd ], & pipe [pipe_wr ], & saAttr );
3232 } else {
33- pipe = try os .pipe (); // leaks on default, but more portable, TODO: use pip2 and close earlier?
33+ // we could save setting and and unsetting 1 pipe end, but this would
34+ // 1. allow more leak time and 2. makes things less consistent with windows
35+ // TODO: benchmarks
36+ pipe = try os .pipe2 (@as (u32 , os .O .CLOEXEC ));
3437 }
3538
3639 // write read side of pipe to string + add to spawn command
@@ -40,21 +43,20 @@ pub fn main() !void {
4043 &.{ child_path , s_handle },
4144 gpa ,
4245 );
46+
47+ // enabling of file inheritance directly before and directly after spawn
48+ // less time to leak => better
4349 {
44- // close read side of pipe, less time for leaking, if closed immediately with posix_spawn
4550 if (os .hasPosixSpawn ) child_proc .posix_actions = try os .posix_spawn .Actions .init ();
4651 defer if (os .hasPosixSpawn ) child_proc .posix_actions .? .deinit ();
4752 if (os .hasPosixSpawn ) try child_proc .posix_actions .? .close (pipe [pipe_wr ]);
53+
54+ try os .enableInheritance (pipe [pipe_rd ]);
4855 defer os .close (pipe [pipe_rd ]);
4956
5057 try child_proc .spawn ();
5158 }
5259
53- // call fcntl on Unixes to disable handle inheritance (windows one is per default not enabled)
54- if (builtin .os .tag != .windows ) {
55- try std .os .disableFileInheritance (pipe [pipe_wr ]);
56- }
57-
5860 // windows does have inheritance disabled on default, but we check to be sure
5961 if (builtin .target .os .tag == .windows ) {
6062 var handle_flags : windows.DWORD = undefined ;
0 commit comments