Skip to content

Commit 7656a2a

Browse files
committed
add incref/decref to fast seq iteration
1 parent c133b71 commit 7656a2a

File tree

1 file changed

+11
-1
lines changed

1 file changed

+11
-1
lines changed

Modules/_json.c

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1754,7 +1754,10 @@ _encoder_iterate_mapping_lock_held(PyEncoderObject *s, PyUnicodeWriter *writer,
17541754
PyObject *key, *value;
17551755
for (Py_ssize_t i = 0; i < PyList_GET_SIZE(items); i++) {
17561756
PyObject *item = PyList_GET_ITEM(items, i);
1757-
1757+
#ifdef Py_GIL_DISABLED
1758+
// gh-119438: in the free-threading build the critical section on items can get suspended
1759+
Py_DECREF(item);
1760+
#endif
17581761
if (!PyTuple_Check(item) || PyTuple_GET_SIZE(item) != 2) {
17591762
PyErr_SetString(PyExc_ValueError, "items must return 2-tuples");
17601763
return -1;
@@ -1765,8 +1768,14 @@ _encoder_iterate_mapping_lock_held(PyEncoderObject *s, PyUnicodeWriter *writer,
17651768
if (encoder_encode_key_value(s, writer, first, dct, key, value,
17661769
indent_level, indent_cache,
17671770
separator) < 0) {
1771+
#ifdef Py_GIL_DISABLED
1772+
Py_DECREF(item);
1773+
#endif
17681774
return -1;
17691775
}
1776+
#ifdef Py_GIL_DISABLED
1777+
Py_DECREF(item);
1778+
#endif
17701779
}
17711780

17721781
return 0;
@@ -1900,6 +1909,7 @@ _encoder_iterate_fast_seq_lock_held(PyEncoderObject *s, PyUnicodeWriter *writer,
19001909
PyObject *seq, PyObject *s_fast,
19011910
Py_ssize_t indent_level, PyObject *indent_cache, PyObject *separator)
19021911
{
1912+
_Py_CRITICAL_SECTION_ASSERT_OBJECT_LOCKED(s_fast);
19031913
for (Py_ssize_t i = 0; i < PySequence_Fast_GET_SIZE(s_fast); i++) {
19041914
PyObject *obj = PySequence_Fast_GET_ITEM(s_fast, i);
19051915
#ifdef Py_GIL_DISABLED

0 commit comments

Comments
 (0)