Skip to content

Daemon loses writes queued between the stop signal and its shutdown #56

Closed

Description

@neoromantic

Found while fixing #34. When SIGTERM or SIGINT arrives (tg daemon stop, Ctrl-C), @mtcute/bun's exit hook (utils/exit-hook.js, registered by StorageManager and the sqlite driver when a session loads) saves and closes every session file right away, then sends the signal again. That's why the daemon logs "Received SIGTERM" twice. The daemon's own graceful shutdown runs a second or more later, after the main-loop sleep and any running sync job.

In that window the clients are still connected, but their storage is closed:

  • the updates loop can't save pts/qts after a batch, so it errors and restarts until the process exits, and the next start fetches that difference again;
  • peers seen by a running sync job aren't saved to the session file;
  • destroy() on shutdown fails with "Database has closed" when anything was queued (since Daemon doesn't close clients on shutdown or after a failed first connect #34 that's logged at debug, not warn).

Nothing is corrupted: the state on disk is the state at the signal. The cost is redone work and log noise on every stop of a busy daemon.

Fix: stop the updates loops and sync workers when the signal arrives, before mtcute's hook closes the storage, or close the clients from the daemon's own signal handler. Either way it means ordering our handler before mtcute's process listeners without touching mtcute internals (no supported option turns the sqlite driver's hook off in 0.32).

Done when a tg daemon stop during an active sync logs no updates-loop error and no failed save, and the live daemon smoke test still passes.

Activity

  1. added
    bug
    on Oct 21, 2026
  2. added 3 commits that reference this issue on Oct 22, 2026
    411c25a
    309eb19
    78f5f89
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

    bug

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions