Repository navigation
Description
Warning
Production impact: this issue caused more than 10,000 Redis connections to accumulate in my production environment.
This is a production observation, not a connection count generated by the automated reproduction below. This report does not claim a confirmed service outage or data loss.
Problem
A long-lived worker consuming schedule_poller with Celery's gevent pool can retain Redis result-subscription connections after it has dispatched scheduled workflows. The tasks finish, but the associated result drainer keeps the PubSub/backend alive. Over time, repeated dispatch batches accumulate idle connections instead of releasing them.
The total Redis connection count is not expected to increase by exactly one on every workflow execution: a batch can contain multiple schedules, and unrelated worker activity can open or close connections. The useful signal is that new result connections remain idle after dispatch finishes and more of these connections appear over subsequent batches.
Environment and scope
- Initially observed on a Dify 1.16.1-based deployment.
- Celery 5.6.3, gevent 26.4.0, redis-py 7.4.0.
- Redis broker/result backend; gevent worker pool; default non-shared result backend configuration.
- The same
group(...).apply_async()dispatch path is present in upstreammainat405c39692da2e1bd7945290ee8f3a310e6a9438a, inapi/schedule/workflow_schedule_task.py.
Reproduction
- In a test environment, run a long-lived gevent Celery worker that consumes
schedule_pollerand has a Redis result backend. - Publish and enable one minimal workflow with a recurring schedule, such as once per minute. Avoid external side effects in the workflow.
- Confirm that the poller actually dispatches due schedules. Look for its existing log message,
Total processed: ... workflow schedule(s) dispatched. An empty poll does not exercise the affected group code. - Track the poller process's Redis client IDs, last command, age, and idle time over multiple dispatch batches. Attribute connections to the process rather than relying on server-wide totals, particularly behind NAT.
- Disable the test schedule and let outstanding work finish. Check whether the same result-client IDs remain idle, often with
UNSUBSCRIBEas their last command. Enable the schedule again and check for additional retained connections.
Do not run the group directly in a short-lived shell process to test this: process exit closes its sockets and hides retention in a long-lived worker.
Expected / actual behavior
Expected: once this fire-and-forget dispatch no longer needs its result consumer, the consumer stops and its connections close. Other tasks' backends and the broker remain usable.
Actual: result subscriptions can be canceled while the drainer remains alive and retains its Redis connection. This led to the production accumulation reported above.
Evidence and cause
Diagnosis of this path found idle Redis clients with UNSUBSCRIBE as their last command. Source inspection and an isolated Celery/gevent reproduction identify this sequence:
group.apply_async() → AsyncResult.then() → result drainer/subscription → result cancellation without stopping the drainer.
task_ignore_result=True disables ordinary task result storage, but does not prevent the group's callback-registration path. Unsubscribing a channel does not by itself close the result consumer. In redis-py 7.4.0, calling PubSub.close() does disconnect its held connection; that lifecycle close is missing here.
The production count above is reported separately from the automated test. Raw production logs, internal addresses, credentials, and workflow contents are not published here.
Proposed fix and verification limits
PR #44543 scopes a private Redis result backend to each dispatch, stops its drainer, closes PubSub and the private pool in finally, then restores the original backend. It keeps group publication and avoids closing the broker/shared backend. The schedule commit happens before waiting for cleanup on the successful path.
Regression tests exercise the Celery/gevent lifecycle in isolation. This is not a claim that the fix has already been validated under production load or all Redis network-fault scenarios. See the PR for exact commands, results, and current CI status.
Activity
[-]Redis result subscription connections accumulate in gevent schedule poller workers[/-][+]Scheduled workflow polling leaves Redis result connections open (10,000+ observed in production)[/+]wylswz commentedon Oct 22, 2026
Reproduced. Here's a script for checking stale connections locally.