Skip to content

fix(worker): don't crash when stopping an exited worker on Windows - #718

Open
Wayfarer545 wants to merge 1 commit into
taskiq-python:masterfrom
Wayfarer545:fix/windows-shutdown-permission-error
Open

Wayfarer545 wants to merge 1 commit into
taskiq-python:masterfrom
Wayfarer545:fix/windows-shutdown-permission-error

Conversation

@Wayfarer545

Copy link
Copy Markdown

On Windows, Ctrl+C can crash the main process with PermissionError: [WinError 5] Access is denied and exit code 1.

The console delivers Ctrl+C to the workers too, so an idle worker has often exited by the time _shutdown_workers calls os.kill. On Windows os.kill means TerminateProcess, which fails this way for a process that has already exited. CPython handles the same case in Popen.terminate.

This change treats PermissionError like the existing ProcessLookupError branch.

Related to #422: same traceback, but the cause here isn't user permissions.

If several workers are running, the exception used to abort the loop, so workers after the exited one were left to finish their shutdown. Now they get os.kill too, as they already do when no worker has exited. On Windows that terminates them right away. That existing behavior is not changed here.

Tested on Windows 11, Python 3.13, with a real Ctrl+C in a new console (idle broker with 1 and 2 workers, ZeroMQBroker with tornado):

  • before: crash in 24 of 24 runs
  • after: exit code 0 and no traceback in 32 of 32 runs

The new test fails on Windows without the fix and passes with it on 3.10, 3.13 and 3.14. On Linux it passes either way, through the ProcessLookupError branch, so only the Windows CI job catches a regression.

@codecov

codecov Bot commented Nov 7, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 86.69%. Comparing base (f08a54a) to head (23dbd79).
⚠️ Report is 1 commits behind head on master.

Additional details and impacted files
@@            Coverage Diff             @@
##           master     #718      +/-   ##
==========================================
+ Coverage   85.48%   86.69%   +1.20%     
==========================================
  Files          68       68              
  Lines        2660     2660              
==========================================
+ Hits         2274     2306      +32     
+ Misses        386      354      -32     

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

This branch has not been deployed

No deployments
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.

2 participants