Opened 7 weeks ago

Last modified 6 weeks ago

#36662 assigned Cleanup/optimization

django.contrib.messages Storage creates circular references with Request objects — at Initial Version

Reported by: Raphael Gaschignard Owned by:
Component: contrib.messages Version: dev
Severity: Normal Keywords: gc, garbage, weakref
Cc: Raphael Gaschignard Triage Stage: Accepted
Has patch: yes Needs documentation: no
Needs tests: yes Patch needs improvement: yes
Easy pickings: no UI/UX: no

Description

To be honest I don't know how much people care about this kind of issue, but I am having some (very silly) operational issues downstream of Requests getting garbage collected at awkward times.

Request objects hold onto message storages via Request._messages. The storage classes hold references back to Request. This creates a reference cycle preventing Requests from being collected as fast as they could be (and can create some awkwardness in CPython, with del calls happening on associated objects in seemingly unrelated spots)

The "simple" fix here would be to hold onto request in the storage through a weakref, and add a property to get the request object on the storage class.

Change History (1)

by Raphael Gaschignard, 7 weeks ago

Attachment: cycle.2.png added

An example of the sort of cycle that appears (objgraph output)

Note: See TracTickets for help on using tickets.
Back to Top