Repository navigation
Description
Self Checks
- I have read the Contributing Guide and Language Policy.
- This is only for bug report, if you would like to ask a question, please head to Discussions.
- I have searched for existing issues search for existing issues, including closed ones.
- I confirm that I am using English to submit this report, otherwise it will be closed.
- 【中文用户 & Non English User】请使用英语提交,否则会被关闭 :)
- Please do not modify this template :) and fill in all the required fields.
Dify version
1.20.0 (also reproduced on 1.17.1; still present on main)
Cloud or Self Hosted
Self Hosted (Docker)
Steps to reproduce
This is a regression of the leak fixed in #44551.
#44551 replaced fire-and-forget celery.group(...).apply_async() dispatches with individual apply_async() calls in three places, because a group dispatched from inside a gevent worker task leaks one Redis pubsub connection per dispatch. The later refactor #45941 (refactor(api): isolate trigger schedule runtime) moved schedule publication to api/services/trigger/schedule_gateway.py and re-introduced group():
def publish_schedules(schedule_ids: Sequence[str]) -> None:
from tasks.workflow_schedule_tasks import run_schedule_trigger
group(run_schedule_trigger.s(schedule_id) for schedule_id in schedule_ids).apply_async()publish_schedules is called by poll_workflow_schedules, which is enabled by default (ENABLE_WORKFLOW_SCHEDULE_POLLER_TASK=true, WORKFLOW_SCHEDULE_POLLER_INTERVAL=1). The other two call sites from #44551 (trigger_provider_refresh, rag_pipeline_run_task) are still fixed in 1.20.0.
To reproduce:
- Deploy with docker compose using the defaults (
CELERY_BACKEND=redis, gevent worker pool). - Publish at least one workflow with a Schedule trigger, so that the poller dispatches every minute.
- Watch Redis clients over time:
redis-cli client list | grep cmd=unsubscribe | wc -l
✔️ Expected Behavior
The schedule poller publishes run_schedule_trigger tasks without opening result-backend subscriptions, and the number of Redis connections from the worker stays flat.
❌ Actual Behavior
The worker leaks one Redis connection per poller tick, so about 1 per minute or 1,440 per day, without bound. The leaked connections idle forever. Their last command is UNSUBSCRIBE on celery-task-meta-<task-id>, and their idle times are exactly 60 seconds apart:
id=5318 idle=1078 sub=0
id=5372 idle=996 sub=0
id=5375 idle=936 sub=0
id=5380 idle=875 sub=0
MONITOR shows a brand-new connection (AUTH + SELECT + SUBSCRIBE celery-task-meta-<id>) on every tick. Instrumenting celery.backends.redis.ResultConsumer.start shows a fresh consumer with pubsub=None on every tick.
Root cause (Celery 5.6.3):
RedisBackend.thread_safeisFalse, soapp.backendis stored inthreading.local()(celery/app/base.py). Under gevent this is greenlet-local, and each task runs in a fresh greenlet.group.apply_async()registers a barrier on every child result (celery/canvas.py:res.then(p, weak=True)). This callsbackend.add_pending_result(), thenResultConsumer.consume_from(), and finallyResultConsumer.start(), which opens a dedicated pubsub connection.- When the task greenlet exits, its greenlet-local backend, consumer, and pubsub are abandoned and never stopped. The socket stays open.
Production impact we observed: after about 2.7 days of uptime there were more than 1,000 leaked connections and connected_clients was above 1,300. The resulting memory pressure and swapping stalled dify-sandbox interpreter startup, so workflow Code nodes were killed by the sandbox timeout. After dispatching the tasks individually, new zombie connections stopped appearing and the sandbox kills stopped.
Suggested fix: as in #44551, dispatch the tasks individually. The group's GroupResult is never awaited, so the barrier serves no purpose here.
- group(run_schedule_trigger.s(schedule_id) for schedule_id in schedule_ids).apply_async()
+ for schedule_id in schedule_ids:
+ run_schedule_trigger.apply_async(args=(schedule_id,))Workaround: restart the worker container periodically.
Activity