Skip to content

Fix Redis result consumer missing results stored during pub/sub reconnect (#11089) - #11108

Merged
auvipy merged 5 commits into
celery:mainfrom
tony1233200:fix/11089-redis-reconnect-reread
Nov 28, 2026
Merged

auvipy merged 5 commits into
celery:mainfrom
tony1233200:fix/11089-redis-reconnect-reread

Conversation

@tony1233200

Copy link
Copy Markdown
Contributor

Problem

Fixes #11089.

ResultConsumer._reconnect_pubsub() (Redis result backend) reads the subscribed tasks' metas before it creates the new pubsub and subscribes. A result stored in the gap between that read and the subscribe taking effect is never seen by the waiting client: its publish was missed, and nothing reads the stored meta again — so AsyncResult.get() times out even though the result is sitting in Redis as SUCCESS. The reporter measured this under load: ~0.03% of 50k results in a chaos test, and provided a deterministic reproduction of the ordering.

Fix

Read the subscribed tasks' metas once more after subscribing, so anything stored during the reconnect window is picked up from the store even though its publish was missed. Cost: one extra MGET per reconnect. The meta-read is factored into a small _read_subscribed_metas() helper used for both reads. (This is the reporter's preferred of the two fixes they measured; the subscribe-first alternative invalidates more existing tests.)

Tests

  • New regression test test__reconnect_pubsub_reads_meta_stored_during_subscribe: the meta lands in the store during subscribe(); asserts it is still delivered (task leaves the wait set). Fails without the fix, passes with it.
  • test__reconnect_pubsub_without_state_change updated for the second read (mget now called twice per reconnect with subscriptions).
  • Full file t/unit/backends/test_redis.py with the fix: 208 passed, 16 skipped. flake8 clean.

Disclosure: this change was prepared with the assistance of an AI coding tool; the diagnosis, reproduction and test results above were verified locally by running the test suite.

…nect

_reconnect_pubsub read the subscribed tasks' metas before creating
the new pubsub and subscribing. A result stored in the gap between
that read and the subscribe is never seen: its publish was missed
and nothing reads the stored meta again, so AsyncResult.get() times
out although the result sits in Redis as SUCCESS. Read the metas
once more after subscribing (one extra MGET per reconnect).
@auvipy
auvipy self-requested a review November 15, 2026 17:59
@auvipy auvipy added this to the 5.7.x milestone Nov 15, 2026
@auvipy

auvipy commented Nov 19, 2026

Copy link
Copy Markdown
Member

@MehrazRumman

@codecov

codecov Bot commented Nov 28, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 91.81%. Comparing base (ebfd0de) to head (1d0c2b8).
⚠️ Report is 1 commits behind head on main.
✅ All tests successful. No failed tests found.

Additional details and impacted files
@@           Coverage Diff           @@
##             main   #11108   +/-   ##
=======================================
  Coverage   91.80%   91.81%           
=======================================
  Files         153      153           
  Lines       22293    22296    +3     
  Branches     2794     2794           
=======================================
+ Hits        20467    20471    +4     
  Misses       1530     1530           
+ Partials      296      295    -1     
Flag Coverage Δ
unittests 91.79% <100.00%> (+<0.01%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

@auvipy

auvipy commented Nov 28, 2026

Copy link
Copy Markdown
Member

@ChristianMurphy

@ChristianMurphy ChristianMurphy left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good, with one addition before merge: an integration test. I ran afffb9847 against main (bffee14b2) with the script from #11089. It kills the client's pub/sub connection while a 2 s task runs on a prefork worker.

get(timeout=10) timed out with SUCCESS stored main This PR
Redis 7.4 7 of 8 0 of 8
Valkey 8.1 8 of 12 0 of 12

The new test and the updated one fail on main's redis.py. The unit tests also catch the second read moved before the subscribe.

The integration test is the addition, since the unit test stores the result through a mocked subscribe(). The one below runs without a worker, fails on main with TimeoutError, and passes here 20 of 20 times on Redis and on Valkey.

Test for t/integration/test_redis_backend.py (also needs import socket)
def test_result_stored_during_pubsub_reconnect_is_received(app):
    if not app.conf.result_backend.startswith('redis'):
        pytest.skip('Requires redis result backend.')

    backend = app.backend
    task_id = uuid()
    result = app.AsyncResult(task_id)
    backend.add_pending_result(result)
    mget = backend.client.mget
    stored = []

    def mget_then_store(keys):
        metas = mget(keys)
        if not stored:
            # The task ends after this read and before the new subscribe.
            backend.store_result(task_id, 42, states.SUCCESS)
            stored.append(task_id)
        return metas

    backend.client.mget = mget_then_store
    try:
        # The pub/sub connection is lost.
        pubsub = backend.result_consumer._pubsub
        pubsub.connection._sock.shutdown(socket.SHUT_RDWR)

        assert result.get(timeout=5) == 42
    finally:
        backend.client.mget = mget
        backend.forget(task_id)
What I ran
  • Celery at afffb9847 and main at bffee14b2, each with kombu main (9cd1cef8), redis-py 8.2.0, Python 3.13, prefork with 2 processes.
  • The script pauses for 3 s after the first read, as in the issue. The task ends between the read and the subscribe.
  • Without the kill, get() returned 3 of 3 times on both.
  • t/unit/backends/test_redis.py in the 3.13-unit tox environment: 224 passed. With main's celery/backends/redis.py under these tests, 2 fail.
  • The unit tests catch each of 4 changes to the new code: dropping either read, moving the second read before the subscribe, and passing on empty results.
  • One earlier run of this PR on Valkey timed out once in 8 while other test runs loaded the machine. The 28 runs after that all returned.

…ry#11089)

Covers the case where a task result is stored after the last read
and before the new subscribe, while the pub/sub connection is lost:
the result must still be received after the reconnect. Fails on
main with TimeoutError; passes with the fix.
@tony1233200

Copy link
Copy Markdown
Contributor Author

Added the integration test as suggested (test_result_stored_during_pubsub_reconnect_is_received in t/integration/test_redis_backend.py, plus the socket import).

Verified locally against a real Redis: the test fails on main's redis.py with TimeoutError and passes with the fix (5/5 runs); the full test_redis_backend.py file passes (9 passed) and t/unit/backends/test_redis.py passes (208 passed, 16 skipped).

@auvipy
auvipy merged commit acb1428 into celery:main Nov 28, 2026
22 of 23 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Redis result backend: a result stored during a pub/sub reconnect is never seen, and get() times out

4 participants