Read _reader under the lock in WindowsApiEmitter.queue_events() (#1170)#1175
Open
uttam12331 wants to merge 1 commit into
Open
Read _reader under the lock in WindowsApiEmitter.queue_events() (#1170)#1175uttam12331 wants to merge 1 commit into
uttam12331 wants to merge 1 commit into
Conversation
`on_thread_stop()` reads and clears `self._reader` while holding `self._lock`, but `queue_events()` read `self._reader` without holding the lock, so the two accesses to the shared attribute were unsynchronized. Copy `self._reader` into a local while holding `self._lock` (mirroring `on_thread_stop()`), then use the local reference outside the lock. Closes gorakhargosh#1170
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
Closes #1170.
WindowsApiEmitter.on_thread_stop()reads and clearsself._readerwhile holdingself._lock:but
queue_events()readself._readerwithout holding the same lock, so the two accesses to the shared attribute were unsynchronized.Fix
Copy
self._readerinto a local while holdingself._lock, mirroringon_thread_stop(), then use the local reference outside the lock (thereader.get_events()loop stays outside the lock, as before). This is the approach suggested in the issue.Tests
Added
test_queue_events_reads_reader_under_lock, which spies on the emitter's lock and asserts thatqueue_events()acquires it while reading_reader. It fails onmaster(the lock is never taken inqueue_events()) and passes with this change.ruff check/ruff formatare clean; added a changelog entry.(The test is in the Windows-only
test_observers_winapi.pymodule and was run on Windows.)