Repository navigation
gh-152254: Fix a crash in PyObject_ClearManagedDict under memory pressure
#152255
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
Open
sobolevn
wants to merge
3
commits into
python:main
Choose a base branch
from
sobolevn:issue-152254
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+85
−0
Open
Changes from all commits
Commits
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -354,6 +354,80 @@ def test_pyobject_dump(self): | |
| output = self.pyobject_dump(NULL) | ||
| self.assertRegex(output, r'<object at .* is freed>') | ||
|
|
||
| def test_object_clear_managed_dict(self): | ||
| # PyObject_ClearManagedDict | ||
| clear = _testcapi.pyobject_clear_managed_dict | ||
| ManagedDictType = _testcapi.ManagedDictType | ||
|
|
||
| obj = ManagedDictType() | ||
| obj.value = 'first' | ||
| # asserting `__dict__` is a must to materialize the value before clean: | ||
| self.assertEqual(obj.__dict__, {'value': 'first'}) | ||
| self.assertIs(type(obj.__dict__), dict) | ||
|
vstinner marked this conversation as resolved.
Resolved
|
||
| clear(obj) | ||
| self.assertEqual(obj.__dict__, {}) | ||
| self.assertIs(type(obj.__dict__), dict) | ||
|
|
||
| # big object: | ||
| obj = ManagedDictType() | ||
| existing_dict = {f'attr_{x}': x for x in range(100)} | ||
| obj.__dict__ = existing_dict | ||
| self.assertIs(obj.__dict__, existing_dict) | ||
| clear(obj) | ||
| self.assertEqual(obj.__dict__, {}) | ||
| self.assertIs(type(obj.__dict__), dict) | ||
|
|
||
| # empty dict: | ||
| obj = ManagedDictType() | ||
| self.assertEqual(obj.__dict__, {}) | ||
| self.assertIs(type(obj.__dict__), dict) | ||
| clear(obj) | ||
| self.assertEqual(obj.__dict__, {}) | ||
| self.assertIs(type(obj.__dict__), dict) | ||
|
|
||
| # double clear: | ||
| obj = ManagedDictType() | ||
| obj.value = 'first' | ||
| self.assertEqual(obj.__dict__, {'value': 'first'}) | ||
| clear(obj) | ||
| clear(obj) | ||
| self.assertEqual(obj.__dict__, {}) | ||
| self.assertIs(type(obj.__dict__), dict) | ||
|
|
||
| # unmaterialized: | ||
| obj = ManagedDictType() | ||
| obj.value = 'first' | ||
| clear(obj) | ||
| self.assertEqual(obj.__dict__, {}) | ||
| self.assertIs(type(obj.__dict__), dict) | ||
|
|
||
| # CRASHES: clear(NULL) | ||
|
|
||
| @threading_helper.requires_working_threading() | ||
| def test_object_clear_managed_dict_ft_memory_error(self): | ||
| # PyObject_ClearManagedDict, gh-152254 | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can you write a short sentence to describe the purpose of the test? I understand that you call PyObject_ClearManagedDict() from a different thread so _Py_IsOwnedByCurrentThread() is false. |
||
| clear = _testcapi.pyobject_clear_managed_dict | ||
| ManagedDictType = _testcapi.ManagedDictType | ||
|
|
||
| obj = ManagedDictType() | ||
| obj.value = 'first' | ||
| self.assertEqual(len(obj.__dict__), 1) | ||
|
|
||
| def worker(): | ||
| # This needs to be in a separate thread for the crash to happen: | ||
| with support.catch_unraisable_exception() as ex: | ||
| # `clear` used to crash: | ||
| _testcapi.call_with_nomemory(0, 1, clear, obj) | ||
| self.assertIsNotNone(ex.unraisable) | ||
| self.assertIs(ex.unraisable.exc_type, MemoryError) | ||
| self.assertIn('managed dict', ex.unraisable.err_msg) | ||
| self.assertEqual(obj.__dict__, {}) | ||
| self.assertIs(type(obj.__dict__), dict) | ||
|
|
||
| threading_helper.run_concurrently([worker]) | ||
| self.assertEqual(obj.__dict__, {}) | ||
| self.assertIs(type(obj.__dict__), dict) | ||
|
|
||
|
|
||
| class RefTracerTest(unittest.TestCase): | ||
| @support.requires_resource('cpu') | ||
|
|
||
2 changes: 2 additions & 0 deletions
2
Misc/NEWS.d/next/Core_and_Builtins/2026-06-26-11-34-50.gh-issue-152254.yK6TGp.rst
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,2 @@ | ||
| Fix a crash during the clean up of managed dict objects under a memory | ||
| pressure in :c:func:`PyObject_ClearManagedDict`. |
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
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nitpick: I would prefer to call it
clear_managed_dict, sinceclear()sounds like a different function (like PyDict_Clear()). Same remark in the second test.