Skip to content

Commit 60fb698

Browse files
authored
Merge pull request #2835 from spevans/pr_process_no_fatal_error
Process: Dont call fatalError() for errors in run()
2 parents ec6a993 + 54359df commit 60fb698

File tree

1 file changed

+15
-14
lines changed

1 file changed

+15
-14
lines changed

Sources/Foundation/Process.swift

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -439,6 +439,15 @@ open class Process: NSObject {
439439
#endif
440440

441441
open func run() throws {
442+
443+
func _throwIfPosixError(_ posixErrno: Int32) throws {
444+
if posixErrno != 0 {
445+
// When this is called, self.executableURL is already known to be non-nil
446+
let userInfo: [String: Any] = [ NSURLErrorKey: self.executableURL! ]
447+
throw NSError(domain: NSPOSIXErrorDomain, code: Int(posixErrno), userInfo: userInfo)
448+
}
449+
}
450+
442451
self.processLaunchedCondition.lock()
443452
defer {
444453
self.processLaunchedCondition.broadcast()
@@ -826,11 +835,11 @@ open class Process: NSObject {
826835
CFRunLoopAddSource(managerThreadRunLoop?._cfRunLoop, source, kCFRunLoopDefaultMode)
827836

828837
let fileActions = _CFPosixSpawnFileActionsAlloc()
829-
posix(_CFPosixSpawnFileActionsInit(fileActions))
830838
defer {
831839
_CFPosixSpawnFileActionsDestroy(fileActions)
832840
_CFPosixSpawnFileActionsDealloc(fileActions)
833841
}
842+
try _throwIfPosixError(_CFPosixSpawnFileActionsInit(fileActions))
834843

835844
// File descriptors to duplicate in the child process. This allows
836845
// output redirection to NSPipe or NSFileHandle.
@@ -905,24 +914,24 @@ open class Process: NSObject {
905914
}
906915

907916
for (new, old) in adddup2 {
908-
posix(_CFPosixSpawnFileActionsAddDup2(fileActions, old, new))
917+
try _throwIfPosixError(_CFPosixSpawnFileActionsAddDup2(fileActions, old, new))
909918
}
910919
for fd in addclose.filter({ $0 >= 0 }) {
911-
posix(_CFPosixSpawnFileActionsAddClose(fileActions, fd))
920+
try _throwIfPosixError(_CFPosixSpawnFileActionsAddClose(fileActions, fd))
912921
}
913922

914923
#if canImport(Darwin)
915924
var spawnAttrs: posix_spawnattr_t? = nil
916-
posix_spawnattr_init(&spawnAttrs)
917-
posix_spawnattr_setflags(&spawnAttrs, .init(POSIX_SPAWN_CLOEXEC_DEFAULT))
925+
try _throwIfPosixError(posix_spawnattr_init(&spawnAttrs))
926+
try _throwIfPosixError(posix_spawnattr_setflags(&spawnAttrs, .init(POSIX_SPAWN_CLOEXEC_DEFAULT)))
918927
#else
919928
for fd in 3 ... findMaximumOpenFD() {
920929
guard adddup2[fd] == nil &&
921930
!addclose.contains(fd) &&
922931
fd != taskSocketPair[1] else {
923932
continue // Do not double-close descriptors, or close those pertaining to Pipes or FileHandles we want inherited.
924933
}
925-
posix(_CFPosixSpawnFileActionsAddClose(fileActions, fd))
934+
try _throwIfPosixError(_CFPosixSpawnFileActionsAddClose(fileActions, fd))
926935
}
927936
#endif
928937

@@ -1134,11 +1143,3 @@ extension Process {
11341143

11351144
public static let didTerminateNotification = NSNotification.Name(rawValue: "NSTaskDidTerminateNotification")
11361145
}
1137-
1138-
private func posix(_ code: Int32, file: StaticString = #file, line: UInt = #line) {
1139-
switch code {
1140-
case 0: return
1141-
case EBADF: fatalError("POSIX command failed with error: \(code) -- EBADF", file: file, line: line)
1142-
default: fatalError("POSIX command failed with error: \(code)", file: file, line: line)
1143-
}
1144-
}

0 commit comments

Comments
 (0)