Skip to content

fix(remote-control): fix Telegram adapter race condition#472

Merged
sorrycc merged 1 commit intomasterfrom
fix/telegram-adapter-race-condition
Apr 9, 2026
Merged

fix(remote-control): fix Telegram adapter race condition#472
sorrycc merged 1 commit intomasterfrom
fix/telegram-adapter-race-condition

Conversation

@sorrycc
Copy link
Copy Markdown
Member

@sorrycc sorrycc commented Apr 9, 2026

Summary

  • Fix race condition where running flag was set too late (in grammY's onStart callback instead of before bot.start()), causing testConnection() to return "Adapter is not running" and onConfigChanged() to leak duplicate bot instances on rapid restarts
  • Extract stopBot() helper that tears down the bot without removing event listeners, preventing the stop-guard from nuking freshly-registered service listeners
  • Move setMyCommands to onStart callback so it only fires after polling is established, eliminating ETIMEDOUT spam when api.telegram.org is unreachable
  • Add optional testConnection() to the adapter interface, implemented in Telegram via getMe() for real token/network verification

Test plan

  • Toggle Telegram on/off rapidly in settings → only one "Telegram polling established" per start in logs, no duplicate setMyCommands calls
  • testConnection() returns { ok: true, botUsername: "..." } immediately after enabling
  • Explicit stop → no duplicate "disconnected" event
  • Unreachable api.telegram.org → no setMyCommands ETIMEDOUT spam
  • bun ready passes (typecheck + lint + format + 572 tests)

…icate bot instances

bot.start() is non-blocking but `running` was only set in the onStart
callback. This caused testConnection() to report "Adapter is not running"
and onConfigChanged() to leak duplicate bot instances on rapid restarts.

- Set running=true before bot.start() so isRunning() is immediately accurate
- Extract stopBot() helper to stop the bot without removing event listeners
- Add stop-guard in start() to prevent leaked instances
- Detect polling death via .then() on bot.start() promise
- Move setMyCommands to onStart callback to avoid ETIMEDOUT spam
- Add testConnection() to adapter interface for real token verification
@sorrycc sorrycc merged commit b2d842f into master Apr 9, 2026
6 checks passed
@github-actions
Copy link
Copy Markdown

github-actions bot commented Apr 9, 2026

Build Ready

Platform Architecture Download Size
macOS Apple Silicon Download 147.40 MB
macOS Intel Download 155.07 MB
Windows x64 Download 171.05 MB

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.

1 participant