Fix deadlock in Measurements.close() and add test coverage - #281
Closed
jaisinha77777 wants to merge 1 commit into
Closed
Fix deadlock in Measurements.close() and add test coverage#281jaisinha77777 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. Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
|
Thanks for your pull request! It looks like this may be your first contribution to a Google open source project. Before we can look at your pull request, you'll need to sign a Contributor License Agreement (CLA). View this failed invocation of the CLA check for more information. For the most up to date status, view the checks section at the bottom of the pull request. |
Author
|
Closing to reopen without attribution footer per updated commit conventions. |
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 thatMeasurements.close()deadlocks:_channels_lockis a plainthreading.Lock(not reentrant), soclose()blocks forever the moment it callsclose_channel()while already holding the lock — reproducible any time there's at least one open channel:The loop also mutates
self._channelswhile iterating over it, which is a second, independent bug (RuntimeError: dictionary changed size during iteration) masked by the deadlock.Separately,
close_channel()'s docstring says calling it on a channel that doesn't exist is a no-op ("If the channel doesn't exist yet, it will be created"), but the implementation (del self._channels[channel]) raisesKeyErrorin 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
close()now just clears the dict directly under the lock, without calling back intoclose_channel().close_channel()usesdict.pop(channel, None)so it's idempotent on a missing channel, matching its docstring.Test plan
concordia/utils/measurements_test.py, including a regression test that runsclose()on a background thread and asserts it completes within a timeout (hangs/fails before this fix, passes after).concordia/utils/async_measurements_test.pycoveringReactiveMeasurements(capture/subscribe/dispose semantics, plus confirmingclose()doesn't deadlock on the subclass either).python -m pytest concordia/utils/ -q→ all passing.python -m pyink --checkon all changed files → clean.🤖 Generated with Claude Code