[V3 Core] Difficulty reloading a cog with multiple modules. #1457

Closed
opened 2018-03-24 17:41:17 +01:00 by Redjumpman · 2 comments
Redjumpman commented 2018-03-24 17:41:17 +01:00 (Migrated from github.com)

Please be sure to read through other issues as well to make sure what you are suggesting/reporting has not already
been suggested/reported

Type:

  • Suggestion
  • Bug

Brief description of the problem

Reloading a cog seems to only work with the main module. Auxiliary module changes do not appear to be reflected in the reload. Workaround involves doing a bot shutdown and starting the bot again.

Expected behavior

The cog and all supporting modules in the folder are "reloaded" to reflect the changes.

Actual behavior

Only changes made to the main cog are reflected in the main cog. Shutting down the bot and starting it again is the only way to see the changes.

Steps to reproduce

  1. Make a cog with 2 modules
  2. Add in an reproducible error to the auxiliary module.
  3. Test the error
  4. Fix the error
  5. Reload the cog
Please be sure to read through other issues as well to make sure what you are suggesting/reporting has not already been suggested/reported ### Type: - [ ] Suggestion - [x] Bug ### Brief description of the problem Reloading a cog seems to only work with the main module. Auxiliary module changes do not appear to be reflected in the reload. Workaround involves doing a bot shutdown and starting the bot again. ### Expected behavior The cog and all supporting modules in the folder are "reloaded" to reflect the changes. ### Actual behavior Only changes made to the main cog are reflected in the main cog. Shutting down the bot and starting it again is the only way to see the changes. ### Steps to reproduce 1. Make a cog with 2 modules 2. Add in an reproducible error to the auxiliary module. 3. Test the error 4. Fix the error 5. Reload the cog
Tobotimus commented 2018-03-25 00:53:46 +01:00 (Migrated from github.com)

This is a pretty complex issue unfortunately, and something lots of projects have trouble with. Here's an example of why.

We have a python package:

package
 | - __init__.py
 | - module1.py
 \ - module2.py

Say we make some modifications to the code in some of these modules, and want to reload it. Obviously, only one module can be interpreted at a time. So which one do we do first? It begs the question. What the reload command does is iterates over each module inside the package, reloading one after the other.

For now let's assume we're reloading __init__.py first.

Say inside __init__.py, we have this import line:

from .module1 import *

So from here, python will go and look for whatever needs to be imported from module1.py. However, it's doing this before we have a chance to reload module1.py, hence whatever is imported will be from the old module.

The same thing will happen when we reload module1.py, if it depends in any way on module2.py.

Reloading a module after a separate module has already imported an object from it obviously will not change the behaviour of that imported object.

In summary

This issue is dependent on the import order of the modules within your package. Sometimes, reloading will show the changes, sometimes it won't.

To solve this issue, we need to recursively reload modules which are imported from within other modules. However this recursion isn't particularly reliable if two modules import each other. You've struck gold on this one, RJM :)

This is a pretty complex issue unfortunately, and something lots of projects have trouble with. Here's an example of why. We have a python package: ``` package | - __init__.py | - module1.py \ - module2.py ``` Say we make some modifications to the code in some of these modules, and want to reload it. Obviously, only one module can be interpreted at a time. So which one do we do first? It begs the question. What the `reload` command does is iterates over each module inside the package, reloading one after the other. For now let's assume we're reloading `__init__.py` first. Say inside `__init__.py`, we have this import line: ```py from .module1 import * ``` So from here, python will go and look for whatever needs to be imported from `module1.py`. However, it's doing this *before we have a chance to reload `module1.py`*, hence whatever is imported will be from the *old module*. The same thing will happen when we reload `module1.py`, if it depends in any way on `module2.py`. Reloading a module after a separate module has already imported an object from it obviously will not change the behaviour of that imported object. ### In summary This issue is dependent on the import order of the modules within your package. Sometimes, reloading will show the changes, sometimes it won't. To solve this issue, we need to recursively reload modules which are *imported* from within other modules. However this recursion isn't particularly reliable if two modules import each other. You've struck gold on this one, RJM :)
tekulvw commented 2018-04-03 02:50:14 +02:00 (Migrated from github.com)

Should be mostly fixed with #1458, closing for now. If this issue pops up again we'll need another issue with more detail on that specific case.

Should be mostly fixed with #1458, closing for now. If this issue pops up again we'll need another issue with more detail on that specific case.
Sign in to join this conversation.
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#1457
No description provided.