Repository navigation
JSONEncoder's tolist() branch hangs and exhausts memory when a mock reaches a response, instead of raising TypeError #10076
Replies: 1 comment
|
There is a useful distinction from an ordinary circular reference here. An unrestricted For the test that triggered this, configure the mocked call to return the actual response-shaped data rather than letting its default mock reach the renderer. from unittest.mock import Mock
client = Mock()
client.fetch.return_value = {"items": [{"id": 1}]}
# Pass client.fetch() through the same response code as the real result.If you want to test rejection of an unsupported object, I checked both the encoder source at This avoids the repeated conversion in these test cases, but does not change DRF's behavior for an unrestricted mock. Any library change would need to preserve genuine |
Uh oh!
There was an error while loading. Please reload this page.
This is a follow-up to #7546, which was closed as an unactionable edge case. I hit this while running some tests, memory exploded and my machine ground to a halt.
The stdlib json encoder raises TypeError here. DRF's default() sees tolist, calls it, gets another Mock, and repeats forever.
Happens here: https://github.com/encode/django-rest-framework/blob/main/rest_framework/utils/encoders.py#L61-L63
All reactions