Opened 3 hours ago

Last modified 3 hours ago

#36662 new Cleanup/optimization

django.contrib.messages Storage creates circular references with Request objects

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

Description (last modified by Raphael Gaschignard)

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.

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

Attachments (1)

cycle.2.png (57.1 KB ) - added by Raphael Gaschignard 3 hours ago.
An example of the sort of cycle that appears (objgraph output)

Download all attachments as: .zip

Change History (2)

by Raphael Gaschignard, 3 hours ago

Attachment: cycle.2.png added

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

comment:1 by Raphael Gaschignard, 3 hours ago

Description: modified (diff)
Note: See TracTickets for help on using tickets.
Back to Top