Skip to content

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

Open
NNTin wants to merge 10 commits intoCog-Creators:V3/developfrom
NNTin:fix/rpc_reload
Open

fix(core): rpc reload has old cache#6628
NNTin wants to merge 10 commits intoCog-Creators:V3/developfrom
NNTin:fix/rpc_reload

Conversation

@NNTin
Copy link
Contributor

@NNTin NNTin commented Nov 4, 2025

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

@github-actions github-actions bot added Category: Core - RPC/ZMQ Related to provided (provisional) API for implementing RPC in cogs Category: Core - Bot Class This is related to the `redbot.core.bot.Red` class. labels Nov 4, 2025
@NNTin
Copy link
Contributor Author

NNTin commented Nov 4, 2025

            # 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

…pyc files in __pycache__ directories

Reload Problem: When modules were reloaded after source code changes, Python continued using the cached .pyc
@github-actions github-actions bot added the Category: Core - Bot Commands This is related to core commands (Core and CogManagerUI cog classes). label Nov 4, 2025
NNTin added 4 commits November 4, 2025 12:10
…rpc_reload; this test will fail, HTTP test re-runs red.rpc._pre_login() after fixtures, wiping earlier RPC handler registrations.
… it as:

params[0] (which is "httptest") → pkg_names argument
So pkg_names gets the value "httptest" instead of ["httptest"].
@NNTin
Copy link
Contributor Author

NNTin commented Nov 4, 2025

The test with the version update was fixed in commit 9650a09 (?)
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 😖

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Category: Core - Bot Class This is related to the `redbot.core.bot.Red` class. Category: Core - Bot Commands This is related to core commands (Core and CogManagerUI cog classes). Category: Core - RPC/ZMQ Related to provided (provisional) API for implementing RPC in cogs

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant