#35801: Signals are dispatched to receivers associated with dead senders
------------------------------+------------------------------------
     Reporter:  bobince       |                    Owner:  (none)
         Type:  Bug           |                   Status:  new
    Component:  Core (Other)  |                  Version:  5.1
     Severity:  Normal        |               Resolution:
     Keywords:                |             Triage Stage:  Accepted
    Has patch:  1             |      Needs documentation:  0
  Needs tests:  0             |  Patch needs improvement:  1
Easy pickings:  0             |                    UI/UX:  0
------------------------------+------------------------------------
Changes (by Simon Charette):

 * has_patch:  0 => 1
 * needs_better_patch:  0 => 1

Comment:

 Thanks for all the extra details Sjoerd, this is greatly appreciated.

 Here's how I think we can resume the problem in a few bullet points

 1. `Signal` doesn't provide a way to connect to non-weak senders
 2. `Signal.receivers` doesn't hold a strong reference to senders as it
 uses `id` to retrieve them by lookup and under memory pressure `id` can
 return the same value for two distinct objects
 3. The migration framework creates and disposes of `ModelBase` instances
 (aka `Model` subclasses) which creates a  lot of memory pressure (see
 #29898) and cycles through `sender` for all model signals which makes the
 perfect conditions for `id(SomeFakeModel) == id(SomeOtherFakeModel)`
 collisions.

 With all that said I can see a few solutions which should be considered.

 First making `Signal.connect(weak=True)` also use `weakref` for senders
 like
 [https://github.com/bobince/django/compare/main..signals_from_dead_senders
 bobince implemented] seems like a good step forward. If you could open a
 PR from your branch that also tests `Signal(use_caching=True)` (which
 should work as it already uses a `WeakKeyDictionary` for its sender cache)
 we should be land this work independently of the following proposed
 solutions. I believe we should have `weakref.ref(sender)` raise a
 `TypeError` instead of silencing though as the proposed code now holds a
 ''strong'' reference to `sender` in `receivers` which could make things
 much worst but this can be discussed further during code review.

 That should leave us with what should be done with signal receivers
 connected with `weak=False`. In the case of `django-dirty-fields`'s usage
 Sjoerd I think they should switch to `weak=True` as their `reset_state`
 receiver is defined at the module level anyway so it's causing more harm
 than good. There are albeit rare use cases for `weak=False` and it's a
 public API so I wonder if the migration framework should do a better job
 at clearing after itself by implementing `__del__` logic for `AppConfig`
 and fake models for the core signals at least.
-- 
Ticket URL: <https://code.djangoproject.com/ticket/35801#comment:7>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

-- 
You received this message because you are subscribed to the Google Groups 
"Django updates" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To view this discussion visit 
https://groups.google.com/d/msgid/django-updates/01070194323cd1c9-36597853-ebc7-468d-bd75-8ed66c0334b8-000000%40eu-central-1.amazonses.com.

Reply via email to