Repository navigation
Fix Redis result consumer missing results stored during pub/sub reconnect (#11089) - #11108
Conversation
…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).
Codecov Report✅ All modified and coverable lines are covered by tests. 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
Flags with carried forward coverage won't be shown. Click here to find out more. ☔ View full report in Codecov by Harness. |
ChristianMurphy
left a comment
There was a problem hiding this comment.
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
afffb9847and main atbffee14b2, 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.pyin the3.13-unittox environment: 224 passed. With main'scelery/backends/redis.pyunder 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.
|
Added the integration test as suggested ( Verified locally against a real Redis: the test fails on main's |
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 — soAsyncResult.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
test__reconnect_pubsub_reads_meta_stored_during_subscribe: the meta lands in the store duringsubscribe(); asserts it is still delivered (task leaves the wait set). Fails without the fix, passes with it.test__reconnect_pubsub_without_state_changeupdated for the second read (mgetnow called twice per reconnect with subscriptions).t/unit/backends/test_redis.pywith 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.