You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Idle worker doesn't stop on SIGTERM without uvloop #722
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:
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.
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.
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:
Activity
z22092 commentedon Nov 23, 2026
Same cause with taskiq-postgresql's
PostgresqlBrokerbefore 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. Aloop.call_later(1.0, ...)timer re-armed every second insidelisten()made all three drivers exit within 2.2 s.test(worker): check that taskiq worker stops while it waits for a mes…
AlexeyShalaev commentedon Nov 23, 2026
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
PostgresqlBrokerwithout the extra timer. If you have a minute to try the branch on your setup: