Skip to content

Process.Unix: while reaping all processes, handle encountering direct children. #79817

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
Dec 23, 2022
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
Original file line number Diff line number Diff line change
Expand Up @@ -551,6 +551,24 @@ private Task WaitForExitAsync(CancellationToken cancellationToken = default)
}, cancellationToken);
}

private void ChildReaped(int exitCode, bool configureConsole)
{
lock (_gate)
{
Debug.Assert(!_exited);

_exitCode = exitCode;

if (_usesTerminal)
{
// Update terminal settings before calling SetExited.
Process.ConfigureTerminalForChildProcesses(-1, configureConsole);
}

SetExited();
}
}

private bool TryReapChild(bool configureConsole)
{
lock (_gate)
Expand All @@ -566,16 +584,7 @@ private bool TryReapChild(bool configureConsole)

if (waitResult == _processId)
{
_exitCode = exitCode;

if (_usesTerminal)
{
// Update terminal settings before calling SetExited.
Process.ConfigureTerminalForChildProcesses(-1, configureConsole);
}

SetExited();

ChildReaped(exitCode, configureConsole);
return true;
}
else if (waitResult == 0)
Expand Down Expand Up @@ -636,7 +645,7 @@ internal static void CheckChildren(bool reapAll, bool configureConsole)
}
} while (pid > 0);

if (checkAll)
if (checkAll && !reapAll)
{
// We track things to unref so we don't invalidate our iterator by changing s_childProcessWaitStates.
ProcessWaitState? firstToRemove = null;
Expand Down Expand Up @@ -675,8 +684,20 @@ internal static void CheckChildren(bool reapAll, bool configureConsole)
{
do
{
pid = Interop.Sys.WaitPidExitedNoHang(-1, out _);
} while (pid > 0);
int exitCode;
pid = Interop.Sys.WaitPidExitedNoHang(-1, out exitCode);
if (pid <= 0)
{
break;
}

// Check if the process is a child that has just terminated.
if (s_childProcessWaitStates.TryGetValue(pid, out ProcessWaitState? pws))
{
pws.ChildReaped(exitCode, configureConsole);
pws.ReleaseRef();
}
} while (true);
}
}
}
Expand Down