Skip to content
Closed
Show file tree
Hide file tree
Changes from 3 commits
Commits
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
31 changes: 31 additions & 0 deletions Lib/test/test_json/test_recursion.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
from test import support
from test.test_json import PyTest, CTest
import weakref
import gc



Comment thread
picnixz marked this conversation as resolved.
class JSONTestObject:
Expand Down Expand Up @@ -115,6 +118,34 @@ def default(self, o):
with support.infinite_recursion(1000):
EndlessJSONEncoder(check_circular=False).encode(5j)

@support.skip_if_unlimited_stack_size
@support.skip_emscripten_stack_overflow()
@support.skip_wasi_stack_overflow()
def test_memory_leak_on_recursion_error(self):
"""Test that no memory leak occurs when a RecursionError is raised."""
Comment thread
okiemute04 marked this conversation as resolved.
Outdated
weak_refs = []
class LeakTestObj:
pass

def default(obj):
if isinstance(obj, LeakTestObj):
new_obj = LeakTestObj()
weak_refs.append(weakref.ref(new_obj))
return new_obj
raise TypeError


obj = LeakTestObj()
for _ in range(1000):
obj = [obj]

with self.assertRaises(RecursionError):
self.dumps(obj, default=default)

gc.collect()
Comment thread
okiemute04 marked this conversation as resolved.
Outdated
for i, ref in enumerate(weak_refs):
self.assertIsNone(ref(),
f"Object {i} still alive - memory leak detected!")
Comment thread
okiemute04 marked this conversation as resolved.
Outdated

class TestPyRecursion(TestRecursion, PyTest): pass
class TestCRecursion(TestRecursion, CTest): pass
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Fix a memory leak in the :mod:`json` module when encoding objects with a
Comment thread
okiemute04 marked this conversation as resolved.
Outdated
custom ``default()`` function that raises an exception, when a recursion
error occurs, or when nested encoding fails.
7 changes: 6 additions & 1 deletion Modules/_json.c
Original file line number Diff line number Diff line change
Expand Up @@ -1632,8 +1632,12 @@ encoder_listencode_obj(PyEncoderObject *s, PyUnicodeWriter *writer,
}

if (_Py_EnterRecursiveCall(" while encoding a JSON object")) {
if (ident != NULL) {
PyDict_DelItem(s->markers, ident);
Comment thread
okiemute04 marked this conversation as resolved.
Outdated
Py_XDECREF(ident);
}
Comment on lines +1635 to +1637
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
if (ident != NULL) {
int del_rv = PyDict_DelItem(s->markers, ident);
Py_DECREF(ident);
if (del_rv < 0) {
Py_DECREF(newobj);
return -1;
}
}
if (ident != NULL) {
(void)PyDict_DelItem(s->markers, ident);
}

Actually since we are anyway falling through the return -1 case we can simplify this as follows. Then keep the XDECREF for indent. However explicitly suppress the error code of DelItem.

Py_DECREF(newobj);
Py_XDECREF(ident);

Comment thread
okiemute04 marked this conversation as resolved.
Outdated
return -1;
}
rv = encoder_listencode_obj(s, writer, newobj, indent_level, indent_cache);
Expand All @@ -1642,6 +1646,7 @@ encoder_listencode_obj(PyEncoderObject *s, PyUnicodeWriter *writer,
Py_DECREF(newobj);
if (rv) {
_PyErr_FormatNote("when serializing %T object", obj);
Comment thread
okiemute04 marked this conversation as resolved.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please revert this

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

okay

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Revert cosmetic changes. Nothing was properly reverted.

Py_XDECREF(ident);
return -1;
}
Expand Down
Loading