Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bot.run does not return when stop is called #517

Open
M4tyr0 opened this issue May 13, 2024 · 3 comments · May be fixed by #518
Open

Bot.run does not return when stop is called #517

M4tyr0 opened this issue May 13, 2024 · 3 comments · May be fixed by #518

Comments

@M4tyr0
Copy link

M4tyr0 commented May 13, 2024

Describe the bug
I am currently running the Bot in a different thread via the following code which works well:

 # Activate Eventloop for current thread
class MMThread():
def run(self, ...)
        self.loop = asyncio.new_event_loop()
        asyncio.set_event_loop(self.loop)

        self.bot = Bot(
            settings=Settings(
                MATTERMOST_URL = "URL",
                MATTERMOST_PORT = 443,
                BOT_TOKEN = self.api_token, #@cluster-bot
                BOT_TEAM = "...",
                SSL_VERIFY = True,
            ),  # Either specify your settings here or as environment variables.
            plugins=[HelpPlugin(),                   
                    StatusPlugin(...)],  # Add your own plugins here.
        )   
        self.bot.run()

def start(self):
    self.thread = threading.Thread(target=self.run, args=()) 

def stop(self):
    self.bot.stop()
    self.thread.join()

How To Reproduce
Give us simple steps to reproduce the behavior:

Expected behavior
I expect for bot.run to return after everything is shutdown internally and the thread to join.

Operating Environment (please complete the following information):

  • mmpy_bot Version: 2.1.4
  • mattermostdriver/mattermostautodriver Version: 1.2.2
  • Mattermost Server Version: -
  • Python Version: 3.10.12
  • OS: Linux
@unode
Copy link
Collaborator

unode commented May 13, 2024

How are you starting the bot?

Can you provide a minimal working example?

And in:

    self.thread = threading.Thread(target=run, args=()) 

what is run? Not the same as self.run.

@M4tyr0
Copy link
Author

M4tyr0 commented May 13, 2024

Hi,
here is a minimal (not) working example. For security reasons, I removed the API Key and the URL:


import threading
import asyncio
import time

from mmpy_bot import Bot, Settings
from mmpy_bot.plugins import (
    HelpPlugin
)

class MMBot():

    def __init__(self, api_token ):
      self.api_token = api_token
      return
    

    def run_thread(self):       
        # Activate Eventloop for current thread
        self.loop = asyncio.new_event_loop()
        asyncio.set_event_loop(self.loop)

        self.bot = Bot(
            settings=Settings(
                MATTERMOST_URL = "URL",
                MATTERMOST_PORT = 443,
                BOT_TOKEN = self.api_token, #@cluster-bot
                BOT_TEAM = "test",
                SSL_VERIFY = True,
            ),  # Either specify your settings here or as environment variables.
            plugins=[HelpPlugin()],  # Add your own plugins here.
        )  

        print('Starting Mattermost') 
        self.bot.run()

        self.loop.stop()
        self.loop.close()
        
        print('Mattermost Run exited')
        return


    ########################################################
    def start(self):
        self.thread = threading.Thread(target=self.run_thread, args=())    
        self.thread.start()           

    def stop(self):
        self.bot.stop()        
        self.thread.join()


if __name__ == '__main__':
    print('Create Bot')
    bot = MMBot("API_KEY")

    print('Start Bot:')
    bot.start()

    time.sleep(10)
    bot.stop()

    print('Mattermost Bot loop stopped')

Output:

Create Bot
Start Bot:
[05/13/2024 13:35:29][httpx][INFO] HTTP Request: GET URL/api/v4/users/me "HTTP/1.1 200 OK"
Starting Mattermost
[05/13/2024 13:35:29][mmpy.bot][INFO] Starting bot Bot.
[05/13/2024 13:35:29][mmpy.threadpool][INFO] Scheduler thread started.
[05/13/2024 13:35:29][mattermostautodriver.websocket][INFO] Websocket authentification OK
[05/13/2024 13:35:39][mmpy.bot][INFO] Stopping bot.
[05/13/2024 13:35:39][mmpy.threadpool][INFO] Stopping threadpool, waiting for threads...
[05/13/2024 13:35:39][mmpy.threadpool][INFO] Scheduler thread stopped.
[05/13/2024 13:35:39][mmpy.threadpool][INFO] Threadpool stopped.

@unode unode linked a pull request May 13, 2024 that will close this issue
@M4tyr0 M4tyr0 changed the title Bot run does not return when stop is called Bot.run does not return when stop is called May 13, 2024
@unode
Copy link
Collaborator

unode commented May 13, 2024

Thanks, I was able to replicate the error.

Seems like there are multiple bugs at play. One fix is now in #518 and will be merged in the future. This allows the bot to stop but there's a more complicated issue behind.

At the core of mattermostautodriver the driver currently blocks until a new message is received. This means that there is (in the worst case) a 5 minute delay (timeout) between the bot trying to stop and actually exiting. Worst case because with the patch in #518 the bot will exit if a message is sent by Mattermost before this.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants