Signal receivers cache should weakly reference senders.
|Reported by:||Simon Charette||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
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 3 years ago by
comment:1 Changed 3 years ago by
|Severity:||Release blocker → Normal|
|Summary:||Signal receivers caching should weakly reference senders. → Signal receivers cache should weakly reference senders.|
comment:2 Changed 3 years ago by
|Triage Stage:||Unreviewed → Ready for checkin|