Re: [Django] #34746: High CPU/memory consumption when a 5XX is raised with large local variables

27 views
Skip to first unread message

Django

unread,
Jul 28, 2023, 11:28:49 AM7/28/23
to django-...@googlegroups.com
#34746: High CPU/memory consumption when a 5XX is raised with large local variables
--------------------------------------+------------------------------------
Reporter: Rémi Dupré | Owner: (none)
Type: Cleanup/optimization | Status: new
Component: Error reporting | Version: 4.2
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 1 | UI/UX: 0
--------------------------------------+------------------------------------
Changes (by Natalia Bidart):

* type: Bug => Cleanup/optimization
* easy: 0 => 1
* stage: Unreviewed => Accepted


Comment:

I have managed to reproduce the issue, thank you for the example. Indeed
if `v = pprint(v)` is replaced by something that does not render the whole
variable, the memory and CPU usage do not spike.

Accepting so we can evaluate whether an alternative could be used instead
of `pprint` that would account for the size limit before printing.

--
Ticket URL: <https://code.djangoproject.com/ticket/34746#comment:2>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

Django

unread,
Jul 28, 2023, 11:47:20 AM7/28/23
to django-...@googlegroups.com
#34746: High CPU/memory consumption when a 5XX is raised with large local variables
--------------------------------------+------------------------------------
Reporter: Rémi Dupré | Owner: (none)
Type: Cleanup/optimization | Status: new
Component: Error reporting | Version: 4.2
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 1 | UI/UX: 0
--------------------------------------+------------------------------------

Comment (by Natalia Bidart):

After some experimentation following the suggested link to reprlib,
perhaps something like this could be a good alternative? (it needs better
code formatting, more reprlib limits sets and tests, of course)

{{{
#!diff
--- a/django/views/debug.py
+++ b/django/views/debug.py
@@ -347,12 +347,17 @@ class ExceptionReporter:
self.template_does_not_exist = True
self.postmortem = self.exc_value.chain or [self.exc_value]

+ import reprlib
+ repr_instance = reprlib.Repr()
+ repr_instance.maxstring = 5000
+ repr_instance.maxlist = 1000
+
frames = self.get_traceback_frames()
for i, frame in enumerate(frames):
if "vars" in frame:
frame_vars = []
for k, v in frame["vars"]:
- v = pprint(v)
+ v = repr_instance.repr(v)
# Trim large blobs of data
if len(v) > 4096:
v = "%s… <trimmed %d bytes string>" % (v[0:4096],
len(v))
}}}

--
Ticket URL: <https://code.djangoproject.com/ticket/34746#comment:3>

Django

unread,
Jul 28, 2023, 1:22:50 PM7/28/23
to django-...@googlegroups.com
#34746: High CPU/memory consumption when a 5XX is raised with large local variables
-------------------------------------+-------------------------------------
Reporter: Rémi Dupré | Owner: Vishesh
Type: | Garg
Cleanup/optimization | Status: assigned

Component: Error reporting | Version: 4.2
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 1 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Vishesh Garg):

* owner: (none) => Vishesh Garg
* status: new => assigned


--
Ticket URL: <https://code.djangoproject.com/ticket/34746#comment:4>

Django

unread,
Jul 28, 2023, 1:29:22 PM7/28/23
to django-...@googlegroups.com
#34746: High CPU/memory consumption when a 5XX is raised with large local variables
--------------------------------------+------------------------------------
Reporter: Rémi Dupré | Owner: (none)
Type: Cleanup/optimization | Status: assigned

Component: Error reporting | Version: 4.2
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 1 | UI/UX: 0
--------------------------------------+------------------------------------
Changes (by Vishesh Garg):

* owner: Vishesh Garg => (none)


--
Ticket URL: <https://code.djangoproject.com/ticket/34746#comment:5>

Django

unread,
Jul 28, 2023, 1:33:18 PM7/28/23
to django-...@googlegroups.com
#34746: High CPU/memory consumption when a 5XX is raised with large local variables
-------------------------------------+-------------------------------------
Reporter: Rémi Dupré | Owner: Vishesh
Type: | Garg
Cleanup/optimization | Status: assigned
Component: Error reporting | Version: 4.2
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 1 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Vishesh Garg):

* owner: (none) => Vishesh Garg


--
Ticket URL: <https://code.djangoproject.com/ticket/34746#comment:6>

Django

unread,
Jul 31, 2023, 10:02:17 AM7/31/23
to django-...@googlegroups.com
#34746: High CPU/memory consumption when a 5XX is raised with large local variables
-------------------------------------+-------------------------------------
Reporter: Rémi Dupré | Owner: Vishesh
Type: | Garg
Cleanup/optimization | Status: assigned
Component: Error reporting | Version: 4.2
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 1 | Needs documentation: 0
Needs tests: 1 | Patch needs improvement: 1

Easy pickings: 1 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Natalia Bidart):

* needs_better_patch: 0 => 1
* has_patch: 0 => 1
* needs_tests: 0 => 1


Comment:

[https://github.com/django/django/pull/17127 PR]

--
Ticket URL: <https://code.djangoproject.com/ticket/34746#comment:7>

Django

unread,
Aug 8, 2023, 1:24:13 AM8/8/23
to django-...@googlegroups.com
#34746: High CPU/memory consumption when a 5XX is raised with large local variables
-------------------------------------+-------------------------------------
Reporter: Rémi Dupré | Owner: Vishesh
Type: | Garg
Cleanup/optimization | Status: assigned
Component: Error reporting | Version: 4.2
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 1 | Needs documentation: 0
Needs tests: 1 | Patch needs improvement: 1
Easy pickings: 1 | UI/UX: 0
-------------------------------------+-------------------------------------

Comment (by Adam Johnson):

I think using reprlib is acceptable, this is exactly the kind of situation
it’s designed for. It will be a shame to lose the pretty-printing aspect,
but perhaps reprlib/pprint will grow appropriate functionality in the
future.

--
Ticket URL: <https://code.djangoproject.com/ticket/34746#comment:8>

Django

unread,
Dec 2, 2023, 1:28:12 PM12/2/23
to django-...@googlegroups.com
#34746: High CPU/memory consumption when a 5XX is raised with large local variables
-------------------------------------+-------------------------------------
Reporter: Rémi Dupré | Owner: Yash
Type: | Kumar Verma

Cleanup/optimization | Status: assigned
Component: Error reporting | Version: 4.2
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 1 | Needs documentation: 0
Needs tests: 1 | Patch needs improvement: 1
Easy pickings: 1 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Yash Kumar Verma):

* owner: Vishesh Garg => Yash Kumar Verma


Comment:

Hello folks, I saw the two pull requests that were attached in the ticket
are stale, and the original author have both closed. I'm interesting in
picking this as my first ticket, can I proceed?

--
Ticket URL: <https://code.djangoproject.com/ticket/34746#comment:9>

Django

unread,
Dec 3, 2023, 4:17:36 AM12/3/23
to django-...@googlegroups.com
#34746: High CPU/memory consumption when a 5XX is raised with large local variables
-------------------------------------+-------------------------------------
Reporter: Rémi Dupré | Owner: Yash
Type: | Kumar Verma
Cleanup/optimization | Status: assigned
Component: Error reporting | Version: 4.2
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 1 | Needs documentation: 0
Needs tests: 1 | Patch needs improvement: 1
Easy pickings: 1 | UI/UX: 0
-------------------------------------+-------------------------------------

Comment (by Yash Kumar Verma):

I was:
* able to verify the following
* when exception is raised after declaring a large variable, memory
usage shoots up.
* when exception is not raised after declaring a large variable,
memory remains in live with normal operations.

'''Setup I used to reproduce the error and suggested solution'''
* added a middleware which logs the change in memory after each request.
* created two views, one which throws error and one that does not.
* observed the memory usage in both the cases.

'''Current'''

[[Image(https://github.com/YashKumarVerma/django-
pocs/blob/master/poc01/poc01/2023-12-03-14-18-23.png?raw=true)]]

'''After using reprlib'''

[[Image(https://github.com/YashKumarVerma/django-
pocs/blob/master/poc01/poc01/2023-12-03-14-35-23.png?raw=true)]]

--
Ticket URL: <https://code.djangoproject.com/ticket/34746#comment:10>

Django

unread,
Feb 2, 2024, 9:51:04 AM2/2/24
to django-...@googlegroups.com
#34746: High CPU/memory consumption when a 5XX is raised with large local variables
-------------------------------------+-------------------------------------
Reporter: Rémi Dupré | Owner:
Type: | keerthivasansa

Cleanup/optimization | Status: assigned
Component: Error reporting | Version: 4.2
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 1 | Needs documentation: 0
Needs tests: 1 | Patch needs improvement: 1
Easy pickings: 1 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by keerthivasansa):

* owner: Yash Kumar Verma => keerthivasansa

--
Ticket URL: <https://code.djangoproject.com/ticket/34746#comment:11>

Django

unread,
Feb 3, 2024, 9:06:37 AM2/3/24
to django-...@googlegroups.com
#34746: High CPU/memory consumption when a 5XX is raised with large local variables
-------------------------------------+-------------------------------------
Reporter: Rémi Dupré | Owner: Keerthi
Type: | Vasan S A

Cleanup/optimization | Status: assigned
Component: Error reporting | Version: 4.2
Severity: Normal | Resolution:
Keywords: | Triage Stage: Ready for
| checkin

Has patch: 1 | Needs documentation: 0
Needs tests: 1 | Patch needs improvement: 1
Easy pickings: 1 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Keerthi Vasan S A):

* stage: Accepted => Ready for checkin

--
Ticket URL: <https://code.djangoproject.com/ticket/34746#comment:12>

Django

unread,
Feb 3, 2024, 10:20:48 AM2/3/24
to django-...@googlegroups.com
#34746: High CPU/memory consumption when a 5XX is raised with large local variables
-------------------------------------+-------------------------------------
Reporter: Rémi Dupré | Owner: Keerthi
Type: | Vasan S A
Cleanup/optimization | Status: assigned
Component: Error reporting | Version: 4.2
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted

Has patch: 1 | Needs documentation: 0
Needs tests: 1 | Patch needs improvement: 1
Easy pickings: 1 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Mariusz Felisiak):

* stage: Ready for checkin => Accepted

Comment:

You should not mark your own PRs as "ready for checkin".
--
Ticket URL: <https://code.djangoproject.com/ticket/34746#comment:13>

Django

unread,
Feb 4, 2024, 9:12:53 AM2/4/24
to django-...@googlegroups.com
#34746: High CPU/memory consumption when a 5XX is raised with large local variables
-------------------------------------+-------------------------------------
Reporter: Rémi Dupré | Owner: Keerthi
Type: | Vasan S A
Cleanup/optimization | Status: assigned
Component: Error reporting | Version: 4.2
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 1 | Needs documentation: 0
Needs tests: 1 | Patch needs improvement: 1
Easy pickings: 1 | UI/UX: 0
-------------------------------------+-------------------------------------
Comment (by Keerthi Vasan S A):

Ah sorry, I wanted to set this ticket as ready to be reviewed as mentioned
by a contributor in the PR. How do I do that?
--
Ticket URL: <https://code.djangoproject.com/ticket/34746#comment:14>

Django

unread,
Feb 9, 2024, 10:25:37 AM2/9/24
to django-...@googlegroups.com
#34746: High CPU/memory consumption when a 5XX is raised with large local variables
-------------------------------------+-------------------------------------
Reporter: Rémi Dupré | Owner: Keerthi
Type: | Vasan S A
Cleanup/optimization | Status: assigned
Component: Error reporting | Version: 4.2
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 1 | Needs documentation: 0
Needs tests: 1 | Patch needs improvement: 1
Easy pickings: 1 | UI/UX: 0
-------------------------------------+-------------------------------------
Comment (by Natalia Bidart):

Replying to [comment:14 Keerthi Vasan S A]:


> Ah sorry, I wanted to set this ticket as ready to be reviewed as
mentioned by a contributor in the PR. How do I do that?

Hi!

The proper procedure to get a re-review is described in
[https://docs.djangoproject.com/en/dev/internals/contributing/writing-code
/submitting-patches/#patch-style the PR review checklist]. Basically you
need to unset the relevant flags in this ticket so the PR gets added back
to the "patches needing review" section of the
[https://dashboard.djangoproject.com/ Django Developer Dahsboard].
--
Ticket URL: <https://code.djangoproject.com/ticket/34746#comment:15>

Django

unread,
Feb 10, 2024, 1:40:37 AM2/10/24
to django-...@googlegroups.com
#34746: High CPU/memory consumption when a 5XX is raised with large local variables
-------------------------------------+-------------------------------------
Reporter: Rémi Dupré | Owner: Keerthi
Type: | Vasan S A
Cleanup/optimization | Status: assigned
Component: Error reporting | Version: 4.2
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 1 | Needs documentation: 0
Needs tests: 1 | Patch needs improvement: 1
Easy pickings: 1 | UI/UX: 0
-------------------------------------+-------------------------------------
Comment (by Keerthi Vasan S A):

Gotcha, thank you!
--
Ticket URL: <https://code.djangoproject.com/ticket/34746#comment:16>

Django

unread,
Feb 10, 2024, 1:41:10 AM2/10/24
to django-...@googlegroups.com
#34746: High CPU/memory consumption when a 5XX is raised with large local variables
-------------------------------------+-------------------------------------
Reporter: Rémi Dupré | Owner: Keerthi
Type: | Vasan S A
Cleanup/optimization | Status: assigned
Component: Error reporting | Version: 4.2
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 1 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Keerthi Vasan S A):

* needs_better_patch: 1 => 0
* needs_tests: 1 => 0

--
Ticket URL: <https://code.djangoproject.com/ticket/34746#comment:17>

Django

unread,
Feb 22, 2024, 10:11:20 AM2/22/24
to django-...@googlegroups.com
#34746: High CPU/memory consumption when a 5XX is raised with large local variables
-------------------------------------+-------------------------------------
Reporter: Rémi Dupré | Owner: Keerthi
Type: | Vasan S A
Cleanup/optimization | Status: assigned
Component: Error reporting | Version: 4.2
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 1 | Needs documentation: 0
Needs tests: 1 | Patch needs improvement: 1
Easy pickings: 1 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Natalia Bidart):

* needs_better_patch: 0 => 1
* needs_tests: 0 => 1

--
Ticket URL: <https://code.djangoproject.com/ticket/34746#comment:18>

Django

unread,
Aug 4, 2024, 10:26:00 AM8/4/24
to django-...@googlegroups.com
#34746: High CPU/memory consumption when a 5XX is raised with large local variables
-------------------------------------+-------------------------------------
Reporter: Rémi Dupré | Owner: Keerthi
Type: | Vasan S A
Cleanup/optimization | Status: assigned
Component: Error reporting | Version: 4.2
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 1 | Needs documentation: 0
Needs tests: 1 | Patch needs improvement: 1
Easy pickings: 1 | UI/UX: 0
-------------------------------------+-------------------------------------
Comment (by Ahmed Ibrahim):

Is this still being worked on or should I handle t?
--
Ticket URL: <https://code.djangoproject.com/ticket/34746#comment:19>

Django

unread,
Aug 12, 2024, 12:30:22 PM8/12/24
to django-...@googlegroups.com
#34746: High CPU/memory consumption when a 5XX is raised with large local variables
-------------------------------------+-------------------------------------
Reporter: Rémi Dupré | Owner: Keerthi
Type: | Vasan S A
Cleanup/optimization | Status: assigned
Component: Error reporting | Version: 4.2
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 1 | Needs documentation: 0
Needs tests: 1 | Patch needs improvement: 1
Easy pickings: 1 | UI/UX: 0
-------------------------------------+-------------------------------------
Comment (by Natalia Bidart):

Replying to [comment:19 Ahmed Ibrahim]:
> Is this still being worked on or should I handle t?

Hello Ahmed! Thanks for your eagerness to help. As we already discussed in
the PR, this is still being work on and I'll provide more feedback in the
PR.
--
Ticket URL: <https://code.djangoproject.com/ticket/34746#comment:20>

Django

unread,
Apr 27, 2025, 11:49:16 AMApr 27
to django-...@googlegroups.com
#34746: High CPU/memory consumption when a 5XX is raised with large local variables
-------------------------------------+-------------------------------------
Reporter: Rémi Dupré | Owner: Keerthi
Type: | Vasan S A
Cleanup/optimization | Status: assigned
Component: Error reporting | Version: 4.2
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 1 | Needs documentation: 0
Needs tests: 1 | Patch needs improvement: 1
Easy pickings: 1 | UI/UX: 0
-------------------------------------+-------------------------------------
Comment (by Jitesh Nair):

Hi,

Is this ticket open to be taken over?
--
Ticket URL: <https://code.djangoproject.com/ticket/34746#comment:21>
Reply all
Reply to author
Forward
0 new messages