Address shutdown behavior #6633

Closed
NNTin wants to merge 4 commits from fix/dry-run into V3/develop
NNTin commented 2025-11-21 01:25:23 +01:00 (Migrated from github.com)
Analysis of the problem

Problem Analysis

Based on the error traceback and codebase investigation, here's what's happening:

The Error

AttributeError: 'Red' object has no attribute '_AutoShardedClient__queue'. Did you mean: '_AutoShardedClient__shards'?

This error occurs during the bot shutdown process when red.close() is called. The issue is that the Red class (which inherits from discord.ext.commands.AutoShardedBot) is trying to access an internal attribute __queue that doesn't exist.

Root Cause

The problem stems from the mixin initialization order in the Red class:

class Red(
    commands.GroupMixin, RPCMixin, dpy_commands.bot.AutoShardedBot
):

Looking at the initialization chain:

  1. Red.init (line 104-241 in bot.py) does extensive setup work BEFORE calling super().init() at line 241

  2. RPCMixin.init (line 144-146 in _rpc.py) calls super().init(**kwargs)

  3. commands.GroupMixin doesn't define init, so it passes through

  4. Finally reaches AutoShardedBot.init which sets up internal attributes like __queue

The issue is that during the --dry-run execution:

  • The bot starts initialization

  • At line 366 in main.py, when --dry-run is detected, it calls sys.exit(ExitCodes.SHUTDOWN)

  • This triggers the shutdown handler at line 539

  • The shutdown handler calls await red.close() at line 441

  • But red.close() → super().close() (line 2278 in bot.py) tries to access AutoShardedClient's internal __queue attribute

  • This attribute was never properly initialized because the initialization was interrupted

Why This Happens Specifically with --dry-run

The --dry-run flag causes an early exit at line 365-366 in main.py:

if cli_flags.dry_run:
    sys.exit(ExitCodes.SHUTDOWN)

This happens before await red.start(token) is called (line 369), which means:

  • The bot's connection lifecycle never begins

  • Discord.py's internal sharding infrastructure (including __queue) is never fully initialized

  • But the shutdown handler still tries to call close() which expects these internals to exist

try, catch might not be the best solution but I can't think of a better one. This only happens when it is not fully initialized and thus sharding is not possible.

closes #6572
the original issue is also addressed.

PS C:\privat\codex\Red-DiscordBot> python -m redbot --no-prompt
Error: No instance name was provided!
PS C:\privat\codex\Red-DiscordBot> python -m redbot --no-prompt tinkerer
Instance with name 'tinkerer' doesn't exist. You can create new instance using `redbot-setup` prior to running the bot.
<details> <summary>Analysis of the problem</summary> ## Problem Analysis Based on the error traceback and codebase investigation, here's what's happening: ### The Error ``` AttributeError: 'Red' object has no attribute '_AutoShardedClient__queue'. Did you mean: '_AutoShardedClient__shards'? ``` This error occurs during the bot shutdown process when red.close() is called. The issue is that the Red class (which inherits from discord.ext.commands.AutoShardedBot) is trying to access an internal attribute __queue that doesn't exist. ### Root Cause The problem stems from the **mixin initialization order** in the Red class: ```python class Red( commands.GroupMixin, RPCMixin, dpy_commands.bot.AutoShardedBot ): ``` Looking at the initialization chain: 1. **Red.__init__** (line 104-241 in bot.py) does extensive setup work BEFORE calling super().__init__() at line 241 2. **RPCMixin.__init__** (line 144-146 in _rpc.py) calls super().__init__(**kwargs) 3. **commands.GroupMixin** doesn't define __init__, so it passes through 4. Finally reaches **AutoShardedBot.__init__** which sets up internal attributes like __queue The issue is that during the --dry-run execution: - The bot starts initialization - At line 366 in __main__.py, when --dry-run is detected, it calls sys.exit(ExitCodes.SHUTDOWN) - This triggers the shutdown handler at line 539 - The shutdown handler calls await red.close() at line 441 - But red.close() → super().close() (line 2278 in bot.py) tries to access AutoShardedClient's internal __queue attribute - This attribute was never properly initialized because the initialization was interrupted ### Why This Happens Specifically with --dry-run The --dry-run flag causes an early exit at line 365-366 in __main__.py: ```python if cli_flags.dry_run: sys.exit(ExitCodes.SHUTDOWN) ``` This happens **before** await red.start(token) is called (line 369), which means: - The bot's connection lifecycle never begins - Discord.py's internal sharding infrastructure (including __queue) is never fully initialized - But the shutdown handler still tries to call close() which expects these internals to exist </details> try, catch might not be the best solution but I can't think of a better one. This only happens when it is not fully initialized and thus sharding is not possible. closes #6572 the original issue is also addressed. ```pwsh PS C:\privat\codex\Red-DiscordBot> python -m redbot --no-prompt Error: No instance name was provided! PS C:\privat\codex\Red-DiscordBot> python -m redbot --no-prompt tinkerer Instance with name 'tinkerer' doesn't exist. You can create new instance using `redbot-setup` prior to running the bot. ```
Jackenmen commented 2026-03-04 22:04:07 +01:00 (Migrated from github.com)

This will be fixed by the next discord.py update:
https://github.com/Rapptz/discord.py/pull/10422

This will be fixed by the next discord.py update: https://github.com/Rapptz/discord.py/pull/10422

Pull request closed

Sign in to join this conversation.
No reviewers
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
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set

Reference
Mrcoolgamer11/Red-DiscordBot!6633
No description provided.