Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
pairs with this PR django/channels_redis#339
to address a memory leak issue in pubsub backend django/channels_redis#276
Here is the race condition in pubsub.py that leads to the memory leak:
asyncio.CancelledError
while awaiting in the receive() method, we get a chance to cleanup internal data related to the channel and all is wellasyncio.CancelledError
outside of the receive() method, we cancel the channels task, but we don't have a way to cleanup internal data related to that channel. That data will live forever until the process is restarted.Solution is to set a
cancel_callback
in the dispatch code that will run when hitting anasyncio.CancelledError
. By default this callback will be None, but if the channels_layer has aclean_channel
method, it will call that instead (with the channel name as the argument, so we know which channel to cleanup)TODO: maybe tests? I might need some help here