Repository navigation
fix(daemon): end a FLOOD_WAIT wait when the daemon stops - #74
Merged
Merged
Conversation
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]>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What and why
tg daemon stopfailed withDAEMON_SHUTDOWN_TIMEOUTafter 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
SIGTERM,SIGINTorDaemon.stop()) now goes throughrequestShutdown, which sets the flag and abortsctx.runtime.shutdownwith aDaemonStopError.client.withParams({ abortSignal }), and each sync job'smessages.getHistorypasses the same signal as its per-callabortSignal. mtcute ends the flood sleep with the signal's reason. Nothing in mtcute is patched and no client is torn down.fetchMessagesRawrefuses to start once the signal is aborted, because mtcute's sleep never wakes on a signal that is already aborted.stoppedoutcome: it is pending again with the attempts, last error andnext_run_atit had before it started, and it is logged at debug asJob N left pending: the daemon is stopping. OnlyDaemonStopErrorcounts as a stop; any other error during a stop is handled as before.getMe, refresh, status tick or 1 s wait.daemonNetworkMiddlewares(ctx, accountId)with no change in behaviour, so the contract test runs against the daemon's real config.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 whatDAEMON_SHUTDOWN_TIMEOUT, a longer--timeoutand--forcedo),docs/sync-strategy.md(Retries),docs/rate-limiting.md,docs/database-schema.mdanddocs/testing.md.How I checked it
bun run check,bun run test(1745 pass) andbun run test:e2e(255 pass).New
src/__tests__/daemon-stop.test.tsrunscreateDaemon().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, makesstart()resolve toSuccesswithin 1 s of the stop, below the fake wait's 1.5 s guard (withDaemon.stop()setting only the flag, both tests fail on that bound). Nothing goes toconsole.warnorconsole.error, and the job row stays pending with its attempts, last error and due time.Contract tests on mtcute 0.32.6:
daemonNetworkMiddlewaresover a final step that returnsFLOOD_WAIT_300rejects with aDaemonStopErrorwithin 200 ms of the stop.TelegramClient(in memory, no network) passes the same signal object tomessages.getDialogsthroughwithParams(...).iterDialogs(...).Unit tests:
forward_catchup,backward_historyandinitial_loadjobs leave their row, cursors, history ranges, messages and rate limits unchanged.requestShutdown, the SIGTERM handler andfetchMessagesRaw.The live integration test
daemon connects, bootstraps dialogs, syncs history and stops cleanlynow also checks that no job is leftrunningaftertg daemon stopand that the daemon's output contains noDaemonStopErrororAbortError. 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 signalledwithParams(...).iterDialogsandmessages.getHistoryand 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: …) unlessAGENTS.mdsays otherwiseOut of scope (possible follow-ups)
client.start()orgetMeduring the startup connect, or in the health check'sgetMe, 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.🤖 Generated with Claude Code