You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Chord does not clean up subscribed channels in redis #3812
On step 3 we have 4 open channels
On step 5 we have 8 open channels
On our live application in 2 hours (between restarts) we have 3 200 000 open channels, which generally slows everything and hell gates open with tenth of different issues.
PS: will try to return with pull request later
Reacted by Jeremy Wu, Flávio Juvenal, anozh, Christian Pedersen, Lars Solberg, Kacper Krupa and Alex Lopes
@MikaelWahlberg I have reproduced the bug with the prefork pool. I found some instances in the source code where the backend should unsubscribe from channels but have not resolved it completely yet. One temporary solution for the worker might be to set --max-tasks-per-child to a relatively low number (e.g. 5-10).
@georgepsarakis Yeah, that could have worked as a workaround, but our main problem seems to be with Celery beat, and there we don't have that option...
Glad to found this issue. My redis-cli pubsub channels | grep celery-task-meta | wc -l is currently at ~48k, which I found strange.. It doesn't cause an issue yet tho, but it might soon...
@MikaelWahlberg what do you mean exactly? beat runs separately than the worker. From my research so far, the problem is caused by the worker processes and my suggested option refers to that. I would suggest that anyone that encounters this issue to use --max-tasks-per-child, which will allow the connections that appear subscribed to be closed.
@georgepsarakis Sorry, I missed this comment. What I mean is that the number of pubsub channels keep growing, even if we set --max-tasks-per-child on the "receiving" workers. And once we restart the celery-beat process it goes down again.
Sure, as a workaround we could restart the celery-beat every day as well, that would probably help a lot.
It seems to me that there's no reason for celery-beat to even bother subscribing in the first place - it doesn't deal with results, it just queues up tasks.
A side effect of this issue can lead to some out-of-control resource usage - at a certain point, duplicate subscribe messages were being sent out from the beat worker to our Redis instance, and these grew substantially. I hypothesize that this was due to Redis taking a very long time (or just refusing) to acknowledge the subscription, so the task IDs stay in the queue of task IDs to subscribe to, but I haven't dug deep enough into the result backend code to confirm this as the cause.
Regardless, this can be addressed by ensuring that the beat worker doesn't establish a connection to the result backend at all.
Hi. Are you sure this behaviour is related to chords only? I have a simple test setup with a simple task from the base tutorial and made the experience, that something like the code below executed from the python shell opens 100 channels, which are only closed once the python process is terminated.
for i in xrange(1,100):
tasks.add.apply_async((2,2))
I have rerun @mleginus's sample code above and I still get the failure.
My pip3 freeze is:
pip3 freeze | grep celery
celery==4.2.0rc3
The output of his script is:
Redis channels count before chord execution 0
Redis channels count after chord execution 10
Redis channels count before chord execution 10
Redis channels count after chord execution 20
Redis channels count before chord execution 20
Redis channels count after chord execution 30
Several minutes after running the script, running 'redis-cli pubsub channels' still shows the celery-task-meta-xxx elements as still subscribed.
I've tried this on two machines, one Ubuntu 16 and the other mac os X. Redis 4.0.7.
I have confirmed that the tasks are actually succeeding: AsyncResult(task_id.id).status == "SUCCESS".
Therefore, I believe the issue is still that the pubsub client is not receiving the task_succeed message from the work. Today is my first day looking at celery code, so my beliefs should not be trusted.
I have found a workaround for now, which is not very nice: for tasks that have no return value, I call
task_id.backend.result_consumer.cancel_for(task_id.task_id) using the task_id returned from .delay().
@bartloop thanks for the feedback. I am guessing that you are referring to this script, correct?
You need to call AsyncResult.get() so that the caller unsubscribes from the chord. With the code as it is now, the channels will remain active, as long as the ipython process remains active.
Try running this for testing:
defrun_test_task():
"""Run test task."""redis=StrictRedis()
channels_before=len(redis.execute_command('PUBSUB CHANNELS'))
print('Redis channels count before chord execution {}'.format(channels_before))
result=enrich.delay()
sleep(5)
channels_after=len(redis.execute_command('PUBSUB CHANNELS'))
print('Redis channels count after chord execution {}'.format(channels_after))
result.get()
channels_after=len(redis.execute_command('PUBSUB CHANNELS'))
print('Redis channels count after result retrieval {}'.format(channels_after))
@georgepsarakis thank you for taking the time to show me that correction.
The code you modified decremented the count by 1, not by 10:
Redis channels count before chord execution 0
Redis channels count after chord execution 10
Redis channels count after result retrieval 9
Redis channels count before chord execution 9
Redis channels count after chord execution 19
Redis channels count after result retrieval 18
But, our issue is not really about chords - we're struggling with doing .delay()s on single tasks with no return value.
So here's my newbie question: for tasks that have no result, how do I do the equivalent of .get() but in a non-blocking way? The documentation for AsyncResult shows a forget() function, which seems to be what I want, but it has no effect on the subscription count. I also tried adding ignore_result=True to the task decorators, but that had no effect. I assume there is some way to do this.
@bartloop This sounds like a different but related issue.
Please open a new one with the relevant test case.
If you are up to it, contribute an integration test and Mark it as xfail.
@georgepsarakis I hope you don't mind me closing this one again.
I'd rather keep our issue list clean and this one should be resolved.
If there is an exact regression of this issue let's reopen. If not, please encourage our users to open issues with the relevant test cases.
Err... is it just me or should this issue be reopened ? I just tried out gugu's code on a recent 4.4.0 install and I still have the same symptoms - subscriptions for the chord's header subtasks are still not cleared.
NB : I'm still browsing the (numerous) related tickets etc so I might have overlooked something though - if yes, please enlighten me ;-)
Activity
[-]Chord does not clean up subscribed channels[/-][+]Chord does not clean up subscribed channels in redis[/+]MikaelWahlberg commentedon Aug 14, 2017
Any news about this? We are also running in to this issue and it causes us a bit of a headache...
gugu commentedon Aug 14, 2017
I wrote my own chord implementation and did not try to fix this
georgepsarakis commentedon Aug 15, 2017
@gugu do you mind sharing your work? If you have it available in a public repository of course.
georgepsarakis commentedon Aug 15, 2017
@MikaelWahlberg I have reproduced the bug with the prefork pool. I found some instances in the source code where the backend should unsubscribe from channels but have not resolved it completely yet. One temporary solution for the worker might be to set
--max-tasks-per-childto a relatively low number (e.g. 5-10).MikaelWahlberg commentedon Aug 15, 2017
@georgepsarakis Yeah, that could have worked as a workaround, but our main problem seems to be with Celery beat, and there we don't have that option...
xeor commentedon Aug 22, 2017
Glad to found this issue. My
redis-cli pubsub channels | grep celery-task-meta | wc -lis currently at ~48k, which I found strange.. It doesn't cause an issue yet tho, but it might soon...georgepsarakis commentedon Aug 22, 2017
@MikaelWahlberg what do you mean exactly?
beatruns separately than the worker. From my research so far, the problem is caused by the worker processes and my suggested option refers to that. I would suggest that anyone that encounters this issue to use--max-tasks-per-child, which will allow the connections that appear subscribed to be closed.MikaelWahlberg commentedon Sep 4, 2017
@georgepsarakis Sorry, I missed this comment. What I mean is that the number of pubsub channels keep growing, even if we set --max-tasks-per-child on the "receiving" workers. And once we restart the celery-beat process it goes down again.
Sure, as a workaround we could restart the celery-beat every day as well, that would probably help a lot.
gugu commentedon Sep 9, 2017
@georgepsarakis yes, at Monday I'll create a separate module for this
jmaroeder commentedon Sep 12, 2017
It seems to me that there's no reason for celery-beat to even bother subscribing in the first place - it doesn't deal with results, it just queues up tasks.
A side effect of this issue can lead to some out-of-control resource usage - at a certain point, duplicate subscribe messages were being sent out from the beat worker to our Redis instance, and these grew substantially. I hypothesize that this was due to Redis taking a very long time (or just refusing) to acknowledge the subscription, so the task IDs stay in the queue of task IDs to subscribe to, but I haven't dug deep enough into the result backend code to confirm this as the cause.
Regardless, this can be addressed by ensuring that the beat worker doesn't establish a connection to the result backend at all.
gugu commentedon Sep 17, 2017
@georgepsarakis here is my very first ezy-chord release:
https://github.com/gugu/ezy-chord
(tomorrow I'll move it to my company's webpage: https://github.com/EzyInsights/ezy-chord)
rdrongow commentedon Sep 29, 2017
Hi. Are you sure this behaviour is related to chords only? I have a simple test setup with a simple task from the base tutorial and made the experience, that something like the code below executed from the python shell opens 100 channels, which are only closed once the python process is terminated.
27 remaining items
georgepsarakis commentedon Apr 19, 2018
@mleginus do you perhaps have time to test the branch on PR #4666 ? Your feedback will be valuable.
johnarnold commentedon Apr 27, 2018
@mleginus @georgepsarakis @thedrow Any update on this? Still blocking 4.2...
georgepsarakis commentedon Apr 28, 2018
@johnarnold we would like some feedback on the possible solution. #4666 will probably be merged into master quite soon.
bartloop commentedon May 3, 2018
I have rerun @mleginus's sample code above and I still get the failure.
My pip3 freeze is:
pip3 freeze | grep celery
celery==4.2.0rc3
The output of his script is:
Redis channels count before chord execution 0
Redis channels count after chord execution 10
Redis channels count before chord execution 10
Redis channels count after chord execution 20
Redis channels count before chord execution 20
Redis channels count after chord execution 30
Several minutes after running the script, running 'redis-cli pubsub channels' still shows the celery-task-meta-xxx elements as still subscribed.
I've tried this on two machines, one Ubuntu 16 and the other mac os X. Redis 4.0.7.
I have confirmed that the tasks are actually succeeding: AsyncResult(task_id.id).status == "SUCCESS".
Therefore, I believe the issue is still that the pubsub client is not receiving the task_succeed message from the work. Today is my first day looking at celery code, so my beliefs should not be trusted.
I have found a workaround for now, which is not very nice: for tasks that have no return value, I call
task_id.backend.result_consumer.cancel_for(task_id.task_id) using the task_id returned from .delay().
Let me know if I can provide any other info.
georgepsarakis commentedon May 4, 2018
@bartloop thanks for the feedback. I am guessing that you are referring to this script, correct?
You need to call
AsyncResult.get()so that the caller unsubscribes from the chord. With the code as it is now, the channels will remain active, as long as theipythonprocess remains active.Try running this for testing:
bartloop commentedon May 4, 2018
@georgepsarakis thank you for taking the time to show me that correction.
The code you modified decremented the count by 1, not by 10:
Redis channels count before chord execution 0
Redis channels count after chord execution 10
Redis channels count after result retrieval 9
Redis channels count before chord execution 9
Redis channels count after chord execution 19
Redis channels count after result retrieval 18
But, our issue is not really about chords - we're struggling with doing .delay()s on single tasks with no return value.
So here's my newbie question: for tasks that have no result, how do I do the equivalent of .get() but in a non-blocking way? The documentation for AsyncResult shows a forget() function, which seems to be what I want, but it has no effect on the subscription count. I also tried adding ignore_result=True to the task decorators, but that had no effect. I assume there is some way to do this.
Thanks!
thedrow commentedon May 4, 2018
@bartloop This sounds like a different but related issue.
Please open a new one with the relevant test case.
If you are up to it, contribute an integration test and Mark it as xfail.
thedrow commentedon May 4, 2018
@georgepsarakis I hope you don't mind me closing this one again.
I'd rather keep our issue list clean and this one should be resolved.
If there is an exact regression of this issue let's reopen. If not, please encourage our users to open issues with the relevant test cases.
georgepsarakis commentedon May 4, 2018
@thedrow not at all, I just reopened in order not to discuss on a closed issue, thanks a lot for explaining.
mleginus commentedon Jun 18, 2018
@georgepsarakis and @thedrow I can reproduce the behaviour reported by @bartloop -
#3812 (comment) I would like to see whether I will be able to write an integration test case that could reproduce such behaviour. For this, I need to have two celery workers running inside the test - one listening on
childrenqueue and another ondefaultqueue. Could you advice on how to create a second worker fixture (in addition tocelery_session_workerhttps://github.com/celery/celery/blob/master/t/integration/conftest.py#L65) and pass in appropriate settings to listen on a specific queue? Thanks in advance.georgepsarakis commentedon Jun 18, 2018
@mleginus could you please open a new issue so that we can analyze this further? Thanks!
BrunoDesthuilliers commentedon Jan 29, 2020
Err... is it just me or should this issue be reopened ? I just tried out gugu's code on a recent 4.4.0 install and I still have the same symptoms - subscriptions for the chord's header subtasks are still not cleared.
NB : I'm still browsing the (numerous) related tickets etc so I might have overlooked something though - if yes, please enlighten me ;-)
celery -A app report :