﻿id	summary	reporter	owner	description	type	status	component	version	severity	resolution	keywords	cc	stage	has_patch	needs_docs	needs_tests	needs_better_patch	easy	ui_ux
20943	Signal receivers cache should weakly reference senders.	Simon Charette	nobody	"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.

{{{#!python
In [1]: import gc

In [2]: import weakref

In [3]: from django.dispatch import Signal

In [4]: signal = Signal(use_caching=True)

In [5]: def receiver(**kwargs): pass

In [6]: signal.connect(receiver)

In [7]: class cls: pass

In [8]: wref = weakref.ref(cls)

In [9]: signal.send(cls)
Out[9]: [(<function __main__.receiver>, None)]

In [10]: del cls

In [11]: gc.collect()
Out[11]: 33

In [12]: wref() is None
Out[12]: 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."	Bug	closed	Core (Other)	1.6-beta-1	Normal	fixed			Ready for checkin	1	0	0	0	0	0
