Skip to content

Shared task board where people and AI coding agents work as teammates. Source-available, self-hostable.

Go

fix(telegram): handle 409 polling conflict and add global error boundary

Jiayuan Zhang5741402a1a4c
diff --git a/apps/gateway/telegram/telegram.service.ts b/apps/gateway/telegram/telegram.service.ts
index 3101484404..40c8842738 100644
--- a/apps/gateway/telegram/telegram.service.ts
+++ b/apps/gateway/telegram/telegram.service.ts
@@ -192,11 +192,22 @@ export class TelegramService implements OnModuleInit, OnModuleDestroy {
} else {
// Long-polling mode — pull updates from Telegram (local development)
this.pollingMode = true;
- this.bot.start({
- onStart: () => {
- this.logger.log("Telegram bot initialized (long-polling mode)");
- },
- });
+ this.bot
+ .start({
+ onStart: () => {
+ this.logger.log("Telegram bot initialized (long-polling mode)");
+ },
+ })
+ .catch((err) => {
+ if (err instanceof GrammyError && err.error_code === 409) {
+ this.logger.warn(
+ "Telegram bot polling conflict (409): another instance is already running. Polling stopped.",
+ );
+ } else {
+ this.logger.error(`Telegram bot polling error: ${err instanceof Error ? err.message : err}`);
+ }
+ this.pollingMode = false;
+ });
}
}
@@ -245,6 +256,12 @@ export class TelegramService implements OnModuleInit, OnModuleDestroy {
private setupHandlers(): void {
if (!this.bot) return;
+ // Global error boundary — catches unhandled errors from all middleware/handlers
+ // so they don't crash the polling loop or propagate as unhandled rejections.
+ this.bot.catch((err) => {
+ this.logger.error(`Unhandled bot error: ${err.message}`);
+ });
+
// Per-chat serialization middleware — ensures messages from the same chat
// are processed one at a time, preventing race conditions.
const chatQueues = new Map<string, Promise<void>>();