sopel-irc / sopel

:robot::speech_balloon: An easy-to-use and highly extensible IRC Bot framework. Formerly Willie.
https://sopel.chat
Other
951 stars 405 forks source link

cli: `restart` command fails if instance was already restarted #2515

Open dgw opened 9 months ago

dgw commented 9 months ago

It's "good practice" to use python3 -m in many cases, and Sopel works just fine if started using python3 -m sopel.cli.run. Yet, trying to restart it fails using python3 -m sopel.cli.run restart (or sopel restart, if the sopel script comes from the same python3) fails:

# In a different shell, `python3.10 -m sopel.cli.run` is already running
$ python3.10 -m sopel.cli.run restart
Traceback (most recent call last):
  File "/home/dgw/github/sopel-irc/sopel/sopel/cli/run.py", line 20, in <module>
    from . import utils
ImportError: attempted relative import with no known parent package

Something about the restart implementation here is allowing the environment or invocation to change just enough that the new instance can't run properly:

https://github.com/sopel-irc/sopel/blob/03dd79b232e084eb997c7ac9a958995c6b8501a1/sopel/cli/run.py#L326-L328

I admit that I haven't troubleshooted (troubleshot?) this beyond testing the behavior and tracking down the responsible code, but it's interesting to find an unanswered comment on Stack Overflow (Ctrl+F for python -m if the anchor link doesn't work) about this exact situation on a question about having Python scripts restart themselves. I guess sys.execv(), seemingly the accepted solution for having Python programs restart themselves, just doesn't handle this case?

This is explicitly not a blocker for 8.0, btw. I just want to document it before I forget.

dgw commented 7 months ago

I've run into intermittent problems with restarting using the typical sopel restart -c configname spelling, too. Hard to troubleshoot as sometimes it works fine, sometimes not. Could be related to restarting the same instance multiple times… Log/traceback snippet follows, but it might deserve its own issue; not sure yet.

# Main shell:
dgw@borealis:~$ sopel restart -c yuzu
Asking Sopel to restart
dgw@borealis:~$

# tmux session where Sopel is running:
[... shutdown stuff ...]
[2023-11-22 08:14:55,705] sopel.bot            DEBUG    - Calling sopel.coretasks.shutdown                                                                                           
Traceback (most recent call last):                                                                                                                                                   
  File "/home/dgw/.local/bin/sopel", line 8, in <module>                                                                                                                             
    sys.exit(main())                                                                                                                                                                 
             ^^^^^^                                                                                                                                                                  
  File "/home/dgw/sopel/sopel/cli/run.py", line 467, in main                                                                                                                         
    return command(opts)                                                                                                                                                             
           ^^^^^^^^^^^^^                                                                                                                                                             
  File "/home/dgw/sopel/sopel/cli/run.py", line 328, in command_start                                                                                                                
    os.execv(sys.executable, ['python'] + sys.argv)                                                                                                                                  
FileNotFoundError: [Errno 2] No such file or directory                                                                                                                               
dgw@borealis:~/sopel$
dgw commented 4 months ago

Can confirm, if I start a Sopel instance (with the normal sopel -c configname command line) and later restart it once it's fine, then the restart fails with the FileNotFoundError traceback shown above on the second restart command.

Tested on a bot with basically no plugins (just version and one of my custom ones), less than a minute after startup, and with just a few seconds between the two restart commands. Unlikely that plugins are interfering with this, and it's also seemingly not related to a long-running bot either (which should make it easier to debug further when we're ready).