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

Close unneeded descriptors on NativeProcess instantiation #85

Merged
merged 2 commits into from
Mar 7, 2024
Merged
Changes from 1 commit
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
Original file line number Diff line number Diff line change
Expand Up @@ -47,26 +47,36 @@ internal class StdioHandle private constructor(

// This is invoked once and only once upon NativeProcess
// instantiation (after fork occurs). We can do some descriptor
// clean up here and close unneeded pipe ends which were duped
// clean up here and close unneeded descriptors which were duped
// in the child process and remain open over there.
if (stdoutFD is StdioDescriptor.Pipe) {
try {
stdoutFD.write.close()
} catch (_: IOException) {}
}

if (stderrFD is StdioDescriptor.Pipe) {
try {
stderrFD.write.close()
} catch (_: IOException) {}
}
try {
if (stdinFD is StdioDescriptor.Pipe) {
// Leave write end open here in parent
stdinFD.read
} else {
stdinFD
}.close()
} catch (_: IOException) {}

if (stdinFD !is StdioDescriptor.Pipe) return@Instance null
try {
if (stdoutFD is StdioDescriptor.Pipe) {
// Leave read end open here in parent
stdoutFD.write
} else {
stdoutFD
}.close()
} catch (_: IOException) {}

try {
stdinFD.read.close()
if (stderrFD is StdioDescriptor.Pipe) {
// Leave read end open here in parent
stderrFD.write
} else {
stdoutFD
}.close()
} catch (_: IOException) {}

if (stdinFD !is StdioDescriptor.Pipe) return@Instance null
if (stdinFD.isClosed) return@Instance null

WriteStream.of(stdinFD)
Expand Down
Loading