-
-
Notifications
You must be signed in to change notification settings - Fork 32.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
Merged
Changes from all commits
Commits
Show all changes
53 commits
Select commit
Hold shift + click to select a range
da0e917
Make the _json module thread safe
eendebakpt 3797dfa
Update Modules/_json.c
eendebakpt 366654c
handle goto and return statements
eendebakpt 5b72cdf
Apply suggestions from code review
eendebakpt c4c24c3
Update Include/internal/pycore_critical_section.h
eendebakpt 370191b
rename macro
eendebakpt 93c4466
Merge branch 'main' into json_ft
eendebakpt eafd3c1
fix typo
eendebakpt daeec46
Merge branch 'json_ft' of github.com:eendebakpt/cpython into json_ft
eendebakpt d54baf2
fix missing to exit critical section
eendebakpt e5fa305
revert changes to tests
eendebakpt d4ddf5d
📜🤖 Added by blurb_it.
blurb-it[bot] 67d942f
Merge branch 'main' into json_ft
eendebakpt 4ffc1b2
Merge branch 'main' into json_ft
eendebakpt 384ca59
sync with main
eendebakpt 64e20aa
sync with main
eendebakpt e6ce9c9
update news entry
eendebakpt 34885a0
fix normal build
eendebakpt 2fe760b
Merge branch 'main' into json_ft
eendebakpt eebccac
add lock around result of PyMapping_Items
eendebakpt db8947c
add tests
eendebakpt c19ad14
fix argument of Py_END_CRITICAL_SECTION_SEQUENCE_FAST
eendebakpt 8b12e0f
Merge branch 'main' into json_ft
eendebakpt 78d3595
avoid Py_EXIT_CRITICAL_SECTION_SEQUENCE_FAST
eendebakpt 6e8615f
use barriers in test
eendebakpt 39ebc00
typo
eendebakpt 7c5b185
whitespace
eendebakpt adf78c7
Merge branch 'main' into json_ft
eendebakpt acd0ad1
resolve merge conflicts
eendebakpt 41e3dee
Update Misc/NEWS.d/next/Core_and_Builtins/2024-06-04-20-26-21.gh-issu…
eendebakpt 75884cb
cleanup
eendebakpt 0424c58
Merge branch 'json_ft' of github.com:eendebakpt/cpython into json_ft
eendebakpt 9c964f9
Merge branch 'main' into json_ft
eendebakpt 5acc999
Merge branch 'main' into json_ft
eendebakpt 5173373
format
eendebakpt 7d00562
only use items locally in encoder_listencode_dict
eendebakpt 00f4d7f
Merge branch 'main' into json_ft
eendebakpt b95c07f
use strong references in iteration
eendebakpt cedc2c3
Merge branch 'main' into json_ft
eendebakpt 0ca453d
replace Py_IncRef with Py_INCREF
eendebakpt 45544f3
Update Modules/_json.c
eendebakpt dcb088e
Apply suggestion from @kumaraditya303
kumaraditya303 5f8ad07
add header
kumaraditya303 61bc8d1
Apply suggestions from code review
eendebakpt f85b695
review comments
eendebakpt 2c0cd18
use _Py_CRITICAL_SECTION_ASSERT_OBJECT_LOCKED
eendebakpt c133b71
remove testing code
eendebakpt 7609117
Merge branch 'main' into json_ft
eendebakpt 7656a2a
add incref/decref to fast seq iteration
eendebakpt 1920aed
Merge branch 'json_ft' of github.com:eendebakpt/cpython into json_ft
eendebakpt d5d65ab
fix
eendebakpt 4a16e18
fix
eendebakpt 00139f3
Update Modules/_json.c
kumaraditya303 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 |
---|---|---|
@@ -0,0 +1,79 @@ | ||
from threading import Barrier, Thread | ||
from test.test_json import CTest | ||
from test.support import threading_helper | ||
|
||
|
||
def encode_json_helper( | ||
json, worker, data, number_of_threads=12, number_of_json_encodings=100 | ||
): | ||
worker_threads = [] | ||
barrier = Barrier(number_of_threads) | ||
for index in range(number_of_threads): | ||
worker_threads.append( | ||
Thread(target=worker, args=[barrier, data, index]) | ||
) | ||
for t in worker_threads: | ||
t.start() | ||
for ii in range(number_of_json_encodings): | ||
json.dumps(data) | ||
data.clear() | ||
for t in worker_threads: | ||
t.join() | ||
|
||
|
||
class MyMapping(dict): | ||
def __init__(self): | ||
self.mapping = [] | ||
|
||
def items(self): | ||
return self.mapping | ||
|
||
|
||
@threading_helper.reap_threads | ||
@threading_helper.requires_working_threading() | ||
class TestJsonEncoding(CTest): | ||
# Test encoding json with concurrent threads modifying the data cannot | ||
# corrupt the interpreter | ||
|
||
def test_json_mutating_list(self): | ||
def worker(barrier, data, index): | ||
barrier.wait() | ||
while data: | ||
for d in data: | ||
if len(d) > 5: | ||
d.clear() | ||
else: | ||
d.append(index) | ||
|
||
data = [[], []] | ||
encode_json_helper(self.json, worker, data) | ||
|
||
def test_json_mutating_exact_dict(self): | ||
def worker(barrier, data, index): | ||
barrier.wait() | ||
while data: | ||
for d in data: | ||
if len(d) > 5: | ||
try: | ||
key = list(d)[0] | ||
d.pop() | ||
except (KeyError, IndexError): | ||
pass | ||
else: | ||
d[index] = index | ||
|
||
data = [{}, {}] | ||
encode_json_helper(self.json, worker, data) | ||
|
||
def test_json_mutating_mapping(self): | ||
def worker(barrier, data, index): | ||
barrier.wait() | ||
while data: | ||
for d in data: | ||
if len(d.mapping) > 3: | ||
d.mapping.clear() | ||
else: | ||
d.mapping.append((index, index)) | ||
|
||
data = [MyMapping(), MyMapping()] | ||
encode_json_helper(self.json, worker, data) |
1 change: 1 addition & 0 deletions
1
Misc/NEWS.d/next/Core_and_Builtins/2024-06-04-20-26-21.gh-issue-116738.q_hPYq.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 @@ | ||
Make the module :mod:`json` safe to use under the free-threading build. |
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.
Shouldn't this be
d.pop(key)
?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.
Yes. See #138339