Skip to content

fix: cancellation propagates from run_maintenance_safe - #110

Merged
AlexeyShalaev merged 1 commit into
masterfrom
fix/107-cancellation-propagates
Oct 16, 2026
Merged

AlexeyShalaev merged 1 commit into
masterfrom
fix/107-cancellation-propagates

Conversation

@AlexeyShalaev

Copy link
Copy Markdown
Member

Closes #107.

  • run_maintenance_safe no longer catches asyncio.CancelledError, KeyboardInterrupt or SystemExit (aio), nor KeyboardInterrupt or SystemExit (sync). run_maintenance logs them before re-raising, as before. Every Exception still goes to result.error.
  • The Redis lock's watchdog stops a run whose lease it lost by cancelling the holder. It now records why. The lock scope catches that CancelledError, calls uncancel(), and raises the new LockLostError (a PartitionError, exported from pg_partsmith) when its cancellation is the only one pending. So a lost lease still lands on result.error. When the task was also cancelled from outside, the CancelledError propagates. The sync lock never cancels its holder, so it doesn't change.
  • Docstrings, docs/concepts/execution.md, the scheduling guide, the production page and docs/agents.md no longer say the safe variant "never raises". They now say that errors land on the result and that cancellation propagates.

Tests:

  • asyncio.timeout(0.05) around a slow run raises TimeoutError.
  • task.cancel() leaves the task cancelled.
  • CancelledError, KeyboardInterrupt and SystemExit propagate (both mirrors).
  • A lost lease ends the scope with LockLostError, and the task's cancelling() is back to 0.
  • A lost lease together with an outside cancel ends cancelled.
  • The existing maintainer test with a real watchdog now expects LockLostError on result.error.

The tests that pinned the old swallowing were rewritten.

Results: unit 2876 passed, integration (PG 17) 575 passed, ruff and mypy clean, docs build clean.

Who notices: code that ran run_maintenance_safe under a timeout or a cancelling supervisor and relied on getting a result back. That is the behaviour #107 asks to fix, so this ships as a fix.

@codecov

codecov Bot commented Oct 16, 2026

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 94.11765% with 1 line in your changes missing coverage. Please review.

Files with missing lines Patch % Lines
pg_partsmith/aio/lock/redis.py 91.66% 1 Missing ⚠️

📢 Thoughts on this report? Let us know!

run_maintenance_safe caught CancelledError, KeyboardInterrupt and SystemExit and returned them on result.error, so asyncio.timeout() never fired, a cancelled task did not end cancelled, and Ctrl+C in the sync mirror was swallowed. They now propagate from both mirrors; every Exception still lands on result.error.

The Redis lock stops a run whose lease it lost by cancelling it. It now takes that cancellation back and raises LockLostError, so a lost lease is still reported on the result; a cancellation from outside at the same time still propagates.

Closes #107
@AlexeyShalaev
AlexeyShalaev force-pushed the fix/107-cancellation-propagates branch from 5a4b662 to 008457d Compare October 16, 2026 15:05
@AlexeyShalaev
AlexeyShalaev merged commit fd857b4 into master Oct 16, 2026
31 of 32 checks passed
@AlexeyShalaev
AlexeyShalaev deleted the fix/107-cancellation-propagates branch October 16, 2026 16:02
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

run_maintenance_safe swallows asyncio.CancelledError, so asyncio.timeout() and task.cancel() do not stop it

1 participant