Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

TSCBasic: fix race condition causing close errors #456

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 13 additions & 14 deletions Sources/TSCBasic/Process/Process.swift
Original file line number Diff line number Diff line change
Expand Up @@ -702,10 +702,6 @@ public final class Process {
// Dupe the read portion of the remote to 0.
posix_spawn_file_actions_adddup2(&fileActions, stdinPipe[0], 0)

// Close the other side's pipe since it was dupped to 0.
posix_spawn_file_actions_addclose(&fileActions, stdinPipe[0])
posix_spawn_file_actions_addclose(&fileActions, stdinPipe[1])

var outputPipe: [Int32] = [-1, -1]
var stderrPipe: [Int32] = [-1, -1]
if outputRedirection.redirectsOutput {
Expand All @@ -715,21 +711,13 @@ public final class Process {
// Open the write end of the pipe.
posix_spawn_file_actions_adddup2(&fileActions, outputPipe[1], 1)

// Close the other ends of the pipe since they were dupped to 1.
posix_spawn_file_actions_addclose(&fileActions, outputPipe[0])
posix_spawn_file_actions_addclose(&fileActions, outputPipe[1])

if outputRedirection.redirectStderr {
// If merged was requested, send stderr to stdout.
posix_spawn_file_actions_adddup2(&fileActions, 1, 2)
} else {
// If no redirect was requested, open the pipe for stderr.
try open(pipe: &stderrPipe)
posix_spawn_file_actions_adddup2(&fileActions, stderrPipe[1], 2)

// Close the other ends of the pipe since they were dupped to 2.
posix_spawn_file_actions_addclose(&fileActions, stderrPipe[0])
posix_spawn_file_actions_addclose(&fileActions, stderrPipe[1])
}
} else {
posix_spawn_file_actions_adddup2(&fileActions, 1, 1)
Expand Down Expand Up @@ -1351,9 +1339,20 @@ private func WTERMSIG(_ status: Int32) -> Int32 {

/// Open the given pipe.
private func open(pipe: inout [Int32]) throws {
let rv = TSCLibc.pipe(&pipe)
var rv = TSCLibc.pipe(&pipe)
guard rv == 0 else {
throw SystemError.pipe(rv)
throw SystemError.pipe(errno)
}

rv = TSCLibc.fcntl(pipe[0], F_SETFD, FD_CLOEXEC)
if rv == 0 {
rv = TSCLibc.fcntl(pipe[1], F_SETFD, FD_CLOEXEC)
}
guard rv != -1 else {
let err = errno
TSCLibc.close(pipe[0])
TSCLibc.close(pipe[1])
throw SystemError.fcntl(errno)
}
}

Expand Down
3 changes: 3 additions & 0 deletions Sources/TSCBasic/misc.swift
Original file line number Diff line number Diff line change
Expand Up @@ -298,6 +298,7 @@ public enum SystemError: Error {
case close(Int32)
case exec(Int32, path: String, args: [String])
case pipe(Int32)
case fcntl(Int32)
case posix_spawn(Int32, [String])
case read(Int32)
case setenv(Int32, String)
Expand Down Expand Up @@ -360,6 +361,8 @@ extension SystemError: CustomStringConvertible {
return "exec error: \(strerror(errno)): \(path) \(joinedArgs)"
case .pipe(let errno):
return "pipe error: \(strerror(errno))"
case .fcntl(let errno):
return "fcntl error: \(strerror(errno))"
case .posix_spawn(let errno, let args):
return "posix_spawn error: \(strerror(errno)), `\(args)`"
case .read(let errno):
Expand Down