Skip to content

fix(daemon): restore real-time updates and sync after a reconnect - #28

Merged
neoromantic merged 2 commits into
mainfrom
fix/daemon-reconnect-16
Oct 21, 2026
Merged

neoromantic merged 2 commits into
mainfrom
fix/daemon-reconnect-16

Conversation

@neoromantic

Copy link
Copy Markdown
Owner

Problem

After one failed health check, attemptReconnect opened a new client but:

  • never registered the update handlers or started the updates loop, so new messages stopped reaching the cache;
  • left the old client open, so two clients shared one session file;
  • left the account's sync worker bound to the old client.

tg status still showed the account as connected. On top of that, closeClientSafe looked up a close() method that mtcute's TelegramClient doesn't have (it closes with destroy()), so it never closed anything.

Fix

  • closeClientSafe now calls client.destroy(). This also fixes the duplicate-account merge, which left its connection open.
  • On reconnect, the old client's handlers are removed, its sync worker is dropped and the client is closed before the new client opens the session file.
  • After a successful reconnect, the handlers are registered on the new client, its updates loop is started and the account's sync worker is rebuilt on it (rebuildSyncWorker in daemon-scheduler.ts; it does nothing while the scheduler isn't running).
  • A failed attempt closes the new client (and removes its handlers if they were registered) and schedules the next retry as before.
  • A reconnect can't run twice at once or register handlers twice: the status changes to reconnecting before the first await, so the next health check skips the account.
  • Sync workers now expose the client they call, so tests can check which client a worker uses.
  • Docs: docs/daemon.md (Error Handling & Resilience), docs/testing.md (mock destroy, not close), and a CHANGELOG entry.

Tests

  • New test: connect, build the scheduler, force a reconnect. It checks that the old client lost its handlers and was closed before the new client started, that the new client has exactly one set of handlers and a running updates loop, that a message sent to the new client is cached, and that the rebuilt sync worker uses the new client.
  • New test: when the updates loop fails to start, the new client's handlers are removed, the client is closed and a retry is scheduled.
  • The existing failure test now also checks that the failed client is closed.
  • Unit tests for rebuildSyncWorker and for closeClientSafe calling destroy().
  • bun run check, bun run test and bun run test:e2e pass locally. The live suite was not run.

Fixes #16

🤖 Generated with Claude Code

neoromantic and others added 2 commits October 21, 2026 16:08
closeClientSafe looked up a close() method, which mtcute's TelegramClient
doesn't have (it closes with destroy()), so the helper silently did
nothing. The duplicate-account merge left its connection open, and the
reconnect fix needs this helper to really close the old client before a
new one opens the same session file.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
attemptReconnect opened a new client but never registered the update
handlers or started its updates loop, left the old client open on the
same session file, and kept the account's sync worker bound to the old
client. After one failed health check the daemon still looked connected
but quietly stopped caching new messages.

A reconnect now removes the old client's handlers, drops its sync worker
and closes it before opening the new client. On success it registers the
handlers, starts the updates loop and rebuilds the sync worker on the new
client. A failed attempt closes the new client and schedules the next
retry as before. Sync workers expose the client they call, so tests can
check which one a worker uses.

Fixes #16

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@neoromantic
neoromantic force-pushed the fix/daemon-reconnect-16 branch from 6f36d95 to c494622 Compare October 21, 2026 12:08
@neoromantic
neoromantic merged commit 77a832c into main Oct 21, 2026
5 checks passed
@neoromantic
neoromantic deleted the fix/daemon-reconnect-16 branch October 21, 2026 12:10
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.

Reconnect doesn't restore real-time updates

1 participant