Signal receivers cache should weakly reference senders.
|Reported by:||charettes||Owned by:||nobody|
|Cc:||Triage Stage:||Ready for checkin|
|Has patch:||yes||Needs documentation:||no|
|Needs tests:||no||Patch needs improvement:||no|
The signal receivers caching introduced in 704ee33f503c96b96c2682b946a11b3b42318ba7 (#16679) to speed up model initialization keeps strong reference to sender thus preventing them from being garbage collected.
In : import gc In : import weakref In : from django.dispatch import Signal In : signal = Signal(use_caching=True) In : def receiver(**kwargs): pass In : signal.connect(receiver) In : class cls: pass In : wref = weakref.ref(cls) In : signal.send(cls) Out: [(<function __main__.receiver>, None)] In : del cls In : gc.collect() Out: 33 In : wref() is None Out: False
Since the cache is only enabled on signals with BaseModel ((pre|post)_init, (pre|post)_save, (pre|post)_delete and m2m_changed) or module (post_syncdb) instances senders this shouldn't be an issue in usual cases since those objects are not meant to be garbage collected during the life of the application.
However it might cause issues to people dynamically creating model classes and modules since those objects won't be garbage collected as of Django 1.6.
I suggest we use a WeakKeyDictionary to cache sender receivers in order to prevent regressions in 1.6 and avoid hard to traceback memory leaks.
Running djangobench on the soon to be attached patch revealed no significant slowdowns.
Change History (6)
Changed 7 months ago by charettes
comment:1 Changed 7 months ago by charettes
- Severity changed from Release blocker to Normal
- Summary changed from Signal receivers caching should weakly reference senders. to Signal receivers cache should weakly reference senders.
comment:2 Changed 7 months ago by akaariai
- Triage Stage changed from Unreviewed to Ready for checkin
comment:3 Changed 7 months ago by Simon Charette <charette.s@…>
- Resolution set to fixed
- Status changed from new to closed