Skip to content

Keep a cancelled task unacked when the child reports SystemExit - #10667

Merged
auvipy merged 5 commits into
celery:mainfrom
GangEunzzang:fix/cancelled-task-systemexit-ack
Sep 18, 2026
Merged

auvipy merged 5 commits into
celery:mainfrom
GangEunzzang:fix/cancelled-task-systemexit-ack

Conversation

@GangEunzzang

Copy link
Copy Markdown
Contributor

Follow-up to the investigation in #10659.

With Billiard 4.3.0 the test_reset_visibility_timeout smoke tests fail: after worker: Cold shutdown (MainProcess) the worker never logs Restoring 1 unacknowledged message(s). The worker isn't stuck. It exits normally, and the task that was still running has been acknowledged and dropped instead of being requeued.

Cold shutdown cancels the active request by sending SIGTERM to the child running it, and Billiard's signal handler in the child raises SystemExit. Up to Billiard 4.3.0rc1 that killed the child, the job came back as Terminated, and Request.on_failure returned early for it, so the message stayed unacked and kombu restored it on close. Since celery/billiard#452 the worker loop catches BaseException, so the same SystemExit is marshaled back as an ordinary task failure. on_failure then takes the acks_late branch, acks_on_failure is on by default, and the message gets acked. Bisecting Billiard between rc1 and 4.3.0 lands on that commit.

This change treats a SystemExit/KeyboardInterrupt result like Terminated when the request was cancelled or revoked by the worker itself. A task that exits on its own is still a failure and goes through the usual path.

Checked with Billiard 4.3.0 pinned: the five failing test_reset_visibility_timeout cases pass, the full t/smoke/tests/test_worker.py passes, and t/unit/worker is unchanged apart from the three new tests. Also reproduced and re-checked on macOS with a plain prefork worker against Redis, where the in-flight message now comes back to the queue after a cold shutdown.

Fixes the smoke failures blocking #10659.

Cold shutdown cancels the running request by sending SIGTERM to the
child, whose Billiard signal handler raises SystemExit. Up to Billiard
4.3.0rc1 that killed the child and the job came back as Terminated,
which on_failure leaves unacked so the broker redelivers it. Since
celery/billiard#452 the worker loop catches BaseException, so the same
SystemExit is marshaled back as an ordinary task failure and the
acks_late branch acknowledges it: the task is dropped instead of
requeued, and the visibility-timeout smoke tests wait forever for
"Restoring 1 unacknowledged message(s)".

Treat a SystemExit or KeyboardInterrupt result the same way as
Terminated when the request was cancelled or revoked by the worker
itself. A task that exits on its own is still a failure.
@codecov

codecov Bot commented Sep 18, 2026 •

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 95.23810% with 1 line in your changes missing coverage. Please review.
✅ Project coverage is 89.13%. Comparing base (c61f789) to head (b6b3557).
⚠️ Report is 1 commits behind head on main.

Files with missing lines Patch % Lines
celery/worker/request.py 95.23% 0 Missing and 1 partial ⚠️
Additional details and impacted files
@@           Coverage Diff           @@
##             main   #10667   +/-   ##
=======================================
  Coverage   89.12%   89.13%           
=======================================
  Files         153      153           
  Lines       20257    20277   +20     
  Branches     2395     2401    +6     
=======================================
+ Hits        18055    18074   +19     
  Misses       1898     1898           
- Partials      304      305    +1     
Flag Coverage Δ
unittests 89.11% <95.23%> (+<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 left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

test task smoke tests are failing here

@GangEunzzang

Copy link
Copy Markdown
Contributor Author

test task smoke tests are failing here

Looking into it now!

@auvipy

auvipy commented Sep 18, 2026 •

Copy link
Copy Markdown
Member

merged the other integration test upgrade and also merged billiard 4.3 to main branch to find out all compatibility issues. only in dev billiard 4.3rc1 remains, which we can change after this task tests are working

With Billiard >= 4.3.0 a task calling sys.exit() no longer kills the
child before the result is reported: the SystemExit comes back as a
failure result and the child exits right after. on_failure then tried
to store the SystemExit, which the JSON serializer rejects, so no
result was ever written and callers waited forever. The termination
smoke tests ran into their job timeout on exactly that.

Report it as WorkerLostError, as before: the process is gone either
way, and a task cannot fail with SystemExit.
@GangEunzzang

Copy link
Copy Markdown
Contributor Author

@auvipy The test_tasks.py job wasn't failing — it was hanging in test_task_termination[SYSTEM_EXIT] until the 60-minute job timeout. This comes from the same Billiard change.

A task calling sys.exit(1) used to kill the child and come back as WorkerLostError. With 4.3.0, the SystemExit is reported as the result first, so on_failure tries to store it. The JSON serializer rejects it (Object of type SystemExit is not JSON serializable), no result is written, and the test's .get() waits indefinitely.

It didn't show up on main because test_worker.py fails first there, which cancels the remaining smoke jobs.

I pushed a second commit that reports this as WorkerLostError like before. The child exits either way — Billiard exits it immediately after reporting the result — and a task can't meaningfully fail with SystemExit. With both commits and 4.3.0 pinned, test_task_termination and the test_worker.py shutdown tests pass locally, and both smoke jobs are green now.

The remaining red check is docker-build: requirements/dev.txt still has [email protected] from #10662, while default.txt now pins ==4.3.0, so pip can't resolve both requirements together. Happy to drop the rc1 pin here as well if you want that included in this PR.

@auvipy

auvipy commented Sep 18, 2026

Copy link
Copy Markdown
Member

yes I already opened a pr to change that wait a min

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.

2 participants