Repository navigation
Fixed #34746 -- Used reprlib to limit variable size in debug reports. - #20578
abdullahmujahidali wants to merge 1 commit into
Conversation
There was a problem hiding this comment.
Hello! Thank you for your contribution 💪
As it's your first contribution be sure to check out the patch review checklist.
If you're fixing a ticket from Trac make sure to set the "Has patch" flag and include a link to this PR in the ticket!
If you have any design or process questions then you can ask in the Django forum.
Welcome aboard ⛵️!
📊 Coverage Report for Changed FilesNote: Missing lines are warnings only. Some lines may not be covered by SQLite tests as they are database-specific. For more information about code coverage on pull requests, see the contributing documentation. |
6f20b39 to
5b866ee
Compare
| frames = self.get_traceback_frames() | ||
| # Use reprlib to limit output size during generation, avoiding | ||
| # high memory consumption for large variables. | ||
| r = reprlib.Repr() |
There was a problem hiding this comment.
From Python 3.12+, the attributes can be passed directly to the constructor, rather than being set as attributes manually.
| frames = self.get_traceback_frames() | ||
| # Use reprlib to limit output size during generation, avoiding | ||
| # high memory consumption for large variables. | ||
| r = reprlib.Repr() |
There was a problem hiding this comment.
Thought: It looks like the Repr is safe for reuse. Should one be created at the module level to avoid needing to recreate the instance? reprlib creates one at the module level, so it shouldn't cause many issues.
There was a problem hiding this comment.
Moved it to a module-level DEBUG_REPR next to DEBUG_ENGINE
5b866ee to
1447ba0
Compare
|
Thanks @abdullahmujahidali, could you resolve the conflicts? |
1447ba0 to
ab92535
Compare
Rebased and resolved the conflicts. |
ab92535 to
669a078
Compare
jacobtylerwalls
left a comment
There was a problem hiding this comment.
Thank you for this, I have initial comments.
| # Limit the size of variable representations during generation, avoiding high | ||
| # memory consumption for large variables. | ||
| DEBUG_REPR = reprlib.Repr( | ||
| maxlevel=3, |
There was a problem hiding this comment.
These values seem quite low.
Loading a dictionary from Django's own test suite (hasnulls.geojson), even adding a single additional level makes a huge difference in understanding the actual content:
>>> DEBUG_REPR.repr(dct)
"{'crs': {'properties': {'name': 'urn:ogc:def:crs:OGC:1.3:CRS84'}, 'type': 'name'}, 'features': [{'geometry': {...}, 'properties': {...}, 'type': 'Feature'}, {'geometry': {...}, 'properties': {...}, 'type': 'Feature'}, {'geometry': {...}, 'properties': {...}, 'type': 'Feature'}], 'name': 'has_nulls', 'type': 'FeatureCollection'}"
>>> DEBUG_REPR.maxlevel=4
>>> DEBUG_REPR.repr(dct)
"{'crs': {'properties': {'name': 'urn:ogc:def:crs:OGC:1.3:CRS84'}, 'type': 'name'}, 'features': [{'geometry': {'coordinates': [...], 'type': 'Polygon'}, 'properties': {'boolean': True, 'datetime': '1994-08-14T11:32:14.123+0000', 'integer': 5, 'name': 'Philadelphia', 'num': 1.001, 'time': '11:32:14.123', 'uuid': '1378c26f-cbe6-44b0-929f-eb330d4991f5'}, 'type': 'Feature'}, {'geometry': {'coordinates': [...], 'type': 'Polygon'}, 'properties': {'integer': None, 'num': None, 'uuid': 'fa2ba67c-a135-4338-b924-a9622b5d869f'}, 'type': 'Feature'}, {'geometry': {'coordinates': [...], 'type': 'Polygon'}, 'properties': {'boolean': False, 'datetime': '2018-11-29T03:02:52+0000', 'integer': 8, 'name': 'north', 'num': 0.0, 'time': '03:02:52', 'uuid': '4494c1f3-55ab-4256-b365-12115cb388d5'}, 'type': 'Feature'}], 'name': 'has_nulls', 'type': 'FeatureCollection'}"I wonder if we can loosen some of these limits, and then still use the trim_value() helper to limit to a total length of 4096 like before.
In other words, I don't want to get follow-up issues on this about how we degraded the usefulness of the debug view.
There was a problem hiding this comment.
Raised the limits a bit and added trim_value() back, so your geojson example shows in full now. Didn't go past 4 levels since it gets slow on deeply nested data.
| except Exception as e: | ||
| v = "Error in formatting: %s: %s" % (e.__class__.__name__, e) | ||
| frame_vars.append((k, v)) |
There was a problem hiding this comment.
Having to handle failing repr() is, I think, another reason to keep a total limit of 4096 length, because this passes through in its entirety.
There was a problem hiding this comment.
Yep errors go through trim_value() now too
| self.assertNotIn("A" * 200, html) | ||
|
|
||
| def test_large_collection_values_handling(self): | ||
| "Large collections should not be fully rendered." |
| except Exception: | ||
| exc_type, exc_value, tb = sys.exc_info() | ||
| reporter = ExceptionReporter(None, exc_type, exc_value, tb) | ||
| with mock.patch("reprlib.Repr.repr", side_effect=MemoryError("OOM")): |
There was a problem hiding this comment.
This doesn't seem realistic. reprlib.repr() already has a bare except. Do we need this test and our own bare except, above?
There was a problem hiding this comment.
Right newer Pythons handle this, but on 3.12 reprlib still raises for very large ints, so I kept the except and tested that case instead of the mock.
Happy to drop it if you'd rather
| maxdeque=10, | ||
| maxstring=200, | ||
| maxother=200, | ||
| ) |
There was a problem hiding this comment.
The existing helper used a placeholder similar to <trimmed>, should we use that instead of the default .?
There was a problem hiding this comment.
Done using fillvalue=""
669a078 to
144300a
Compare
Rendering every local variable with pprint built the full string before trimming it to 4096 characters, so a frame holding a large object made the error report slow and very memory hungry. reprlib stops early, so the cost no longer grows with the size of the data.
144300a to
1390b71
Compare
Uncovered lines in changed filesNote: Missing lines are warnings only. Some database-specific lines may not be measured. More information |
This prevents high CPU and memory consumption when a 5XX error is raised with large local variables. The reprlib module limits output size during generation rather than after, avoiding the need to fully expand large objects before truncating.
Trac ticket number
ticket-34746
Branch description
Replaced
pprintwithreprlib.Repr()to limit variable representation size during generation, preventing high memory/CPU usage when displaying large variables in the technical 500 debug page.AI Assistance Disclosure (REQUIRED)
Checklist
mainbranch.