GeezerVison is showing github.com — gh-116738: Add critical section to dbm/gdbm context manager by yoney · Pull Request #140391 · python/cpython
Conversation
yoney
commented
Oct 20, 2025
•
edited
Loading
Uh oh!
There was an error while loading. Please reload this page.
Add Py_{BEGIN,END}_CRITICAL_SECTION() to the context managers in the dbm and gdbm modules for the free threading build. Other methods are already covered.
This could be a problem in a rare corner case if a thread outlives the context manager's scope. I created a local test, and TSAN identifies the issue.
def test_context_manager(self):
def worker(db):
db.close()
with os_helper.temp_dir() as tmpdirname:
with gdbm.open(f"{tmpdirname}/gdbm_filename", "c") as db:
t1 = threading.Thread(target=worker, args=(db,))
t1.start()
t1.join()
WARNING: ThreadSanitizer: data race (pid=1672837)
Read of size 8 at 0x7f9fdb330e48 by thread T1:
#0 _gdbm_gdbm_close_impl /home/alper/workspace/tsan/./Modules/_gdbmmodule.c:413:15 (_gdbm.cpython-315td-x86_64-linux-gnu.so+0x2ce1) (BuildId: 42fff9900c3973fef01c9a778b42049ab0110b9f)
#1 _gdbm_gdbm_close /home/alper/workspace/tsan/./Modules/clinic/_gdbmmodule.c.h:100:20 (_gdbm.cpython-315td-x86_64-linux-gnu.so+0x2ce1)
#2 method_vectorcall_NOARGS /home/alper/workspace/tsan/Objects/descrobject.c:448:24 (python+0x5c662b) (BuildId: 89128bd333556519224b9af239856b3fcb80f8ab)
...
Previous write of size 8 at 0x7f9fdb330e48 by main thread:
#0 _gdbm_gdbm_close_impl /home/alper/workspace/tsan/./Modules/_gdbmmodule.c:416:18 (_gdbm.cpython-315td-x86_64-linux-gnu.so+0x4944) (BuildId: 42fff9900c3973fef01c9a778b42049ab0110b9f)
#1 gdbm__exit__ /home/alper/workspace/tsan/./Modules/_gdbmmodule.c:693:12 (_gdbm.cpython-315td-x86_64-linux-gnu.so+0x4944)
#2 method_vectorcall_VARARGS /home/alper/workspace/tsan/Objects/descrobject.c:325:24 (python+0x5c5ef9) (BuildId: 89128bd333556519224b9af239856b3fcb80f8ab)
...
SUMMARY: ThreadSanitizer: data race /home/alper/workspace/tsan/./Modules/_gdbmmodule.c:413:15 in _gdbm_gdbm_close_impl
==================
cc: @mpage @colesbury
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
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.
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
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.
LGTM as well.
FYI, you can also use Argument Clinic for __enter__ and __exit__ functions now if you prefer Argument Clinic boilerplate to Py_BEGIN_CRITICAL_SECTION boilerplate.
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
yoney reacted with thumbs up emoji
Uh oh!
There was an error while loading. Please reload this page.
|
Thanks @yoney for the PR, and @colesbury for merging it 🌮🎉.. I'm working now to backport this PR to: 3.14.
🐍🍒⛏🤖
|
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
Assignees
No one assigned
Milestone
No milestone
Development
Successfully merging this pull request may close these issues.
Uh oh!
There was an error while loading. Please reload this page.
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.
|