Skip to content

Scheduled workflow polling leaves Redis result connections open (10,000+ observed in production) #44542

Closed

Description

@CodeWarriors0

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 upstream main at 405c39692da2e1bd7945290ee8f3a310e6a9438a, in api/schedule/workflow_schedule_task.py.

Reproduction

  1. In a test environment, run a long-lived gevent Celery worker that consumes schedule_poller and has a Redis result backend.
  2. Publish and enable one minimal workflow with a recurring schedule, such as once per minute. Avoid external side effects in the workflow.
  3. 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.
  4. 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.
  5. Disable the test schedule and let outstanding work finish. Check whether the same result-client IDs remain idle, often with UNSUBSCRIBE as 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

  1. changed the title [-]Redis result subscription connections accumulate in gevent schedule poller workers[/-] [+]Scheduled workflow polling leaves Redis result connections open (10,000+ observed in production)[/+] on Oct 22, 2026
  2. wylswz commented on Oct 22, 2026

    @wylswz
    Contributor

    Reproduced. Here's a script for checking stale connections locally.

    docker exec $(docker ps -qf name=redis | head -1) sh -c 'redis-cli -a "$REDIS_PASSWORD" --no-auth-warning CLIENT LIST' \
      | awk '/cmd=unsubscribe/ && /sub=0 / && / psub=0 /' \
      | awk '{for(i=1;i<=NF;i++){p=index($i,"=");f[substr($i,1,p-1)]=substr($i,p+1)} print f["id"], f["addr"], "db="f["db"], "age="f["age"], "idle="f["idle"]}' \
      | sort -t= -k3 -n
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions