gh-149123: Avoid InvalidStateError in Windows Proactor polling#152852
Open
zainnadeem786 wants to merge 1 commit into
Open
gh-149123: Avoid InvalidStateError in Windows Proactor polling#152852zainnadeem786 wants to merge 1 commit into
zainnadeem786 wants to merge 1 commit into
Conversation
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Summary
This PR addresses the race condition described in gh-149123.
IocpProactor._poll()checks whether an_OverlappedFutureis pending before invoking its completion callback. However, the callback may eventually callOverlapped.getresult(), which releases the GIL while waiting for the underlying Windows I/O completion.During that window, another thread may complete or cancel the same future. When
_poll()resumes, the subsequent call toFuture.set_exception()orFuture.set_result()can therefore raiseasyncio.InvalidStateError, even though the future was pending before the callback started.This change re-checks the future state immediately before setting the result or exception. If the future became done while the callback was executing,
_poll()simply skips result delivery, matching the existing behavior for futures that are already done before callback execution.No public API or observable behavior is otherwise changed.
Tests
Adds deterministic regression tests covering both execution paths:
set_exception()set_result()Both tests verify that
_poll()no longer raisesasyncio.InvalidStateErrorwhen the future transitions to a completed state during callback execution.Validation
Validated using a Windows debug build of current
main.Targeted regression tests:
Both regression tests pass.
Additional validation:
passed successfully.
A broader local
test_asynciorun encountered unrelated Windows environment permission failures involving named pipes and temporary-directory cleanup, but the new regression tests completed successfully.Changes
Future.done()after the completion callback returns or raises.set_exception()orset_result()if the future became done during callback execution.Fixes gh-149123.