This repository was archived by the owner on Jan 23, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4.8k
Handle recycled child PIDs #27763
Merged
Merged
Handle recycled child PIDs #27763
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
0d4a49c
ProcessWaitState: support recycled child pids
tmds 4fff0f4
Ensure ProcessWaitHandle uses same ProcessWaitState as Process
tmds af7513a
Fix race between Close and CompletionCallback
tmds 861eae0
Revert GetWaitState
tmds b41ef1f
Add test
tmds 0de56c5
Handle race between Close and CompletionCallback
tmds File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -114,6 +114,9 @@ internal static ProcessWaitState AddRef(int processId, bool isNewChild) | |
| ProcessWaitState pws; | ||
| if (isNewChild) | ||
| { | ||
| // When the PID is recycled for a new child, we remove the old child. | ||
| s_childProcessWaitStates.Remove(processId); | ||
|
|
||
| pws = new ProcessWaitState(processId, isChild: true); | ||
| s_childProcessWaitStates.Add(processId, pws); | ||
| pws._outstandingRefCount++; // For Holder | ||
|
|
@@ -142,22 +145,25 @@ internal static ProcessWaitState AddRef(int processId, bool isNewChild) | |
| /// Decrements the ref count on the wait state object, and if it's the last one, | ||
| /// removes it from the table. | ||
| /// </summary> | ||
| internal bool ReleaseRef() | ||
| internal void ReleaseRef() | ||
| { | ||
| ProcessWaitState pws; | ||
| Dictionary<int, ProcessWaitState> waitStates = _isChild ? s_childProcessWaitStates : s_processWaitStates; | ||
| bool removed = false; | ||
| lock (waitStates) | ||
| { | ||
| bool foundState = waitStates.TryGetValue(_processId, out pws); | ||
| Debug.Assert(foundState); | ||
| if (foundState) | ||
| { | ||
| --pws._outstandingRefCount; | ||
| if (pws._outstandingRefCount == 0) | ||
| --_outstandingRefCount; | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is the large documentation comment at the beginning of the file up-to-date?
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I have updated this doc as part of this PR: #28404 |
||
| if (_outstandingRefCount == 0) | ||
| { | ||
| waitStates.Remove(_processId); | ||
| removed = true; | ||
| // The dictionary may contain a different ProcessWaitState if the pid was recycled. | ||
| if (pws == this) | ||
| { | ||
| waitStates.Remove(_processId); | ||
| } | ||
| pws = this; | ||
| } | ||
| else | ||
| { | ||
|
|
@@ -166,7 +172,6 @@ internal bool ReleaseRef() | |
| } | ||
| } | ||
| pws?.Dispose(); | ||
| return removed; | ||
| } | ||
|
|
||
| /// <summary> | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In what situation will they not match?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is the test that is failing.
The test does a WaitForExit. This emits the exited event and schedules the CompletionCallback on the ThreadPool.
Then the user calls Close causing _raisedOnExited to be set to false. When the CompletionCallback runs, it emits the exited event a second time.
In case of the test, the _waitHandle is null (set in Close). In general, the Process instance may be re-used at this point causing _waitHandle to be a different non-null value.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This code is shared between Windows and Unix. Will this ever not match on Windows?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
On Windows, the same thing can happen. So the fix is also needed.