taskiq icon indicating copy to clipboard operation
taskiq copied to clipboard

Unhandled KeyboardInterruptError from interrupt_handler

Open rtnpro opened this issue 1 year ago • 2 comments

Description

When running a taskiq worker that spawns subprocesses, KeyboardInterrupt exceptions are not being caught and handled as expected. This results in an unhandled exception traceback being printed when attempting to gracefully shut down the worker.

Steps to Reproduce

  1. Set up a taskiq worker that spawns subprocesses in its tasks
  2. Run the worker
  3. Send a SIGINT or SIGTERM to the worker process (e.g., by pressing Ctrl+C)

Expected Behavior

The worker should catch the KeyboardInterrupt, log a warning message, and proceed to shut down the broker gracefully.

Actual Behavior

An unhandled KeyboardInterrupt exception is raised, and a traceback is printed:

  File "taskiq/receiver/receiver.py", line 271, in run_task
    returned = await target_future
  File "myapp/tasks.py", line 94, in process_call
    spawn_bot(
  File "myapp/bot_spawner.py", line 115, in spawn_bot
    subprocess.check_call(
  File "subprocess.py", line 408, in check_call
    retcode = call(*popenargs, **kwargs)
  File "subprocess.py", line 391, in call
    return p.wait(timeout=timeout)
  File "subprocess.py", line 1264, in wait
    return self._wait(timeout=timeout)
  File "subprocess.py", line 2053, in _wait
    (pid, sts) = self._try_wait(0)
  File "subprocess.py", line 2011, in _try_wait
    (pid, sts) = os.waitpid(self.pid, wait_flags)
  File "taskiq/cli/worker/run.py", line 107, in interrupt_handler
    raise KeyboardInterrupt

Environment

  • taskiq dependencies' versions:
    • taskiq: 0.11.3
    • taskiq-dependencies: 1.5.3
    • taskiq-redis: 0.5.6
  • Python version: 3.12
  • OS: debian bookworm-slim
  • Command: taskiq worker myapp.worker:broker myapp.tasks --workers=4 --max-threadpool-threads=1 --max-async-tasks=1 --ack-type=when_executed --wait-tasks-timeout=600 --shutdown-timeout=630 --max-tasks-per-child=1000
  • Broker used: taskiq_redis.ListQueueBroker

Additional Context

The issue seems to occur because the KeyboardInterrupt is being raised while waiting for a subprocess to complete. The exception is propagating through the async call stack before it can be caught by the main try-except block in the start_listen function.

rtnpro avatar Oct 22 '24 17:10 rtnpro

@rtnpro this is probably related ? https://github.com/taskiq-python/taskiq-redis/issues/72

pySilver avatar Nov 08 '24 17:11 pySilver

Hello. I guess this issue can be closed. I forgot to write back, but since then the Pull request that introduces graceful shutdowns was merged and released in 0.11.14. Sorry for not letting you know.

The PR: https://github.com/taskiq-python/taskiq/pull/381

s3rius avatar Apr 04 '25 12:04 s3rius