Skip to content

Idle worker doesn't stop on SIGTERM without uvloop #722

Open

Description

@AlexeyShalaev

On the default asyncio loop (no uvloop), a worker that is idle waiting for a message doesn't react to SIGTERM. The process manager forwards the signal, the worker logs Got signal 2., and then it keeps waiting until it gets killed, so broker.shutdown() and the shutdown events never run.

interrupt_handler in taskiq/cli/worker/run.py is registered with signal.signal and calls shutdown_event.set(). That only queues the event's waiters on the loop and doesn't wake it. A loop with no timers stays in epoll_wait until some I/O arrives. uvloop handles signals differently, which is why this only shows up without it.

Repro on taskiq 0.13.0, Python 3.12, no uvloop, with a broker whose listen() just waits on a pipe nobody writes to:

import asyncio, os
from taskiq import AsyncBroker

class IdleBroker(AsyncBroker):
    async def kick(self, message):
        pass

    async def listen(self):
        r, _ = os.pipe()
        loop = asyncio.get_running_loop()
        fut = loop.create_future()
        loop.add_reader(r, fut.set_result, None)
        await fut
        yield b""

broker = IdleBroker()

taskiq worker idlebroker:broker --workers 1, then kill -TERM the main process: the worker is still running 15 s later. Same with taskiq-redis ListQueueBroker against a real Redis with socket_timeout=None: still running 20 s after SIGTERM.

Calling loop.call_soon_threadsafe(shutdown_event.set) in the handler instead (with the loop created before the handlers are installed) makes the worker shut down right away in both cases, and broker.shutdown() runs.

This may be what #63 is about. I can send a PR with that change and a test if you'd like.

Activity

  1. z22092 commented on Nov 23, 2026

    @z22092

    Same cause with taskiq-postgresql's PostgresqlBroker before it started polling (taskiq 0.13.0, no uvloop, PostgreSQL 18.4, taskiq worker app:broker --workers 2, SIGINT once both workers listen): with asyncpg and psqlpy the workers were still running 15 s after the signal on Python 3.10, 3.13 and 3.14 (forkserver), asyncpg stopping only when its pool's idle timer fired about 300 s later, while psycopg exited within 0.15 to 1.13 s because its async waits wake the loop every 0.1 s. A loop.call_later(1.0, ...) timer re-armed every second inside listen() made all three drivers exit within 2.2 s.

  2. AlexeyShalaev commented on Nov 23, 2026

    @AlexeyShalaev
    Author

    Thanks for measuring it across drivers, that fits the cause: the signal handler runs, but the loop only gets to it when something else wakes it up, so psycopg's 0.1 s waits hide the bug and asyncpg's pool timer makes it 300 s.

    #724 wakes the loop from the signal handler itself, so it should cover PostgresqlBroker without the extra timer. If you have a minute to try the branch on your setup:

    pip install "taskiq @ git+https://github.com/AlexeyShalaev/taskiq@fix/worker-signal-wakeup"
    
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions