Repository navigation
Description
On the Redis result backend, get() can time out although the result is stored. After a lost pub/sub connection the client reads the pending results and only then subscribes again, and a result stored in between is never seen. The order is from #5921 (4.4.1), which added the reconnect. I'd read the results once more after subscribing, and I can send that PR.
Main (9c2f238), a 2 s task, the client's pub/sub connection killed after delay() |
get(timeout=10) |
|---|---|
| With a 3 s pause between the read and the subscribe | TimeoutError, stored state SUCCESS, 13 of 15 runs |
| The same without the kill | Returned the result, 5 of 5 |
| No pause, 200 tasks of 50 ms, the connection killed at a random moment in each | TimeoutError with SUCCESS stored for 2 of 200, then 0 of 200 |
Cause and the change I tried
ResultConsumer._reconnect_pubsub() reads the state of every task it waits for with MGET, then subscribes on a new connection (celery/backends/redis.py:256 and :271). A task that stores its result between the two publishes to nobody, and the read came before the result was there. The task has to finish within the few round trips of the reconnect, so it is rare without the pause. With 1 s of latency between the client and Redis the same order shows in MONITOR, and get() timed out in 2 of 2 runs.
Change in _reconnect_pubsub() |
get() in 6 runs of the script |
Existing tests in t/unit/backends/test_redis.py that change |
|---|---|---|
| Read once more after the subscribe | Returned the result in all 6 | 1 |
| Subscribe first, then read | Returned the result in all 6 | 4 |
The first is 7 lines and one more MGET per reconnect.
Checklist
Checklist
- I have verified that the issue exists against the
mainbranch of Celery. - This has already been asked to the discussions forum first.
- I have read the relevant section in the
contribution guide
on reporting bugs. - I have checked the issues list
for similar or identical bug reports. - I have checked the pull requests list
for existing proposed fixes. - I have checked the commit log
to find out if the bug was already fixed in the main branch. - I have included all related issues and possible duplicate issues in this issue (If there are none, check this box anyway).
- I have tried to reproduce the issue with pytest-celery and added the reproduction script below.
Mandatory Debugging Information
- I have included the output of
celery -A proj reportin the issue. - I have verified that the issue exists against the
mainbranch of Celery. - I have included the contents of
pip freezein the issue. - I have included all the versions of all the external dependencies required to reproduce this bug.
Optional Debugging Information
- I have tried reproducing the issue on more than one Python version and/or implementation.
- I have tried reproducing the issue on more than one message broker and/or result backend.
- I have tried reproducing the issue on more than one version of the message broker and/or result backend.
- I have tried reproducing the issue on more than one operating system.
- I have tried reproducing the issue on more than one workers pool.
- I have tried reproducing the issue with autoscaling, retries, ETA/Countdown & rate limits disabled.
- I have tried reproducing the issue after downgrading and/or upgrading Celery and its dependencies.
Related Issues and Possible Duplicates
Related Issues
- Handle Redis connection errors in result consumer #5921 added
_reconnect_pubsub(). - Operation timed out when using redis backend #4039 from 2017 reports
get()timing out although the task succeeded. It is older than this code.
Possible Duplicates
- None
Environment & Settings
Celery version: 5.7.0 (9c2f238)
celery report Output:
software -> celery:5.7.0 (collider) kombu:5.7.0 py:3.14.6
billiard:4.3.2 redis:8.1.0
platform -> system:Linux arch:64bit, ELF
kernel version:6.18.44-fc-v378 imp:CPython
loader -> celery.loaders.app.AppLoader
settings -> transport:redis results:redis://localhost:6379/1
broker_url: 'redis://:********@localhost:6379/0'
result_backend: 'redis://localhost:6379/1'
deprecated_settings: None
Steps to Reproduce
Required Dependencies
- Minimal Python Version: N/A or Unknown
- Minimal Celery Version: 4.4.1 by the code; I tested main only
- Minimal Kombu Version: N/A or Unknown
- Minimal Broker Version: Redis 7.4
- Minimal Result Backend Version: Redis 7.4
- Minimal OS and/or Kernel Version: N/A or Unknown
- Minimal Broker Client Version: N/A or Unknown
- Minimal Result Backend Client Version: N/A or Unknown
Python Packages
pip freeze Output:
billiard==4.3.2
celery @ main (9c2f238)
kombu @ main (445027e)
redis==8.1.0
Other Dependencies
Services
N/A
Minimally Reproducible Test Case
tasks.py, run.py and output
# tasks.py
import time
from celery import Celery
app = Celery("tasks", broker="redis://localhost:6379/0",
backend="redis://localhost:6379/1")
@app.task
def slow():
time.sleep(2)
return "done"# run.py
import time
import redis
from tasks import app, slow
res = slow.delay() # subscribes to the result channel
# Hold the window open: the task ends between the MGET and the SUBSCRIBE.
client = app.backend.client
mget = client.mget
def mget_then_wait(keys):
metas = mget(keys)
time.sleep(3)
return metas
client.mget = mget_then_wait
# The pub/sub connection is lost, as in a network blip.
redis.Redis().client_kill_filter(_type="pubsub")
start = time.monotonic()
try:
print("get() ->", res.get(timeout=10))
except Exception as exc:
print(f"get() -> {type(exc).__name__} after {time.monotonic() - start:.1f} s")
print("stored state:", app.backend.get_task_meta(res.id, cache=False)["status"])$ celery -A tasks worker -c 2 -l warning
$ python run.py
get() -> TimeoutError after 10.0 s
stored state: SUCCESSExpected Behavior
get() returns the result once it is stored.
Actual Behavior
get() waits until its timeout, or forever without one. A new AsyncResult(id).get() returns the result at once.
Activity
Add integration test for result stored during pub/sub reconnect (cele…
Fix Redis result consumer missing results stored during pub/sub recon…