Skip to content
This repository was archived by the owner on Jan 23, 2023. It is now read-only.

Make Process.Start wait for child process to exec - #5421

Merged
stephentoub merged 1 commit into
dotnet:masterfrom
stephentoub:wait_for_child_to_start
Jan 14, 2016
Merged

stephentoub merged 1 commit into
dotnet:masterfrom
stephentoub:wait_for_child_to_start

Conversation

@stephentoub

Copy link
Copy Markdown
Member

We use fork/exec to spawn child processes in System.Diagnostics.Process. This also means we face a flaw in this design that there's a period of time where the child process isn't actually running the target program, which means that the Process we get back may report information related to the parent process' clone rather than to the desired child's information, e.g. the ProcessName may come back as "corerun" rather than the name of the program that will be exec'd.

As a workaround, on systems that have pipe2 (i.e. Linux), this commit augments the fork/exec logic to create a pipe with the O_CLOEXEC flag. That way, the child process will close the pipe when exec transitions the process to the child program, breaking the pipe. The parent process then waits for the pipe to be broken and doesn't return from the start routine until the child has transitioned.

cc: @pallavit
Fixes https://github.com/dotnet/corefx/issues/5378#issuecomment-171704893

@stephentoub

Copy link
Copy Markdown
Member Author

Test Innerloop Windows_NT Debug Build and Test please

(GitHub cloning timeout)

@pallavit

Copy link
Copy Markdown

@stephentoub Thanks for looking at it. I love the solution :). One question though, we do a blocking read on essentially an empty pipe and so rely on it returning due to pipe closing, would we not want to check on the exact error as well?

@stephentoub

Copy link
Copy Markdown
Member Author

I love the solution

😄

would we not want to check on the exact error as well?

And do what if it's not what we expect? At this point we've already forked the child, so we should really be returning a Process object that lets the caller understand the state of the child process, which means we shouldn't error out based on this operation. (That said, I should probably check for EINTR and repeat the read in that case; I'll do that.)

We use fork/exec to spawn child processes in System.Diagnostics.Process.  This also means we face a flaw in this design that there's a period of time where the child process isn't actually running the target program, which means that the Process we get back may report information related to the parent process' clone rather than to the desired child's information, e.g. the ProcessName may come back as "corerun" rather than the name of the program that will be exec'd.

As a workaround, on systems that have pipe2 (i.e. Linux), this commit augments the fork/exec logic to create a pipe with the O_CLOEXEC flag.  That way, the child process will close the pipe when exec transitions the process to the child program, breaking the pipe.  The parent process then waits for the pipe to be broken and doesn't return from the start routine until the child has transitioned.
@stephentoub
stephentoub force-pushed the wait_for_child_to_start branch from 0c1959a to a1f16ad Compare January 14, 2016 19:21
stephentoub added a commit that referenced this pull request Jan 14, 2016
Make Process.Start wait for child process to exec
@stephentoub
stephentoub merged commit 5b3461d into dotnet:master Jan 14, 2016
@stephentoub
stephentoub deleted the wait_for_child_to_start branch January 14, 2016 21:17
@andyleejordan

Copy link
Copy Markdown
Member

You people are fast, thanks for improving this!

andyleejordan added a commit to PowerShell/PowerShell that referenced this pull request Feb 2, 2016
The updated packages fix our Start/Stop process tests, which were
failing intermittently due to the process name appearing incorrect.

See dotnet/corefx#5421 for further details.
@karelz karelz modified the milestone: 1.0.0-rtm Dec 3, 2016
picenka21 pushed a commit to picenka21/runtime that referenced this pull request Feb 18, 2022
…_to_start

Make Process.Start wait for child process to exec

Commit migrated from dotnet/corefx@5b3461d
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Process.Start() sometimes returns wrong ProcessName

5 participants