Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: ConfigWatcher needs to register receivers using strong references #495

Merged
merged 1 commit into from
Nov 6, 2023

Conversation

timmc-edx
Copy link
Member

These inner functions would otherwise get GC'd right away, since Django only holds weak references to receivers by default. (I don't know why this worked in devstack.)

Merge checklist:
Check off if complete or not applicable:

  • Version bumped
  • Changelog record added
  • Documentation updated (not only docstrings)
  • Fixup commits are squashed away
  • Unit tests added/updated
  • Manual testing instructions provided
  • Noted any: Concerns, dependencies, migration issues, deadlines, tickets

These inner functions would otherwise get GC'd right away, since Django
only holds weak references to receivers by default.
Copy link
Contributor

@robrap robrap left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Seems likely to be fine, but I'm curious...

Also, do you know if the plugin signals would not have had this issue?

@receiver(signals.post_save, sender=model, dispatch_uid=f"config_watcher_{short_name}_change")

# Note that weak=False is required here. Django by default only
# holds weak references to receiver functions. But these inner
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Out of curiosity, do you know why it stores weak references and what type of clean-up problems this might cause?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure why it stores weak references, no. I could make some guesses -- perhaps there are architectures where signals are added temporarily, or where an object listens to signals during its lifetime but those listeners should not block garbage collection of the object.

I don't foresee any issue with using strong references here. Normally our @receiver-decorated functions are top-level functions, which means the enclosing module would hold a strong reference to the function.

I don't think plugin signals would have had this issue because they require you to point to a top-level function. (Which is also why they were unsuitable for this app, at least if we want to use sender. The alternative would be to listen to all model saves in a top-level function and then have a conditional in the receiver's body.)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Incidentally, I actually had a branch up for a bit with the top-level function approach before I realized I had overlooked the weak param: main...timmc/top-level#diff-1206f94d4250bda1d46a290ece9b20f6bc8b2f961b6f9adfc91a42002d54099aR93

@timmc-edx timmc-edx merged commit 729fd8d into main Nov 6, 2023
5 checks passed
@timmc-edx timmc-edx deleted the timmc/strong branch November 6, 2023 17:02
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants