Skip to content

Chord does not clean up subscribed channels in redis #3812

Closed

Description

@gugu

Checklist

  • I have included the output of celery -A proj report in the issue.
    (if you are not able to do this, then at least specify the Celery
    version affected).
  • I have verified that the issue exists against the master branch of Celery.
software -> celery:4.0.2 (latentcall) kombu:4.0.2 py:3.5.2
            billiard:3.5.0.2 py-amqp:2.1.4
platform -> system:Darwin arch:64bit imp:CPython
loader   -> celery.loaders.app.AppLoader
settings -> transport:pyamqp results:redis://127.0.0.1/

result_backend: 'redis://127.0.0.1/'
broker_url: 'amqp://guest:********@localhost:5672//'

Steps to reproduce

  1. Create chord on celery with eventlet on redis backend.
  2. Run chord
  3. Run pubsub channels
  4. Run chord again
  5. Run pubsub channels

Code example: https://gist.github.com/gugu/680452f754507ec6bea144a2b257c398

Expected behavior

On step 3 and 5 we need to have 0 channels

Actual behavior

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

Activity

  1. changed the title [-]Chord does not clean up subscribed channels[/-] [+]Chord does not clean up subscribed channels in redis[/+] on Feb 6, 2017
  2. MikaelWahlberg commented on Aug 14, 2017

    @MikaelWahlberg

    Any news about this? We are also running in to this issue and it causes us a bit of a headache...

  3. gugu commented on Aug 14, 2017

    @gugu
    Author

    I wrote my own chord implementation and did not try to fix this

  4. georgepsarakis commented on Aug 15, 2017

    @georgepsarakis
    Contributor

    @gugu do you mind sharing your work? If you have it available in a public repository of course.

  5. georgepsarakis commented on Aug 15, 2017

    @georgepsarakis
    Contributor

    @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).

  6. MikaelWahlberg commented on Aug 15, 2017

    @MikaelWahlberg

    @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...

  7. xeor commented on Aug 22, 2017

    @xeor

    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...

  8. georgepsarakis commented on Aug 22, 2017

    @georgepsarakis
    Contributor

    @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.

  9. MikaelWahlberg commented on Sep 4, 2017

    @MikaelWahlberg

    @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.

  10. gugu commented on Sep 9, 2017

    @gugu
    Author

    @georgepsarakis yes, at Monday I'll create a separate module for this

  11. jmaroeder commented on Sep 12, 2017

    @jmaroeder
    Contributor

    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.

  12. rdrongow commented on Sep 29, 2017

    @rdrongow

    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))
    
  13. 27 remaining items

  14. georgepsarakis commented on Apr 19, 2018

    @georgepsarakis
    Contributor

    @mleginus do you perhaps have time to test the branch on PR #4666 ? Your feedback will be valuable.

  15. johnarnold commented on Apr 27, 2018

    @johnarnold
    Contributor

    @mleginus @georgepsarakis @thedrow Any update on this? Still blocking 4.2...

  16. georgepsarakis commented on Apr 28, 2018

    @georgepsarakis
    Contributor

    @johnarnold we would like some feedback on the possible solution. #4666 will probably be merged into master quite soon.

  17. bartloop commented on May 3, 2018

    @bartloop
    Contributor

    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.

  18. georgepsarakis commented on May 4, 2018

    @georgepsarakis
    Contributor

    @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:

    def run_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))
  19. bartloop commented on May 4, 2018

    @bartloop
    Contributor

    @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!

  20. thedrow commented on May 4, 2018

    @thedrow
    Contributor

    @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.

  21. thedrow commented on May 4, 2018

    @thedrow
    Contributor

    @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.

  22. georgepsarakis commented on May 4, 2018

    @georgepsarakis
    Contributor

    @thedrow not at all, I just reopened in order not to discuss on a closed issue, thanks a lot for explaining.

  23. mleginus commented on Jun 18, 2018

    @mleginus

    @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 children queue and another on default queue. Could you advice on how to create a second worker fixture (in addition to celery_session_worker https://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.

  24. georgepsarakis commented on Jun 18, 2018

    @georgepsarakis
    Contributor

    @mleginus could you please open a new issue so that we can analyze this further? Thanks!

  25. BrunoDesthuilliers commented on Jan 29, 2020

    @BrunoDesthuilliers

    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 :

    software -> celery:4.4.0 (cliffs) kombu:4.6.7 py:2.7.17
                billiard:3.6.1.0 py-amqp:2.5.2
    platform -> system:Linux arch:64bit
                kernel version:5.3.0-26-generic imp:CPython
    loader   -> celery.loaders.app.AppLoader
    settings -> transport:pyamqp results:redis://localhost/
    
    worker_max_tasks_per_child: 10
    result_extended: True
    worker_prefetch_multiplier: 1
    worker_send_sent_event: True
    result_expires: 86400
    worker_disable_rate_limits: True
    worker_send_task_events: True
    timezone: u'Europe/Paris'
    track_started: True
    broker_url: u'amqp://guest:********@localhost:5672//'
    result_backend: u'redis://localhost/'
    worker_pool_restarts: True
    
    
    
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Type

No type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions