-
Notifications
You must be signed in to change notification settings - Fork 1.7k
fix(proto-plus): make Marshal thread-safe and handle race conditions #17774
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
+85
−16
Merged
Changes from 2 commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
693e0d0
fix(proto-plus): make Marshal thread-safe and handle race conditions
chalmerlowe 412b482
test(proto-plus): use patch.dict for safe dictionary manipulation in …
chalmerlowe 896ec8e
update name for clarity
chalmerlowe 4854271
Update packages/proto-plus/proto/marshal/marshal.py
chalmerlowe File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,58 @@ | ||
| from unittest.mock import patch | ||
|
|
||
| from proto.marshal.marshal import Marshal | ||
|
|
||
|
|
||
| def test_marshal_identity(): | ||
| m1 = Marshal(name="foo") | ||
| m2 = Marshal(name="foo") | ||
| assert m1 is m2 | ||
|
|
||
|
|
||
| def test_marshal_different_names(): | ||
| m1 = Marshal(name="foo") | ||
| m2 = Marshal(name="bar") | ||
| assert m1 is not m2 | ||
|
|
||
|
|
||
| def test_marshal_new_race_condition(): | ||
| # Test the case where klass is None at line 266, | ||
| # but NOT None at line 271 (another thread created it). | ||
|
|
||
| from unittest.mock import MagicMock | ||
|
|
||
| mock_instances = MagicMock() | ||
|
|
||
| call_count = 0 | ||
|
|
||
| def get_side_effect(name, default=None): | ||
| nonlocal call_count | ||
| call_count += 1 | ||
| if call_count == 1: | ||
| return None # First check returns None | ||
| # Simulate another thread having created it | ||
| return "fake_instance" | ||
|
|
||
| mock_instances.get.side_effect = get_side_effect | ||
|
|
||
| with patch.object(Marshal, "_instances", mock_instances): | ||
| instance = Marshal(name="race_test") | ||
| assert instance == "fake_instance" | ||
|
|
||
|
|
||
| def test_get_rule_uninitialized_instance(): | ||
| class FakeMarshal: | ||
| # No _rules attribute | ||
| pass | ||
|
|
||
| m = Marshal(name="default") | ||
|
|
||
| # Inject FakeMarshal into Marshal._instances safely using patch.dict | ||
| with patch.dict(Marshal._instances, {"fake_uninitialized": FakeMarshal()}): | ||
|
|
||
| class DummyType: | ||
| pass | ||
|
|
||
| # This should not raise AttributeError because of getattr safety | ||
| rule = m.get_rule(DummyType) | ||
| assert rule == m._noop |
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.