Fix "Event loop is closed" race in EventLoopThread.force_stop#727
Open
nmingam wants to merge 2 commits into
Open
Fix "Event loop is closed" race in EventLoopThread.force_stop#727nmingam wants to merge 2 commits into
nmingam wants to merge 2 commits into
Conversation
The worker thread can close the loop between the None check and call_soon_threadsafe. Snapshot the loop, also guard on is_closed(), and swallow the RuntimeError if it is closed after the check.
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## dev #727 +/- ##
=======================================
Coverage 99.54% 99.54%
=======================================
Files 61 61
Lines 4181 4183 +2
=======================================
+ Hits 4162 4164 +2
Misses 19 19 ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
The pre-commit ruff hook rejects the try/except-pass form (SIM105).
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.
Independent of any feature work and pre-exists on
dev; surfaced while stabilizing CI.tests/test_thread.py::test_thread_already_stoppedintermittently errors at teardown withRuntimeError: Event loop is closed(observed on the 3.11/3.13 CI legs; passes on 3.12/3.14 and locally).Root cause: the worker thread's
_thread_mainfinallyrunsself.loop.close()and thenself.loop = Noneas two separate statements.
force_stop'sif self.loop is Noneguard can therefore pass while the loop isalready closed, and the following
call_soon_threadsaferaises.Fix: snapshot
self.loopinto a local, also guard onloop.is_closed(), and swallow theRuntimeErrorif the worker closes the loop after the check. The
is_closed()guard is exercised by the existingtest_thread_already_stopped; the race-onlyexceptis marked# pragma: no cover.