Use socketpair to implement Process.Start redirection (#34861)
authorStephen Toub <stoub@microsoft.com>
Fri, 22 May 2020 04:19:46 +0000 (00:19 -0400)
committerGitHub <noreply@github.com>
Fri, 22 May 2020 04:19:46 +0000 (00:19 -0400)
commitc44dc40b763b7c74012622a0a6120cd8ffa35ce4
tree703ee82136e864c9b6168b2723693e16c49e522d
parent1c66ad1b71bb84a16b71bb5efcef1fa999c1c8bc
Use socketpair to implement Process.Start redirection (#34861)

* Use socketpair to implement Process.Start redirection

Today on Unix, we create an anonymous pipe via pipe/pipe2 to be used for stdin/stdout/stderr on processes created by Process.Start.  We then wrap the resulting file descriptors with FileStreams to hand out via Process.StandardInput/Output/Error.  This has a few issues, however. Any async operations on the resulting stream (or wrapping stream reader) will actually be async-over-sync, and that in turn means that a) any async read will end up blocking a thread pool thread until it's satisified, and b) the operation isn't cancelable.  The implications of (b) are obvious, and the problem with (a) is that code which launches a bunch of processes and uses BeginOutput/ErrorReadLine or the like will end up blocking a bunch of thread pool threads.

This change replaces the pipe/pipe2 calls with socketpair calls, and instead of wrapping the resulting file descriptors with FileStream, wraps them in Sockets and NetworkStreams.  This gives us the full capabilities of the networking stack, which fully supports asynchronous and cancelable reads and writes.

* Try to fix macOS failures with socketpair
src/libraries/Native/Unix/System.Native/pal_process.c
src/libraries/System.Diagnostics.Process/src/System.Diagnostics.Process.csproj
src/libraries/System.Diagnostics.Process/src/System/Diagnostics/Process.Unix.cs
src/libraries/System.Diagnostics.Process/tests/ProcessStreamReadTests.cs
src/libraries/System.Net.Sockets/src/System/Net/Sockets/Socket.cs