Skip to content

Commit e7d50f1

Browse files
committed
Process: only close the read end of the pipe after reading the full content successfully
If we close the read end of the pipe after hitting an error (EIO for example), the child process may be still running and will print more content into the write end of the pipe. Such write to a closed-read pipe will cause a SIGPIPE signal and fail. Potentiall a fix to rdar://74058113
1 parent 2954e55 commit e7d50f1

File tree

1 file changed

+8
-2
lines changed

1 file changed

+8
-2
lines changed

Sources/TSCBasic/Process.swift

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -611,6 +611,14 @@ public final class Process: ObjectIdentifierProtocol {
611611
break loop
612612
}
613613
case 0:
614+
// Close the read end of the output pipe.
615+
// We should avoid closing the read end of the pipe in case
616+
// -1 because the child process may still have content to be
617+
// flushed into the write end of the pipe. If the read end of the
618+
// pipe is closed, then a write will cause a SIGPIPE signal to
619+
// be generated for the calling process. If the calling process is
620+
// ignoring this signal, then write fails with the error EPIPE.
621+
close(fd)
614622
break loop
615623
default:
616624
let data = buf[0..<n]
@@ -621,8 +629,6 @@ public final class Process: ObjectIdentifierProtocol {
621629
}
622630
}
623631
}
624-
// Close the read end of the output pipe.
625-
close(fd)
626632
// Construct the output result.
627633
return error.map(Result.failure) ?? .success(out)
628634
}

0 commit comments

Comments
 (0)