Skip to content

fix(cli): correct --hardkill-count off-by-one in worker signal handler#648

Open
gitcommit90 wants to merge 1 commit into
taskiq-python:masterfrom
gitcommit90:fix/issue-647-hardkill-count-off-by-one
Open

fix(cli): correct --hardkill-count off-by-one in worker signal handler#648
gitcommit90 wants to merge 1 commit into
taskiq-python:masterfrom
gitcommit90:fix/issue-647-hardkill-count-off-by-one

Conversation

@gitcommit90

Copy link
Copy Markdown

Summary

Fixes #647: --hardkill-count N was off by one in the worker signal handler.

Docs/CLI help describe N as the number of termination signals allowed before a hard kill (hard kill on signal N+1). The handler compared hardkill_counter > args.hardkill_count before incrementing, so with the default N=3 the hard kill fired on the 5th signal instead of the 4th.

Changes

  • taskiq/cli/worker/run.py: change > to >= so hard kill runs on signal N+1.
  • Add tests/cli/worker/hanging_broker.py and tests/cli/worker/test_hardkill_count.py — integration test that spawns a real worker with a broker whose graceful shutdown hangs, sends SIGINTs to the worker child, and asserts survival through N signals and hard kill on N+1.

Test plan

  • New hardkill-count integration test fails on pre-fix code and passes with the fix
  • Full suite: pytest tests/ -q -n auto → 298 passed
  • ruff check, ruff format --check, black --check, mypy clean on touched files

Closes #647

The worker's interrupt handler compared hardkill_counter to
args.hardkill_count with '>' before incrementing, so N allowed
termination signals actually required N+2 signals to hard-kill
(e.g. the default --hardkill-count 3 hard-killed on the 5th signal).

Use '>=' so signal N+1 performs the hard kill, matching the CLI help
text: 'Number of termination signals to the main process before
performing a hardkill.'

Add an integration test that spawns a real worker with a broker whose
graceful shutdown hangs, sends SIGINTs to the worker child process,
and asserts the worker survives N signals and is hard-killed on
signal N+1. The test fails on the previous comparison and passes
with this fix.

Fixes taskiq-python#647

@GefMar GefMar 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.

A few notes on the signal semantics and test reliability.

Comment thread taskiq/cli/worker/run.py
# Hard kill is a signal that we should stop
# everything immediately.
if hardkill_counter > args.hardkill_count:
if hardkill_counter >= args.hardkill_count:

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.

Before changing this condition, could we clarify the contract? With this change, --hardkill-count 3 kills the worker on the fourth signal, while the docs say that sending three signals performs a hard kill. We should pick one interpretation and align the code, CLI help, docs, and test.

)
children = _worker_child_pids(proc.pid)
if children:
worker_pid = children[0]

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.

On macOS, the first child is usually multiprocessing.resource_tracker, not the worker. It ignores SIGINT, so this test will fail there. We need a reliable way to get the actual worker PID.

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.

What if in _worker_child_pids we will filter out all processes that are not python?

if worker_pid is None:
pytest.fail("No worker child process spawned in time")
# Extra settle time so the child's signal handlers are installed.
time.sleep(2.0)

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.

A fixed two-second sleep does not guarantee that the worker has installed its signal handlers, especially on a busy CI runner. Could we wait for an explicit readiness signal from the test broker instead?

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.

We already set asyncio.event in the broker. I suppose, we can reuse it to fit our needs. Like, we only start killing, when the event is set. Reseting in on every iteration.

)
finally:
if proc.poll() is None:
proc.kill()

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.

proc.kill() only stops the supervisor, so the hanging worker may be left running after a test failure. It would be safer to terminate the whole process group or explicitly clean up all child processes.

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.

--hardkill-count appears off by one

3 participants