Stopping during a handler leaves the last update unconfirmed, so it's redelivered on restart
Bug Report
I have read:
I am using the latest version of the library.
Expected Behavior
After bot.stop() (or SIGINT/SIGTERM via run()), an update whose handler finished isn't delivered again on the next start.
Actual Behavior
Telegram confirms updates only when the next getUpdates is sent with offset = last + 1. longPoll advances offset after each yield, but only sends it on the next poll. If stop() lands while a handler is running, the loop exits on signal.aborted before that poll goes out, so the handled update is never confirmed and comes back after restart.
With the default 30s long-poll and short handlers, the window is just the handler's runtime. A deploy's SIGTERM during a slow handler (one that calls an external API, say) hits it every time, and non-idempotent handlers run twice.
Steps to reproduce the Behavior
bot.on("message", async (ctx) => {
console.log("handling", ctx.update.update_id);
await new Promise((r) => setTimeout(r, 5000));
});
await bot.startPolling();
Send the bot a message, then Ctrl+C within the 5s. On the next start, the same update_id is logged again.
A fix could be a final getUpdates({ offset, limit: 1, timeout: 0 }) without the aborted signal when the loop exits with an advanced offset. grammY's stop() does the same thing (getUpdates({ offset, limit: 1 })).