yagop / node-telegram-bot-api

Telegram Bot API for NodeJS
MIT License
8.3k stars 1.5k forks source link

stopPolling() doesn't cancel the polling connection #455

Open peoro opened 6 years ago

peoro commented 6 years ago

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.

GochoMugo commented 6 years ago

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

peoro commented 6 years ago

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.

arenddeboer commented 4 years ago

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.

PtruckStar commented 3 years ago

@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"}

milad2golnia commented 11 months ago

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

FirstAlertDunc commented 10 months ago

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 commented 10 months ago

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?