authorgravatar for ben@magicmoremagic.comBen Crist <ben@magicmoremagic.com> 2022-11-20 12:20:14-06:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2023-10-17 16:56:15-07:00
log6d47198303847409a900f57bbe84195b32375093
tree58cf515c3cea23b281cee0e7d41b61f7fdbbafa2
parent7a9500fd80990a3dc47821e627162bf769eecbba

return error.AlreadyTerminated from std.ChildProcess.kill when necessary


3 files changed, 12 insertions(+), 5 deletions(-)

lib/std/child_process.zig+8-2
...@@ -221,7 +221,10 @@ pub const ChildProcess = struct {...@@ -221,7 +221,10 @@ pub const ChildProcess = struct {
221 return term;221 return term;
222 }222 }
223223
224 try windows.TerminateProcess(self.id, exit_code);224 windows.TerminateProcess(self.id, exit_code) catch |err| switch (err) {
225 error.PermissionDenied => return error.AlreadyTerminated,
226 else => return err,
227 };
225 try self.waitUnwrappedWindows();228 try self.waitUnwrappedWindows();
226 return self.term.?;229 return self.term.?;
227 }230 }
...@@ -231,7 +234,10 @@ pub const ChildProcess = struct {...@@ -231,7 +234,10 @@ pub const ChildProcess = struct {
231 self.cleanupStreams();234 self.cleanupStreams();
232 return term;235 return term;
233 }236 }
234 try os.kill(self.id, os.SIG.TERM);237 os.kill(self.id, os.SIG.TERM) catch |err| switch (err) {
238 error.ProcessNotFound => return error.AlreadyTerminated,
239 else => return err,
240 };
235 try self.waitUnwrapped();241 try self.waitUnwrapped();
236 return self.term.?;242 return self.term.?;
237 }243 }
lib/std/os.zig+2-2
...@@ -639,14 +639,14 @@ pub fn raise(sig: u8) RaiseError!void {...@@ -639,14 +639,14 @@ pub fn raise(sig: u8) RaiseError!void {
639 @compileError("std.os.raise unimplemented for this target");639 @compileError("std.os.raise unimplemented for this target");
640}640}
641641
642pub const KillError = error{PermissionDenied} || UnexpectedError;642pub const KillError = error{ ProcessNotFound, PermissionDenied } || UnexpectedError;
643643
644pub fn kill(pid: pid_t, sig: u8) KillError!void {644pub fn kill(pid: pid_t, sig: u8) KillError!void {
645 switch (errno(system.kill(pid, sig))) {645 switch (errno(system.kill(pid, sig))) {
646 .SUCCESS => return,646 .SUCCESS => return,
647 .INVAL => unreachable, // invalid signal647 .INVAL => unreachable, // invalid signal
648 .PERM => return error.PermissionDenied,648 .PERM => return error.PermissionDenied,
649 .SRCH => unreachable, // always a race condition649 .SRCH => return error.ProcessNotFound,
650 else => |err| return unexpectedErrno(err),650 else => |err| return unexpectedErrno(err),
651 }651 }
652}652}
lib/std/os/windows.zig+2-1
...@@ -1593,11 +1593,12 @@ pub fn GetModuleFileNameW(hModule: ?HMODULE, buf_ptr: [*]u16, buf_len: DWORD) Ge...@@ -1593,11 +1593,12 @@ pub fn GetModuleFileNameW(hModule: ?HMODULE, buf_ptr: [*]u16, buf_len: DWORD) Ge
1593 return buf_ptr[0..rc :0];1593 return buf_ptr[0..rc :0];
1594}1594}
15951595
1596pub const TerminateProcessError = error{Unexpected};1596pub const TerminateProcessError = error{ PermissionDenied, Unexpected };
15971597
1598pub fn TerminateProcess(hProcess: HANDLE, uExitCode: UINT) TerminateProcessError!void {1598pub fn TerminateProcess(hProcess: HANDLE, uExitCode: UINT) TerminateProcessError!void {
1599 if (kernel32.TerminateProcess(hProcess, uExitCode) == 0) {1599 if (kernel32.TerminateProcess(hProcess, uExitCode) == 0) {
1600 switch (kernel32.GetLastError()) {1600 switch (kernel32.GetLastError()) {
1601 Win32Error.ACCESS_DENIED => return error.PermissionDenied,
1601 else => |err| return unexpectedError(err),1602 else => |err| return unexpectedError(err),
1602 }1603 }
1603 }1604 }