Opened 19 months ago
Last modified 7 months ago
#34746 assigned Cleanup/optimization
High CPU/memory consumtion when a 5xx is raised with a large local variable — at Initial Version
Description ¶
Hi!
In a view with large variable in scope, if an exception is raised, the worker will freeze and its memory usage will raise. Here is a minimal example:
import sys from django.urls import path # 300MB large_data = list(range(40 * 1024 * 1024)) def compute_lines(request): local_data = large_data raise ValueError urlpatterns = [ path("compute-lines/", compute_lines) ]
When calling /compute-lines/ you will notice the issue.
After a bit of investigations, it turned out it comes from django.utils.log.AdminEmailHandler in the default logging config. This handler will eventually pretty print the whole context of each trace frame from here : https://github.com/django/django/blob/main/django/views/debug.py#L355
This implementation ensures that no more than 4kB are included in the formatted result, but the entire variable will still have to be rendered first. I'm not sure how it could be solved, but maybe reprlib can be a clue?
According to the ticket's flags, the next step(s) to move this issue forward are:
- To add tests to the patch, then uncheck the "Needs tests" flag on the ticket.
- To improve the patch as described in the pull request review comments or on this ticket, then uncheck "Patch needs improvement".
If creating a new pull request, include a link to the pull request in the ticket comment when making that update. The usual format is:
[https://github.com/django/django/pull/#### PR]
.