Skip to content

fix(daemon): end a FLOOD_WAIT wait when the daemon stops - #74

Merged
neoromantic merged 6 commits into
mainfrom
fix/daemon-stop-flood-wait-64
Oct 22, 2026
Merged

neoromantic merged 6 commits into
mainfrom
fix/daemon-stop-flood-wait-64

Conversation

@neoromantic

@neoromantic neoromantic commented Oct 22, 2026 •

Copy link
Copy Markdown
Owner

What and why

tg daemon stop failed with DAEMON_SHUTDOWN_TIMEOUT after its 10 s timeout when the daemon was waiting out a FLOOD_WAIT (up to 5 minutes) in its dialog pass or a sync job. mtcute's flood waiter sleeps inside the request, and the stop flag was only checked after that request returned, so the daemon kept running until the wait ended.

Fixes #64

Fix

  • A stop (SIGTERM, SIGINT or Daemon.stop()) now goes through requestShutdown, which sets the flag and aborts ctx.runtime.shutdown with a DaemonStopError.
  • The dialog pass lists dialogs through client.withParams({ abortSignal }), and each sync job's messages.getHistory passes the same signal as its per-call abortSignal. mtcute ends the flood sleep with the signal's reason. Nothing in mtcute is patched and no client is torn down. fetchMessagesRaw refuses to start once the signal is aborted, because mtcute's sleep never wakes on a signal that is already aborted.
  • A pass the stop cut short counts as stopped and marks nothing. A job the stop cut short gets a new stopped outcome: it is pending again with the attempts, last error and next_run_at it had before it started, and it is logged at debug as Job N left pending: the daemon is stopping. Only DaemonStopError counts as a stop; any other error during a stop is handled as before.
  • The main loop ends right after a job that the stop cut short, with no health-check getMe, refresh, status tick or 1 s wait.
  • The floodWaiter config moved into an exported daemonNetworkMiddlewares(ctx, accountId) with no change in behaviour, so the contract test runs against the daemon's real config.
  • Docs: docs/daemon.md (shutdown bullet: the tg daemon stop waits out a FLOOD_WAIT in the dialog pass or a sync job, past its 10 s timeout #64 caveat now names only the waits that still hold a stop, says a reconnect's wait ends when the shutdown closes its client, and keeps what DAEMON_SHUTDOWN_TIMEOUT, a longer --timeout and --force do), docs/sync-strategy.md (Retries), docs/rate-limiting.md, docs/database-schema.md and docs/testing.md.

How I checked it

  • bun run check, bun run test (1745 pass) and bun run test:e2e (255 pass).

  • New src/__tests__/daemon-stop.test.ts runs createDaemon().start() with fake clients and the real dialog pass, scheduler, loop and shutdown. A stop during a FLOOD_WAIT in the startup pass, or in a sync job, makes start() resolve to Success within 1 s of the stop, below the fake wait's 1.5 s guard (with Daemon.stop() setting only the flag, both tests fail on that bound). Nothing goes to console.warn or console.error, and the job row stays pending with its attempts, last error and due time.

  • Contract tests on mtcute 0.32.6:

    • daemonNetworkMiddlewares over a final step that returns FLOOD_WAIT_300 rejects with a DaemonStopError within 200 ms of the stop.
    • A real TelegramClient (in memory, no network) passes the same signal object to messages.getDialogs through withParams(...).iterDialogs(...).
  • Unit tests:

    • The startup and reconnect passes stop during a FLOOD_WAIT.
    • forward_catchup, backward_history and initial_load jobs leave their row, cursors, history ranges, messages and rate limits unchanged.
    • A job on the worker that a reconnect rebuilt gets the stop signal and stays as it was.
    • A stopped job whose chat was turned off while it ran is dropped, not left pending.
    • Earlier attempts are kept.
    • A non-stop error during a stop still retries or defers.
    • A real-scheduler restart runs the same job ID to completion.
    • The main loop breaks out on a 60th tick.
    • requestShutdown, the SIGTERM handler and fetchMessagesRaw.
  • The live integration test daemon connects, bootstraps dialogs, syncs history and stops cleanly now also checks that no job is left running after tg daemon stop and that the daemon's output contains no DaemonStopError or AbortError. I ran it against a temp copy of the test data dir (timeout 540 bun --env-file=.env test src/__integration__ -t 'daemon connects'): 1 pass, 14 expects, 37 s, so the signalled withParams(...).iterDialogs and messages.getHistory and the stop behave normally on the real client. A real FLOOD_WAIT can't be forced live, so the offline tests above are the proof for the wait itself.

  • Tests pass locally and use made-up data only

  • README, docs and affected demos follow the product content standard

  • Title follows the repo's commit style: Conventional Commits (feat: …, fix: …) unless AGENTS.md says otherwise

Out of scope (possible follow-ups)

  • A FLOOD_WAIT inside client.start() or getMe during the startup connect, or in the health check's getMe, still holds a stop until it ends. One in a reconnect doesn't: the health check starts the reconnect without waiting for it, and the shutdown closes that client, which ends mtcute's flood sleep through the client's teardown signal.
  • A message on a second stop signal, and tracking background work (such as a reconnect's dialog pass) in the shutdown cleanup.

🤖 Generated with Claude Code

neoromantic and others added 6 commits October 22, 2026 16:45
A daemon stop waited out a FLOOD_WAIT that its dialog pass or a sync job
was sleeping through inside mtcute's flood waiter, up to five minutes, so
tg daemon stop gave up after its 10 s timeout with DAEMON_SHUTDOWN_TIMEOUT
while the daemon kept running until the wait ended.

The stop now aborts a signal that the dialog pass (through withParams)
and each sync job's messages.getHistory pass as the per-call abortSignal.
mtcute ends the flood sleep with that signal's reason, so nothing in
mtcute is patched and no client is torn down to cut the wait short. The
pass counts as stopped and marks nothing; the job goes back to pending
with its attempts, last error and due time as they were, and the main
loop ends right after it, with no health check or status tick. Neither
is logged as an error.

Closes #64

Co-Authored-By: Claude Opus 5.5 <[email protected]>
…out does

The shutdown bullet lost its only description of DAEMON_SHUTDOWN_TIMEOUT
when the #64 caveat went, and it read as if every stop were prompt. A
FLOOD_WAIT in a connect, a reconnect or the health check's getMe still
waits out mtcute's flood waiter with no signal, and closing the clients
may take up to 30 s, so tg daemon stop can still give up after its 10 s
default. The bullet now names those waits, says what the command reports
then and what a longer --timeout does, and says that --force kills the
daemon only after the timeout, as src/commands/daemon.ts does, rather
than at once.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
waitLikeFloodWait rejects after 1500 ms when its signal never aborts, so
the 2000 ms bound in the daemon-stop tests held even when the stop didn't
cut the wait short: with Daemon.stop() setting only the flag, both tests
still ended about 1500 ms after the stop and failed only on unrelated
assertions. At 1000 ms, as the bootstrap and scheduler tests use against
the same guard, the bound fails on its own (checked with that change).

Co-Authored-By: Claude Opus 5.5 <[email protected]>
…ses its client

The shutdown bullet listed a reconnect among the FLOOD_WAITs that still
hold a stop. The health check starts a reconnect without waiting for it,
so the main loop still exits on a stop, and the shutdown then closes the
new client; mtcute's flood waiter listens to the client's teardown
signal, so that wait ends there. Only the startup connect and the health
check's getMe still hold the stop.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
… rebuilt

After a reconnect every job on the account runs on the worker that
rebuildSyncWorker builds, but no test checked that it gets the daemon's
stop signal: building it with a signal of its own still passed the whole
suite. The new case runs a job on the rebuilt worker during a FLOOD_WAIT
and checks that the request gets the stop signal and the job stays as it
was.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
A job the stop cut short is dropped, not left pending, when its chat was
turned off while it ran, but no test covered it: reporting it as stopped
for a row that was deleted still passed the suite. The table of jobs
whose chat was turned off now includes a daemon stop.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@neoromantic
neoromantic merged commit 7915e60 into main Oct 22, 2026
5 checks passed
@neoromantic
neoromantic deleted the fix/daemon-stop-flood-wait-64 branch October 22, 2026 13:49
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.

tg daemon stop waits out a FLOOD_WAIT in the dialog pass or a sync job, past its 10 s timeout

1 participant