Skip to content

[BUG] Bot hangs when using ctrl + c and MessageQueue #2012

Closed
@revolter

Description

@revolter

Steps to reproduce

  1. Set the working variable to False
  2. Run the bot using ./main.py
  3. Hit ctrl + c

Expected behaviour

The bot should stop.

Actual behaviour

The bot hangs.

Configuration

Operating System: 10.15.5 (19F101)

Version of Python, python-telegram-bot & dependencies:

python-telegram-bot 12.8
certifi 2020.06.20
Python 3.7.7 (default, Mar 10 2020, 15:43:33)  [Clang 11.0.0 (clang-1100.0.33.17)]

Logs

Working:

> ./main.py
2020-07-02 12:59:25,880 - __main__ - INFO - Started polling
2020-07-02 12:59:26,157 - __main__ - INFO - Bot started. Press Ctrl-C to stop.
^C2020-07-02 12:59:27,504 - telegram.ext.updater - INFO - Received signal 2 (SIGINT), stopping...

then exits.

Not working:

> ./main.py
2020-07-02 12:59:34,283 - telegram.ext.updater - WARNING - Connection pool of Request object is smaller than optimal value (8)
2020-07-02 12:59:34,283 - __main__ - INFO - Started polling
2020-07-02 12:59:34,537 - __main__ - INFO - Bot started. Press Ctrl-C to stop.
^C2020-07-02 12:59:37,574 - telegram.ext.updater - INFO - Received signal 2 (SIGINT), stopping...

and hangs.

main.py:

#!/usr/bin/env python3
# -*- coding: utf-8 -*-

import logging

logging.basicConfig(format='%(asctime)s - %(name)s - %(levelname)s - %(message)s', level=logging.INFO)

from telegram.ext import Updater

from queue_bot import QueueBot

logger = logging.getLogger(__name__)
updater = None


def main():
    logger.info('Started polling')

    updater.start_polling(timeout=0.01)

    logger.info('Bot started. Press Ctrl-C to stop.')

    updater.idle()


if __name__ == '__main__':
    token = 'TOKEN'
    working = True

    if working:
        updater = Updater(token, use_context=True)
    else:
        queue_bot = QueueBot(token)
        updater = Updater(bot=queue_bot, use_context=True)

    main()

queue_bot.py:

#!/usr/bin/env python3
# -*- coding: utf-8 -*-

from telegram.bot import Bot
from telegram.ext import messagequeue


class QueueBot(Bot):
    def __init__(self, *args, **kwargs):
        super(QueueBot, self).__init__(*args, **kwargs)

        self._is_messages_queued_default = True
        self._msg_queue = messagequeue.MessageQueue()

    def __del__(self):
        try:
            self._msg_queue.stop()
        except:
            pass

    @messagequeue.queuedmessage
    def send_message(self, *args, **kwargs):
        return super(QueueBot, self).send_message(*args, **kwargs)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions