Fix deadlock in Measurements.close() and add test coverage - #282
Open
jaisinha77777 wants to merge 1 commit into
Open
Fix deadlock in Measurements.close() and add test coverage#282jaisinha77777 wants to merge 1 commit into
jaisinha77777 wants to merge 1 commit into
Conversation
Measurements.close() held _channels_lock while iterating over _channels and calling close_channel() for each entry, which itself tried to reacquire the same non-reentrant threading.Lock, deadlocking whenever there was at least one open channel. The loop also mutated _channels while iterating over it. close_channel() additionally raised KeyError on a channel that was never published to, despite its docstring documenting no-op behavior. Adds measurements_test.py and async_measurements_test.py, covering concordia/utils/measurements.py and async_measurements.py, which previously had no test coverage. Part of google-deepmind#205.
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
While adding test coverage for concordia/utils/measurements.py (part of #205, no test file previously existed), I found that Measurements.close() deadlocks:
_channels_lock is a plain threading.Lock (not reentrant), so close() blocks forever the moment it calls close_channel() while already holding the lock. Reproducible any time there's at least one open channel:
The loop also mutates self._channels while iterating over it, a second independent bug (RuntimeError: dictionary changed size during iteration) masked by the deadlock.
Separately, close_channel()'s docstring documents no-op behavior for a channel that doesn't exist ("If the channel doesn't exist yet, it will be created"), but the implementation (del self._channels[channel]) raises KeyError in that case.
There don't appear to be any current callers of close()/close_channel() in the repo, which is presumably why this has gone unnoticed.
Fix
Test plan