node-telegram-bot-api icon indicating copy to clipboard operation
node-telegram-bot-api copied to clipboard

stopPolling() doesn't cancel the polling connection

Open peoro opened this issue 7 years ago • 7 comments

Bug Report

I have read:

I am using the latest version of the library.

Expected Behavior

After calling stopPolling no listener should be left waiting for polling-related events: if no other event handlers are active, Node's event loop should terminate.

Actual Behavior

A TLSSocket object starts listening on a socket when polling starts. stopPolling doesn't stop it.

This is because HTTPS requests to the Telegram server use the forever agent (src/telegram.js:230), and the agent is not cancelled by stopPolling (which calls this._polling.stop(): src/telegramPolling.js:57)

Steps to reproduce the Behavior

Run the following piece of code:

const TelegramBot = require('node-telegram-bot-api');
const bot = new TelegramBot(TOKEN, {polling:true});
bot.stopPolling();

This program never terminates as the polling request is still active.

peoro avatar Nov 07 '17 16:11 peoro

I can not seem to reproduce this bug. Which Node.js version are you using?

GochoMugo avatar Nov 16 '17 13:11 GochoMugo

I'm using node 8.8.1 on some machines and 9.2.0 on others.

I just tried again to run the snippet I provided and noticed something weird: the behaviour changes depending on the bot. I just created a brand new bot to test this, and the script above constantly takes 10 seconds to terminate (between 10.2 and 10.5 real time seconds, every single time), both on node 8.8.1 and 9.2.0. Then I started a conversation with the bot (i.e. I sent it /start), and now the snippet is terminating instantly.

Could you try reproducing this bug using the token of a newly created bot?

I cannot use the bot I was using when I created this issue (since a program is already polling on it and I cannot stop it now), but the snippet was definitely taking longer than 10 seconds to terminate with that bot.

peoro avatar Nov 18 '17 06:11 peoro

I thought I was hitting this bug too, but giving it a better look it may help to provide cancel: true as object property. eg:

bot.stopPolling({
        cancel: true,
        reason: 'Lost leader status'
    })

Just for anyone running into this.

arenddeboer avatar Dec 12 '19 17:12 arenddeboer

@arenddeboer did this but still getting an error when restarting the bot

error: [polling_error] {"code":"ETELEGRAM","message":"ETELEGRAM: 409 Conflict: terminated by other getUpdates request; make sure that only one bot instance is running"}

PtruckStar avatar May 31 '21 08:05 PtruckStar

I have this problem yet even when calling bot.stopPolling({ cancel: true });.

milad2golnia avatar Sep 28 '23 16:09 milad2golnia

I have been experiencing this issue too - I found that by setting the bot._polling._abort flag to true just after calling bot.stopPolling(), it stopped the execution as expected.

Looks like this flag is only being set if the options.cancel property is not set - thus it cancels the promise, but never stops execution.

Selection_564

Perhaps the flag could be set just after (or before) the timer is cleared?

FirstAlertDunc avatar Oct 20 '23 13:10 FirstAlertDunc

Ah also why is there a "reason" being provided to the Bluebird's Promise.cancel() method when it doesn't accept any arguments?

Selection_565

Is this for future feature provision or something? Not against doing it - just noticed it doesn't seem to go anywhere?

FirstAlertDunc avatar Oct 20 '23 13:10 FirstAlertDunc