#22694: telegram: stabilize multi-account webhook mode
channel: telegram
size: M
Cluster:
Webhook Configuration and Resilience
## Summary
- keep `monitorTelegramProvider` alive in webhook mode until abort, so channel manager does not auto-restart a healthy webhook worker
- support multi-account webhook mode on one listener by routing per-path and sharing the HTTP server
- derive account-specific default webhook paths (`/telegram-webhook/<accountId>`) for non-default accounts and fail fast on path conflicts
## Why
- in webhook mode, the provider task returned immediately after startup, which made gateway think the channel exited and trigger restart loops
- repeated restarts in multi-account setups caused `listen EADDRINUSE 127.0.0.1:8787`
## Testing
- `corepack pnpm vitest src/telegram/webhook.test.ts src/telegram/monitor.test.ts`
Fixes #22672
<!-- greptile_comment -->
<h3>Greptile Summary</h3>
Refactored Telegram webhook mode to fix restart loops and enable multi-account support on a shared HTTP server. The key changes prevent the gateway manager from treating successful webhook startup as an exit (which triggered restarts) and eliminate `EADDRINUSE` errors when multiple accounts try to bind to the same port.
- **monitor.ts**: Added await on abort signal to keep the webhook provider task alive until explicitly stopped
- **webhook.ts**: Introduced shared server pool with per-path routing, auto-generated account-specific default paths (`/telegram-webhook/<accountId>`), and fail-fast validation for path conflicts
- **Tests**: Added coverage for webhook task lifecycle, multi-account server sharing, and conflict detection
<h3>Confidence Score: 4/5</h3>
- This PR is safe to merge with one edge case fix needed
- The implementation correctly addresses the restart loop issue and multi-account webhook support with good test coverage. However, there's a logical gap when `abortSignal` is not provided - the webhook task will return immediately instead of staying alive, which could cause the same restart loop the PR aims to fix. This edge case needs to be handled.
- Pay attention to src/telegram/monitor.ts lines 170-178 for the abortSignal edge case
<sub>Last reviewed commit: c203a02</sub>
<!-- greptile_other_comments_section -->
<sub>(2/5) Greptile learns from your feedback when you react with thumbs up/down!</sub>
<!-- /greptile_comment -->
Most Similar PRs
#17918: fix(telegram): start webhook listener before setWebhook + forward w...
by Glucksberg · 2026-02-16
81.0%
#8166: fix(telegram): lifecycle fixes for duplicate messages and auto-reco...
by cheenu1092-oss · 2026-02-03
79.5%
#20309: [BUG]: fix telegram webhook should wait for abort signal instead of...
by kesor · 2026-02-18
79.0%
#20420: Telegram webhook listener to use the gateway's HTTP server instead ...
by kesor · 2026-02-18
78.8%
#22322: fix(googlechat): keep webhook monitor alive until abort
by AIflow-Labs · 2026-02-21
78.5%
#10865: telegram: fast-ACK webhook and retry bind on EADDRINUSE
by u9733037 · 2026-02-07
78.2%
#13521: telegram: require webhook secret in runtime webhook mode
by davidahmann · 2026-02-10
78.0%
#20395: fix(googlechat): prevent infinite auto-restart and ambiguous-target...
by ggalmeida0 · 2026-02-18
78.0%
#23621: fix(LINE): keep startAccount promise alive to prevent auto-restart ...
by ttakanawa · 2026-02-22
76.0%
#6463: fix(telegram): improve timeout handling and prevent channel exits
by ai-fanatic · 2026-02-01
75.6%