fix(core): rpc reload has old cache #6628

Closed
NNTin wants to merge 0 commits from NNTin/fix/rpc_reload into V3/develop
NNTin commented 2025-11-04 10:11:03 +01:00 (Migrated from github.com)

Description of the changes

When a cog is unregistered and re-registered, the RPC methods are being removed and added, but the JsonRpcMethod wrapper is storing a reference to the old method object from the original cog instance.

  • Investigate why rpc_reload() in redbot/core/core_commands.py doesn't properly refresh module code
  • The issue: _cleanup_and_refresh_modules() is called in rpc_load() but old method references may persist
  • Fix the reload sequence to ensure modules are refreshed before re-reigstering RPC handlers
  • Ensure bot.remove_cog() properly unregisters old RPC handlers before new ones are added

Have the changes in this PR been tested?

Yes, via tests
No, via manual reproduction of issue

### Description of the changes > When a cog is unregistered and re-registered, the RPC methods are being removed and added, but the JsonRpcMethod wrapper is storing a reference to the old method object from the original cog instance. > - Investigate why `rpc_reload()` in `redbot/core/core_commands.py` doesn't properly refresh module code > - The issue: `_cleanup_and_refresh_modules()` is called in `rpc_load()` but old method references may persist > - Fix the reload sequence to ensure modules are refreshed before re-reigstering RPC handlers > - Ensure `bot.remove_cog()` properly unregisters old RPC handlers before new ones are added ### Have the changes in this PR been tested? Yes, via tests No, via manual reproduction of issue
NNTin commented 2025-11-04 11:20:05 +01:00 (Migrated from github.com)
            # Call RPC handler and verify it now executes new code
            result = await new_handler()
>           assert result == "version_2", "RPC handler should execute new code after reload"
E           AssertionError: RPC handler should execute new code after reload
E           assert 'version_1' == 'version_2'
E             - version_2
E             ?         ^
E             + version_1
E             ?         ^

from https://github.com/Cog-Creators/Red-DiscordBot/actions/runs/19065280048/job/54454289643?pr=6628
I can now reproduce the problem

```bash # Call RPC handler and verify it now executes new code result = await new_handler() > assert result == "version_2", "RPC handler should execute new code after reload" E AssertionError: RPC handler should execute new code after reload E assert 'version_1' == 'version_2' E - version_2 E ? ^ E + version_1 E ? ^ ``` from https://github.com/Cog-Creators/Red-DiscordBot/actions/runs/19065280048/job/54454289643?pr=6628 I can now reproduce the problem
NNTin commented 2025-11-04 13:02:46 +01:00 (Migrated from github.com)

The test with the version update was fixed in commit 9650a09ef9 (?)
Issue was caching.

python -m pytest tests/core/test_rpc.py::test_rpc_handler_updates_on_reload -v -s --tb=short

Solving rpc reload via websocket, has the same root of error:

Before reload:
Original cog: 2280439307152
Original handler: 2280439127936
Original cog method: 2280439129536

After reload:
New cog: 2280439307152 ← SAME COG OBJECT!
New handler: 2280439127936 ← SAME HANDLER OBJECT!
New cog method: 2280360060864 ← DIFFERENT METHOD OBJECT!

Solving that was a mess. Month just started and my GitHub Copilot is already at 50% usage 😖

The test with the version update was fixed in commit 9650a09ef944c15b7e0cd643056783c9ec6235e2 (?) Issue was caching. python -m pytest tests/core/test_rpc.py::test_rpc_handler_updates_on_reload -v -s --tb=short Solving rpc reload via websocket, has the same root of error: ``` Before reload: Original cog: 2280439307152 Original handler: 2280439127936 Original cog method: 2280439129536 After reload: New cog: 2280439307152 ← SAME COG OBJECT! New handler: 2280439127936 ← SAME HANDLER OBJECT! New cog method: 2280360060864 ← DIFFERENT METHOD OBJECT! ``` Solving that was a mess. Month just started and my GitHub Copilot is already at 50% usage 😖
Owner

Superseded by #6693 and #6739, which replace the custom reload logic this PR modified. This PR also deleted __pycache__ directories on unload (including inside the installed Red package) and left debug prints, so it wasn't kept.

Superseded by #6693 and #6739, which replace the custom reload logic this PR modified. This PR also deleted `__pycache__` directories on unload (including inside the installed Red package) and left debug prints, so it wasn't kept.
Mrcoolgamer11 closed this pull request 2026-11-21 16:10:40 +01:00

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
2 participants
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!6628
No description provided.