Skip to content

Commit 1924f10

Browse files
committed
more cleanups
1 parent 7324b06 commit 1924f10

File tree

3 files changed

+17
-15
lines changed

3 files changed

+17
-15
lines changed

lib/std/os.zig

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4871,21 +4871,21 @@ pub fn lseek_CUR_get(fd: fd_t) SeekError!u64 {
48714871
}
48724872
}
48734873

4874-
const EnableFileInheritanceError = FcntlError || windows.SetHandleInformationError;
4874+
const EnableInheritanceError = FcntlError || windows.SetHandleInformationError;
48754875

4876-
pub inline fn enableFileInheritance(file_handle: fd_t) EnableFileInheritanceError!void {
4876+
pub inline fn enableInheritance(file_handle: fd_t) EnableInheritanceError!void {
48774877
if (builtin.os.tag == .windows) {
48784878
try windows.SetHandleInformation(file_handle, windows.HANDLE_FLAG_INHERIT, 1);
48794879
} else {
4880-
var flags = fcntl(file_handle, F.GETFD, 0);
4880+
var flags = try fcntl(file_handle, F.GETFD, 0);
48814881
flags &= ~@as(u32, FD_CLOEXEC);
48824882
_ = try fcntl(file_handle, F.SETFD, flags);
48834883
}
48844884
}
48854885

4886-
const DisableFileInheritanceError = FcntlError || windows.SetHandleInformationError;
4886+
const DisableInheritanceError = FcntlError || windows.SetHandleInformationError;
48874887

4888-
pub inline fn disableFileInheritance(file_handle: fd_t) DisableFileInheritanceError!void {
4888+
pub inline fn disableInheritance(file_handle: fd_t) DisableInheritanceError!void {
48894889
if (builtin.os.tag == .windows) {
48904890
try windows.SetHandleInformation(file_handle, windows.HANDLE_FLAG_INHERIT, 0);
48914891
} else {

test/standalone/childprocess_extrapipe/child.zig

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ pub fn main() !void {
2323
var fcntl_flags = try std.os.fcntl(file_handle, std.os.F.GETFD, 0);
2424
try std.testing.expect((fcntl_flags & std.os.FD_CLOEXEC) == 0);
2525
}
26-
try std.os.disableFileInheritance(file_handle);
26+
try std.os.disableInheritance(file_handle);
2727
var file_in = std.fs.File{ .handle = file_handle }; // read side of pipe
2828
defer file_in.close();
2929
const file_in_reader = file_in.reader();

test/standalone/childprocess_extrapipe/parent.zig

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -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

Comments
 (0)