Skip to content

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

Open
wants to merge 27 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 19 commits
Commits
Show all changes
27 commits
Select commit Hold shift + click to select a range
da0e917
Make the _json module thread safe
eendebakpt May 22, 2024
3797dfa
Update Modules/_json.c
eendebakpt May 22, 2024
366654c
handle goto and return statements
eendebakpt May 24, 2024
5b72cdf
Apply suggestions from code review
eendebakpt May 25, 2024
c4c24c3
Update Include/internal/pycore_critical_section.h
eendebakpt May 25, 2024
370191b
rename macro
eendebakpt May 31, 2024
93c4466
Merge branch 'main' into json_ft
eendebakpt May 31, 2024
eafd3c1
fix typo
eendebakpt May 31, 2024
daeec46
Merge branch 'json_ft' of github.com:eendebakpt/cpython into json_ft
eendebakpt May 31, 2024
d54baf2
fix missing to exit critical section
eendebakpt Jun 4, 2024
e5fa305
revert changes to tests
eendebakpt Jun 4, 2024
d4ddf5d
📜🤖 Added by blurb_it.
blurb-it[bot] Jun 4, 2024
67d942f
Merge branch 'main' into json_ft
eendebakpt Jun 4, 2024
4ffc1b2
Merge branch 'main' into json_ft
eendebakpt Aug 14, 2024
384ca59
sync with main
eendebakpt Aug 14, 2024
64e20aa
sync with main
eendebakpt Aug 14, 2024
e6ce9c9
update news entry
eendebakpt Aug 14, 2024
34885a0
fix normal build
eendebakpt Aug 14, 2024
2fe760b
Merge branch 'main' into json_ft
eendebakpt Aug 14, 2024
eebccac
add lock around result of PyMapping_Items
eendebakpt Aug 15, 2024
db8947c
add tests
eendebakpt Aug 15, 2024
c19ad14
fix argument of Py_END_CRITICAL_SECTION_SEQUENCE_FAST
eendebakpt Aug 15, 2024
8b12e0f
Merge branch 'main' into json_ft
eendebakpt Feb 10, 2025
78d3595
avoid Py_EXIT_CRITICAL_SECTION_SEQUENCE_FAST
eendebakpt Feb 10, 2025
6e8615f
use barriers in test
eendebakpt Feb 10, 2025
39ebc00
typo
eendebakpt Feb 10, 2025
7c5b185
whitespace
eendebakpt Feb 10, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions Include/cpython/critical_section.h
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ PyCriticalSection2_End(PyCriticalSection2 *c);
#ifndef Py_GIL_DISABLED
# define Py_BEGIN_CRITICAL_SECTION(op) \
{
# define Py_EXIT_CRITICAL_SECTION()
# define Py_END_CRITICAL_SECTION() \
}
# define Py_BEGIN_CRITICAL_SECTION2(a, b) \
Expand Down Expand Up @@ -118,6 +119,10 @@ struct PyCriticalSection2 {
PyCriticalSection _py_cs; \
PyCriticalSection_Begin(&_py_cs, _PyObject_CAST(op))

# define Py_EXIT_CRITICAL_SECTION() \
_PyCriticalSection_End(&_py_cs);


# define Py_END_CRITICAL_SECTION() \
PyCriticalSection_End(&_py_cs); \
}
Expand Down
6 changes: 6 additions & 0 deletions Include/internal/pycore_critical_section.h
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,11 @@ extern "C" {
} \
}

# define Py_EXIT_CRITICAL_SECTION_SEQUENCE_FAST() \
if (_should_lock_cs) { \
_PyCriticalSection_End(&_cs); \
}

// Asserts that the mutex is locked. The mutex must be held by the
// top-most critical section otherwise there's the possibility
// that the mutex would be swalled out in some code paths.
Expand Down Expand Up @@ -78,6 +83,7 @@ extern "C" {
# define Py_BEGIN_CRITICAL_SECTION_MUT(mut) {
# define Py_BEGIN_CRITICAL_SECTION2_MUT(m1, m2) {
# define Py_BEGIN_CRITICAL_SECTION_SEQUENCE_FAST(original) {
# define Py_EXIT_CRITICAL_SECTION_SEQUENCE_FAST()
# define Py_END_CRITICAL_SECTION_SEQUENCE_FAST() }
# define _Py_CRITICAL_SECTION_ASSERT_MUTEX_LOCKED(mutex)
# define _Py_CRITICAL_SECTION_ASSERT_OBJECT_LOCKED(op)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Make the module :mod:`json` safe to use under the free-theading build.
13 changes: 12 additions & 1 deletion Modules/_json.c
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

#include "Python.h"
#include "pycore_ceval.h" // _Py_EnterRecursiveCall()
#include "pycore_critical_section.h" // Py_BEGIN_CRITICAL_SECTION()
#include "pycore_runtime.h" // _PyRuntime
#include "pycore_pyerrors.h" // _PyErr_FormatNote

Expand Down Expand Up @@ -1618,12 +1619,16 @@ encoder_listencode_dict(PyEncoderObject *s, _PyUnicodeWriter *writer,

} else {
Py_ssize_t pos = 0;
Py_BEGIN_CRITICAL_SECTION(dct);
while (PyDict_Next(dct, &pos, &key, &value)) {
if (encoder_encode_key_value(s, writer, &first, dct, key, value,
new_newline_indent,
current_item_separator) < 0)
current_item_separator) < 0) {
Py_EXIT_CRITICAL_SECTION();
goto bail;
}
}
Py_END_CRITICAL_SECTION();
}

if (ident != NULL) {
Expand Down Expand Up @@ -1666,8 +1671,12 @@ encoder_listencode_list(PyEncoderObject *s, _PyUnicodeWriter *writer,
s_fast = PySequence_Fast(seq, "_iterencode_list needs a sequence");
if (s_fast == NULL)
return -1;

Py_BEGIN_CRITICAL_SECTION_SEQUENCE_FAST(seq);

if (PySequence_Fast_GET_SIZE(s_fast) == 0) {
Py_DECREF(s_fast);
Py_EXIT_CRITICAL_SECTION_SEQUENCE_FAST();
return _PyUnicodeWriter_WriteASCIIString(writer, "[]", 2);
}

Expand Down Expand Up @@ -1735,10 +1744,12 @@ encoder_listencode_list(PyEncoderObject *s, _PyUnicodeWriter *writer,
if (_PyUnicodeWriter_WriteChar(writer, ']'))
goto bail;
Py_DECREF(s_fast);
Py_EXIT_CRITICAL_SECTION_SEQUENCE_FAST();
return 0;

bail:
Py_XDECREF(ident);
Py_END_CRITICAL_SECTION_SEQUENCE_FAST();
Py_DECREF(s_fast);
Py_XDECREF(separator_indent);
Py_XDECREF(new_newline_indent);
Expand Down
Loading