summaryrefslogtreecommitdiff
path: root/external_plugins
diff options
context:
space:
mode:
authorKenneth Lien <kenneth@anthropic.com>2026-03-20 13:12:58 -0700
committerKenneth Lien <kenneth@anthropic.com>2026-03-20 13:12:58 -0700
commit757480dd7696f34fb9246ee9251244e878b109a9 (patch)
tree3af985860e8e4fb9035cec17fd3bab2b3cc05fa9 /external_plugins
parent9f2a4feab937324f57e0e0096dbfd58600fefbf9 (diff)
parent2aa90a83876b548c5db2e3ecae22d93088e6e0e5 (diff)
Merge remote-tracking branch 'origin/kenneth/telegram-shutdown' into kenneth/channels-rollup
Diffstat (limited to 'external_plugins')
-rw-r--r--external_plugins/telegram/server.ts20
1 files changed, 19 insertions, 1 deletions
diff --git a/external_plugins/telegram/server.ts b/external_plugins/telegram/server.ts
index 9786fc88..85df77d0 100644
--- a/external_plugins/telegram/server.ts
+++ b/external_plugins/telegram/server.ts
@@ -313,7 +313,7 @@ function checkApprovals(): void {
}
}
-if (!STATIC) setInterval(checkApprovals, 5000)
+if (!STATIC) setInterval(checkApprovals, 5000).unref()
// Telegram caps messages at 4096 chars. Split long replies, preferring
// paragraph boundaries when chunkMode is 'newline'.
@@ -516,6 +516,24 @@ mcp.setRequestHandler(CallToolRequestSchema, async req => {
await mcp.connect(new StdioServerTransport())
+// When Claude Code closes the MCP connection, stdin gets EOF. Without this
+// the bot keeps polling forever as a zombie, holding the token and blocking
+// the next session with 409 Conflict.
+let shuttingDown = false
+function shutdown(): void {
+ if (shuttingDown) return
+ shuttingDown = true
+ process.stderr.write('telegram channel: shutting down\n')
+ // bot.stop() signals the poll loop to end; the current getUpdates request
+ // may take up to its long-poll timeout to return. Force-exit after 2s.
+ setTimeout(() => process.exit(0), 2000)
+ void Promise.resolve(bot.stop()).finally(() => process.exit(0))
+}
+process.stdin.on('end', shutdown)
+process.stdin.on('close', shutdown)
+process.on('SIGTERM', shutdown)
+process.on('SIGINT', shutdown)
+
bot.on('message:text', async ctx => {
await handleInbound(ctx, ctx.message.text, undefined)
})