-
-
Notifications
You must be signed in to change notification settings - Fork 33.7k
gh-116738: Make _json module safe in the free-threading build #119438
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
Conversation
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
Contributor
|
You need to include the file that defines that macro. |
eendebakpt
commented
May 22, 2024
nineteendo
reviewed
May 25, 2024
Contributor
nineteendo
left a comment
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.
Revert newlines
Co-authored-by: Nice Zombies <[email protected]>
eendebakpt
commented
May 25, 2024
eendebakpt
commented
Aug 27, 2025
Co-authored-by: Kumar Aditya <[email protected]>
kumaraditya303
approved these changes
Aug 31, 2025
Contributor
kumaraditya303
left a comment
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.
Thanks!
nineteendo
reviewed
Sep 1, 2025
lkollar
pushed a commit
to lkollar/cpython
that referenced
this pull request
Sep 9, 2025
python#119438) Co-authored-by: Kumar Aditya <[email protected]>
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
(updated description)
Writing JSON files (or encoding to a string) is not thread-safe in the sense that when encoding data to json while another thread is mutating the data, the result is not well-defined (this is true for both the normal and free-threading build). But the free-threading build can crash the interpreter while writing JSON because of the usage of methods like
PySequence_Fast_GET_ITEM. In this PR we make the free-threading build safe by adding locks in three places in the JSON encoder.Reading from a JSON file is safe: objects constructed are only known to the executing thread. Encoding data to JSON needs a bit more care: mutable Python objects such as a list or a dict could be modified by another thread during encoding.
Py_BEGIN_CRITICAL_SECTION_SEQUENCE_FASTto project against mutation the listPyDict_Nextis used there). The non-exact dicts usePyMapping_Itemsto create a list of tuples.PyMapping_Itemsitself is assumed to be thread safe, but the resulting list is not a copy and can be mutated.Update 2025-02-10: refactored to avoid using Py_EXIT_CRITICAL_SECTION_SEQUENCE_FAST
Test script
t=JsonThreadingTest(number_of_json_dumps=102, number_of_threads=8)is a factor 25 faster using free-threading. Nice!