Skip to content

[android] Stop leaking FDs in parent test process. #24521

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

Merged
merged 1 commit into from
May 21, 2019
Merged
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
39 changes: 39 additions & 0 deletions stdlib/private/SwiftPrivateLibcExtras/Subprocess.swift
Original file line number Diff line number Diff line change
Expand Up @@ -217,6 +217,10 @@ public func spawnChild(_ args: [String])
if pid == 0 {
// pid of 0 means we are now in the child process.
// Capture the output before executing the program.
close(childStdout.readFD)
close(childStdin.writeFD)
close(childStderr.readFD)
close(childToParentPipe.readFD)
dup2(childStdout.writeFD, STDOUT_FILENO)
dup2(childStdin.readFD, STDIN_FILENO)
dup2(childStderr.writeFD, STDERR_FILENO)
Expand Down Expand Up @@ -261,6 +265,41 @@ public func spawnChild(_ args: [String])

// Close the pipe when we're done writing the error.
close(childToParentPipe.writeFD)
} else {
close(childToParentPipe.writeFD)

// Figure out if the child’s call to execve was successful or not.
var readfds = _stdlib_fd_set()
readfds.set(childToParentPipe.readFD)
var writefds = _stdlib_fd_set()
var errorfds = _stdlib_fd_set()
errorfds.set(childToParentPipe.readFD)

var ret: CInt
repeat {
ret = _stdlib_select(&readfds, &writefds, &errorfds, nil)
} while ret == -1 && errno == EINTR
if ret <= 0 {
fatalError("select() returned an error: \(errno)")
}

if readfds.isset(childToParentPipe.readFD) || errorfds.isset(childToParentPipe.readFD) {
var childErrno: CInt = 0
let readResult: ssize_t = withUnsafeMutablePointer(to: &childErrno) {
return read(childToParentPipe.readFD, $0, MemoryLayout.size(ofValue: $0.pointee))
}
if readResult == 0 {
// We read an EOF indicating that the child's call to execve was successful.
} else if readResult < 0 {
fatalError("read() returned error: \(errno)")
} else {
// We read an error from the child.
print(String(cString: strerror(childErrno)))
preconditionFailure("execve() failed")
}
}

close(childToParentPipe.readFD)
}
#else
var fileActions = _make_posix_spawn_file_actions_t()
Expand Down