[Bug]: After heartbeat completes, chat lock stays held — every user message stuck in queue, /stop reports killed=0/discarded=0
Version
ductor 0.16.0 (installed via pip install --no-cache-dir ductor in Docker, Python 3.12-slim).
Environment
- Transport: Telegram
- Provider: Claude Code (
coordinatoragent) - Heartbeat enabled,
interval_seconds: 300 - Single allowed user, single private chat (no topics)
- Running inside Docker container as PID 1 (entrypoint:
python -c "from ductor_bot.__main__ import _start_bot; _start_bot(verbose=False)")
Symptom
After a heartbeat job completes successfully, the chat becomes permanently "busy":
- Every subsequent user message gets the
[Message in queue...]indicator and never executes. /stopis acknowledged but reportskilled=0anddiscarded=0— i.e. nothing is actually running and the queue is empty, yetis_busyis still true for the chat./newbehaves the same — gets queued, never starts a session.- No new
CLI startinglines appear in the logs after the heartbeat finishes.
The bot recovers only after a process restart.
Reproducible log excerpt
Heartbeat completes normally:
13:09:53 INFO ductor_bot.cli.claude_provider: [main:hb:<chat>:<sid>] CLI wrapper: cwd=/root/.ductor/workspace, model=opus
13:09:53 INFO ductor_bot.cli.service: [main:hb:<chat>:<sid>] CLI execute starting label=main model=opus
13:09:53 INFO ductor_bot.cli.executor: [main:hb:<chat>:<sid>] CLI subprocess starting pid=4019
13:10:35 INFO ductor_bot.cli.claude_provider: [main:hb:<chat>:<sid>] CLI done session=<sid> turns=11 cost=$0.2351 tokens=1519 duration_ms=41790
13:10:35 INFO ductor_bot.cli.service: [main:hb:<chat>:<sid>] CLI main [ok] cost=$0.2351 tokens=1519 duration_ms=42847
13:10:36 INFO ductor_bot.orchestrator.flows: [main:hb:<chat>:<sid>] Heartbeat alert chars=476
13:10:36 INFO ductor_bot.messenger.telegram.transport: [main:hb:<chat>:<sid>] Heartbeat delivered
Then user sends /new, /stop, and free-text — every one queues / aborts to no-op:
13:10:50 INFO ductor_bot.messenger.telegram.app: [main:cb:<chat>] Callback data=mq:2
13:10:50 INFO ductor_bot.messenger.telegram.middleware: [main:cb:<chat>] Queue entry cancelled chat=<chat> entry=2
13:11:15 INFO ductor_bot.messenger.telegram.handlers: [main:msg:<chat>] Abort requested killed=0
13:11:15 INFO ductor_bot.messenger.telegram.middleware: [main:msg:<chat>] Queue drained chat=<chat> discarded=0
13:11:25 INFO ductor_bot.messenger.telegram.handlers: [main:msg:<chat>] Abort requested killed=0
13:11:25 INFO ductor_bot.messenger.telegram.middleware: [main:msg:<chat>] Queue drained chat=<chat> discarded=0
... (mq:3, mq:4, mq:5, mq:6, mq:7, mq:8, mq:10 — each /new gets queued and user manually cancels)
13:15:00 INFO ductor_bot.cron.observer: [main:cron] Cron job starting job=Dez Executor — Trade Execution
13:15:57 INFO ductor_bot.cron.observer: [main:cron] Cron job completed ... status=success
13:16:04 INFO ductor_bot.messenger.telegram.app: [main:cb:<chat>] Callback data=mq:10
Cron jobs run normally (they don't use the chat lock), confirming the rest of the orchestrator is alive — only the chat is stuck.
Suspected cause
TelegramMiddleware.is_busy() is self._lock_pool.any_locked_for_chat(chat_id) or self.has_pending(chat_id) (messenger/telegram/middleware.py:228). Since drain_pending reports 0 entries and cancel_task killed 0 processes, the pending list is empty — so the chat lock from _lock_pool must still be held.
Hypothesis: the heartbeat path acquires (or shares) the chat's session lock to deliver the alert and Heartbeat delivered, but on some code path through v0.16 (possibly via the new memory_flush / memory_compaction hooks, or the new emoji status reaction, or the new cli_parameters resolution for background tasks) the async with lock is exited via an exception that isn't propagated to the caller's logs — leaving the lock acquired. After that, every user message sees lock.locked() == True forever, gets queued, and the queued coroutine never runs because nothing ever releases the lock.
This regressed in v0.16.0 — the same setup was stable on v0.15.0 across many heartbeat cycles.
What would help debug
- Add a
finally:log line in the middleware'sasync with lock:branch confirming lock release per session. - Log lock acquire/release for heartbeat and any other internal flow that touches the chat lock.
- Consider a watchdog: if
is_busy(chat)is true but pending queue is empty AND no task inTaskHubreferences that chat for >N seconds, force-release the lock and log a warning.
Happy to provide more logs / try a patch build.
Fixed in v0.16.1.
The fix has two parts:
- heartbeat turns now serialize through the shared per-session lock, so they cannot race normal Telegram ingress on the same chat/topic;
- post-stream memory flush/compaction is scheduled behind the same lock after the user turn returns, instead of being awaited while Telegram still holds the lock.
That should remove the state where no CLI process is running, the queue is empty, but the chat still appears permanently busy.
Release: https://github.com/PleasePrompto/ductor/releases/tag/v0.16.1
Thanks for the detailed logs; they made the root cause much easier to pin down.