Investigate shutdown handling from unhandled exceptions in main bot task #5780
Labels
No labels
Automated PR
Blocked
Blocked By: Damage Control
Blocked By: Dependency
Blocked By: Other PR
Breaking Change
Category: CI
Category: Cogs - Admin
Category: Cogs - Alias
Category: Cogs - Audio
Category: Cogs - Bank
Category: Cogs - Cleanup
Category: Cogs - CustomCommands
Category: Cogs - Dev
Category: Cogs - Downloader
Category: Cogs - Economy
Category: Cogs - Filter
Category: Cogs - General
Category: Cogs - Image
Category: Cogs - Mod
Category: Cogs - Modlog
Category: Cogs - Mutes
Category: Cogs - Permissions
Category: Cogs - Reports
Category: Cogs - Streams
Category: Cogs - Trivia
Category: Cogs - Trivia - Lists
Category: Cogs - Warnings
Category: Core - API - App Commands Package
Category: Core - API - Audio
Category: Core - API - Bank
Category: Core - API - Commands Package
Category: Core - API - Config
Category: Core - API - Other
Category: Core - API - Utils Package
Category: Core - Bot Class
Category: Core - Bot Commands
Category: Core - Command-line Interfaces
Category: Core - Downloader
Category: Core - Help
Category: Core - i18n
Category: Core - Modlog
Category: Core - Other Internals
Category: Core - RPC/ZMQ
Category: Docker
Category: Docs - Changelogs
Category: Docs - For Developers
Category: Docs - Install Guides
Category: Docs - Other
Category: Docs - User Guides
Category: Meta
Category: RPC/ZMQ methods
Category: Vendored Packages
Changelog Entry: Added
Changelog Entry: Pending
Changelog Entry: Skipped
Closed: Duplicate
Closed: External
Closed: Invalid
Closed: User Error
Closed: Won't Fix
Complex Issue
Docs-only
Feature Branch
Good First Issue
Help Wanted
High Priority
Needs Backport To stable-docs
No Activity
No Repro
QA: Bypassed
QA: Changes Requested
QA: Passed
Release Blocker
Status: Accepted
Status: Frozen
Status: In Progress
Status: Needs Discussion
Status: Needs Info
Status: Needs Triage
Status: PRs Welcome
Type: Bug
Type: Dependency Update
Type: Deprecation
Type: Enhancement
Type: Feature
Type: Informational
Type: Merge
Type: Optimisation
Type: Question
Type: Removal
No milestone
No project
No assignees
1 participant
Notifications
Due date
No due date set.
Dependencies
No dependencies set
Reference
Mrcoolgamer11/Red-DiscordBot#5780
Loading…
Reference in a new issue
No description provided.
Delete branch "%!s()"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
It would seem that Red may infinitely hang during the handling of such exceptions.
An example:
It has been a while since this issue has been posted, however, it seems to be exactly what happened twice to my bot.
I have a bot instance running on Ubuntu 20.04 LTS based server. We had 2 longer power outages recently, in both cases the server got up before the modem could connect back to the ISP, resulting in the bot starting while offline. This is pretty much what happened:
As soon as the bot loaded, it threw an exception about being unable to connect to Discord. HOWEVER, as you can see from timestamps, despite saying "Shutting down", it does not, it's still running, due to which systemd sees it as still running, not restarting it automatically. It wasn't until a few days later that one of the mods noticed the bot is down that it was manually restarted (hence the 3 messages at the end of the log).
Additionally, here is the capture from systemd showing the log, while also showing the service as active:
Redbot version 3.4.18
I have had a similar situation as PewnyPL, usually when there is a power outage the device running the bot comes up faster than the router so there is no connection when the service that starts the bot runs.
Even though I have set the service to wait for network (that I just found out that might not be working) the bot starts first and as result I get a crash. Here is a log of one of those (I have shortened the file paths):
Also note that the process actually terminate around 1h 30m later, despite saying "Shutting down...".
By the way, this is now a request depending on how this will be dealt with: I have set the service to not restart if the Exit code is 0 (just then it doesn't restart if I manually shut it down through [p]shutdown), however, as seen above the Exit code is 0 for this issue then preventing the service from restarting despite the crash. It could be helpful if Exit code wasn't 0 for these cases.
Red v3.5.2 by the way.
Hi, I'd like to report that this is plaguing me too. I can also verify by manually blocking discord.com in hosts that, this issue happens even without using systemd, so systemd is not the core issue here.
I'd like to add I am using the postgres version of RedBot. Perhaps all the users above are also using Postgres? This should be a common issue judging by its presentation yet only 3 reporting has been done so far.
Tried but no effect:
Edit 1: I tried running it with a --no-cogs option, and it seems doesn't seem to have any effect,
Edit 2: Running the code with profiler, it seems that the redbot is stuck in the run_forever loop and any attempts to stop the code must have failed.
I found a workaround for this by adding
ExecStartPre=/bin/sh -c 'until ping -c1 discord.com; do sleep 1; done;'under[Service]in the systemd service file for the bot.This ExecStartPre command causes the ping command to loop until it succeeds, which delays the starting of the bot until discord.com is reachable.
This message is unrelated to this bug. This bug is about the bot shutdown process, not the bot starting up before
discord.comis reachable (which is a different issue that is not a bot problem)The workaround I posted is 100% related to this bug. The bug is that the bot hangs instead of shutting down if discord.com is unreachable.
The workaround mearly prevents the bot from trying to start while discord.com isn't reachable due to network not being ready yet.
Look at the last few lines of the traceback ajwset posted
I agree that the shown scenario is one of the cases that can produce this bug. It is possible that there will need to be some additional care taken since this specific case occurs for shutdowns during startup but I would definitely to properly handle shutdowns happening during startup.
Note that a fix for this issue is unlikely to involve a reconnection strategy which I'm guessing is why Yami said it doesn't relate to this bug - the bot would just actually shutdown (with a proper exit code) rather than hang indefinitely.
As far as workarounds go, this one does help with some of the cases listed in the comments on this issue, it just won't help you resolve the underlying issue of the bot hanging on shutdown if it happens for any other reason than discord.com being unreachable while the bot starts up. Based on this issue, there are some people in the community struggling with it so I'm sure some will find this useful :)
I had a little poke around debugging with visual studio code I found this is the last line (of the bots code) that gets executed before the hang:
github.com/Cog-Creators/Red-DiscordBot@7dfe24397e/redbot/main.py#L445C9-L445C9After Stepping over this line the bot hangs and the debugger hangs too (won't let me pause to debug it anymore). I think some asyncio thing is getting stuck with the shutdown handler.
Yes, while indeed it doesn't fix the bug it is related and worked as a good workaround for the issue (at least for what I was facing). Delaying the start up while domain name can't be solved helped to prevent the crash and hang at least.
After some more poking around I found that if an exception isn't caught inside run_bot() and sys.exit() isn't called, the bot will hang inside the exception handler and shutdown handler.
So I created a pull request that
adds a catch all exception to catch any aiohttp exceptions (or any other kind of exception) to the relevant place. I tested this as working on linux and windows.I don't know if having a catch all is acceptable so feel free to make the catch more specific(I already tried catching discord.HTTPException but discord.py doesn't seem to be catching aiohttp exceptions internally in this case)
Edit: I stopped the exception handler from hanging in the first place instead.