Repository navigation
Fix chord errback given as a single signature never running (#10906) - #11107
Conversation
chord_error_from_stack passed the body's link_error option to the fake
request unchanged. When the errback was set as a single signature
(.set(link_error=eb.s())) that value is one serialized signature, and
_call_task_errbacks iterated it -- publishing one bogus task per dict
key ('task', 'args', 'kwargs', ...) and never calling the errback.
Wrap it with maybe_list, as the group-body path already does.
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #11107 +/- ##
=======================================
Coverage 91.28% 91.28%
=======================================
Files 153 153
Lines 21824 21824
Branches 2710 2710
=======================================
+ Hits 19921 19922 +1
Misses 1603 1603
+ Partials 300 299 -1
Flags with carried forward coverage won't be shown. Click here to find out more. ☔ View full report in Codecov by Harness. |
auvipy
left a comment
There was a problem hiding this comment.
can you check this pr and see if anything can be incorporate from it https://github.com/celery/celery/pull/10907/changes ?
ChristianMurphy
left a comment
There was a problem hiding this comment.
Looks good, with one addition to the tests. I ran it at 1fd5d3ace against main (a2d470f19).
The fix does what #10906 asks, and I did not find a regression. I gave the errback once as one signature and once as a list, and counted the cases where the two forms behave differently:
| Worker | Backend | main | This PR |
|---|---|---|---|
| solo, in-memory broker | cache | 4 of 15 | 0 of 15 |
| solo, in-memory broker | Redis | 4 of 15 | 0 of 15 |
| solo, in-memory broker | SQLite database | 4 of 15 | 0 of 15 |
| prefork, Redis broker | Redis | 6 of 10 | 0 of 10 |
| threads, Redis broker | Redis | 6 of 10 | 0 of 10 |
| eager mode | Redis | 0 of 10 | 0 of 10 |
In every differing case on main the errback runs 0 times and 6 messages go out for task names that do not exist. The cases are a simple body, a chain body and a chord inside a chain, each with .set(link_error=sig), with new-style, old-style and immutable errbacks. Every other row of every table is the same on main and here, so only the broken form changes.
On #10907, which @auvipy asked about: it changed two places. The second, for a chord with a group body, is on main since #10778 (_handle_group_chord_error() reads link_error through maybe_list()), so the line here is the only code still needed. What #10907 had and this PR does not is a test for the list form as well.
That is the addition I'd ask for. The new test only gives the errback as one signature. I planted a bug that always wraps the option in a list, which breaks the list form and a body with no errback, and all 199 tests in t/unit/backends/test_base.py still passed. The unit test below fails on that bug.
Unit test for t/unit/backends/test_base.py
@pytest.mark.parametrize('link_error,called', [
('single', ['errb']),
('list', ['errb']),
('two', ['errb', 'errb2']),
('none', []),
])
def test_chord_error_from_stack_errback_forms(self, link_error, called):
# link_error as one signature, a list, a list of two, or not set.
errb, errb2 = signature('errb'), signature('errb2')
options = {'task_id': 'body-id'}
if link_error != 'none':
options['link_error'] = {
'single': errb, 'list': [errb], 'two': [errb, errb2],
}[link_error]
callback = signature('body', options=options)
sent = []
class RecordingGroup:
def __init__(self, sigs, app=None):
sent.extend(sig.task for sig in sigs)
def apply_async(self, *args, **kwargs):
pass
with patch.object(self.b, 'store_result'), \
patch('celery.backends.base.group', RecordingGroup):
try:
raise ValueError('header failed')
except ValueError as exc:
self.b.chord_error_from_stack(callback, exc=exc)
assert sent == called| The PR's test | This test | |
|---|---|---|
| This PR | passes | 4 of 4 pass |
| The fix reverted | fails | the single form fails |
| The option always wrapped in a list | passes | list, two and unset fail |
link read in place of link_error |
fails | single, list and two fail |
An integration test would be welcome too, since the existing chord errback tests in t/integration/test_canvas.py all go through chord.link_error(), which never stores a single signature. This one goes in test_chord. With the fix reverted it fails after the 60 s wait for the errback; here it passes in 9 s, for both errback styles, on a Redis broker and backend.
Integration test for t/integration/test_canvas.py
@pytest.mark.parametrize(
"errback_task", [errback_old_style, errback_new_style, ],
)
def test_errback_given_as_one_signature_called_by_chord(
self, errback_task, manager, subtests
):
# link_error set to a single signature, not a list (#10906).
if not manager.app.conf.result_backend.startswith("redis"):
raise pytest.skip("Requires redis result backend.")
redis_connection = get_redis_connection()
for msg, body in (
("a simple body", identity.s()),
("a chain body", chain(identity.s(), identity.s())),
):
body.set(link_error=errback_task.s())
chord_sig = chord((fail.s(),), body)
expected_redis_key = chord_sig.body.freeze().id
redis_connection.delete(expected_redis_key)
with subtests.test(msg=f"Error propagates with {msg}"):
res = chord_sig.delay()
with pytest.raises(ExpectedException):
res.get(timeout=TIMEOUT)
with subtests.test(msg=f"Errback is called once with {msg}"):
await_redis_count(1, redis_key=expected_redis_key)
redis_connection.delete(expected_redis_key)For the smoke suite, a case in t/smoke/tests/test_canvas.py with the same chord would cover it on a worker in a container. I have not written or run that one: I had no Docker here.
The cases, overhead, and what I did not run
- The 15 solo-worker cases:
body.set(link_error=X),chord.link_error(X),chord.apply_async(link_error=X),chord.set(link_error=X), a group body with.set(link_error=X), a group body with it on each task, a chain body with.set(link_error=X), a chain body with it on the first task, a body that fails itself, a plain task as a control, andbody.set(link_error=X)followed by.link_error(second), all with a new-style errback; then the simple body and the group body with an old-style and with an immutable errback. - The 10 cases on prefork and threads: a simple body, a chain body, a group body,
chord.link_error(X)and a chord inside a chain, each with a new-style and an old-style errback. - Overhead: the changed expression takes 0.39 µs where main's takes 0.12 µs, once per failed chord.
t/unit/backends/test_base.py,t/unit/tasks/test_canvas.py,t/unit/tasks/test_chord.pyandt/unit/app/test_builtins.pyas tox runs them (python -bb): 656 passed.flake8andisortare clean with both tests added.- Not run: RabbitMQ, the gevent and eventlet pools, the full integration and smoke suites, and Python versions other than 3.13.
This comment was marked as abuse.
This comment was marked as abuse.
ChristianMurphy
left a comment
There was a problem hiding this comment.
Looks good now, thanks for the quick turnaround. I ran 17751e92b.
- The commit only adds tests. The fix is unchanged from what I reviewed.
t/unit/backends/test_base.pyas tox runs it (python -bb): 203 passed, with the 4 new cases among them.- The integration test, which you could not run: on a Redis broker and backend it passes for both errback styles in 8 s. With the fix reverted it fails after the 60 s wait for the errback.
flake8andisortare clean on both test files.
Problem
Fixes #10906.
A chord error callback attached as a single signature —
— never runs when the header fails.
KeyValueStoreBackend.chord_error_from_stack()passed the body'slink_erroroption to the fake request unchanged. With a single signature that value is one serialized signature (a dict), and_call_task_errbacks()iterates it — publishing one bogus task per dict key (task,args,kwargs,options,subtask_type,chord_size,immutable) instead of calling the errback. (The group-body path,_handle_group_chord_error(), already normalizes withmaybe_list(); the plain-body path did not.)Fix
In
chord_error_from_stack(), build the fake request witherrbacks=maybe_list(callback.options.get("link_error")) or []— the same normalization the rest ofcelery/backends/base.pyalready applies tolink_error.Tests
Reproduction (standalone script against unpatched
main): the tasks published for the errback were['task', 'args', 'kwargs', 'options', 'subtask_type', 'chord_size', 'immutable']; with the fix, exactly['errb'].New regression test
test_chord_error_from_stack_calls_a_single_signature_errbackint/unit/backends/test_base.py: fails without the fix, passes with it. Full file with the fix: 199 passed. 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.