Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[BUG]: Updating or reloading plugins does not reload imported files. #3223

Closed
Jerrie-Aries opened this issue Nov 8, 2022 · 1 comment · Fixed by #3226
Closed

[BUG]: Updating or reloading plugins does not reload imported files. #3223

Jerrie-Aries opened this issue Nov 8, 2022 · 1 comment · Fixed by #3226
Labels
bug This is a confirmed bug

Comments

@Jerrie-Aries
Copy link
Contributor

Bot Version

v4.0.1

How are you hosting Modmail?

Other

Error Logs

None

Screenshots

No response

Additional Information

This only occurs on plugins that use multiple files.
More in-depth explanations can be found on discord.py discussions, here.

This happened to me quite a few times now since most of my plugins use multiple files. Updating the plugins does not re-import other files in same directory or sub-directories.

Steps to produce:

  • Create a plugin with multiple files. Example:
    ├── plugin_folder
    │ ├── plugin.py
    │ ├── views.py
  • Make a change in views.py.
  • Update the plugin using ?plugin update ... command. The changes in views.py do not take affect (at least until bot restart). If new attributes were added in a class in views.py, accessing it from plugin.py would raise AttributeError.
@Jerrie-Aries Jerrie-Aries added the maybe: bug An unconfirmed bug label Nov 8, 2022
@Jerrie-Aries
Copy link
Contributor Author

Jerrie-Aries commented Nov 9, 2022

Side note:
Internally when unloading an extension, discord.py has already done the unloading sub-modules thing here, specifically in Bot._call_module_finalizers().
However that will only work if the entry point (where async def setup() is) of the extension is in __init__.py file, which means all the files and directories in the same folder as the __init__.py will be considered as the sub-modules of the extension and will automatically be unloaded when unloading the extension.
Example:
├── myplugin
│ ├── __init__.py
│ ├── views.py
│ ├── models.py
Here the extension string would be something like plugins.myplugin.

Since the file structure for modmail plugins is quite different, and the entry point is not in __init__.py, files and directories in the same folder as the main plugin file (e.g. myplugin.py) will not be unloaded when unloading the plugin.
Example:
├── myplugin
│ ├── myplugin.py
│ ├── views.py
│ ├── models.py
Here the extension string would be something like plugins.myplugin.myplugin.

Luckily, unloading the modules or files in plugin's directory can be done manually, whether in the core of the bot (cogs/plugins.py), or plugin developers can implement it themselves in their plugins.
Possible solution, after unloading the extension do the cleanup:

# plugin here is the Plugin object
ext_parent = ".".join(plugin.ext_string.split(".")[:-1])
for module in list(sys.modules.keys()):
    if module == ext_parent or module.startswith(ext_parent + "."):
        del sys.modules[module]

Jerrie-Aries added a commit to Jerrie-Aries/modmail that referenced this issue Nov 9, 2022
@StephenDaDev StephenDaDev added bug This is a confirmed bug and removed maybe: bug An unconfirmed bug labels Mar 14, 2023
@Jerrie-Aries Jerrie-Aries linked a pull request Mar 25, 2023 that will close this issue
raidensakura pushed a commit to raidensakura/modmail that referenced this issue Apr 10, 2024
* Cleanup after unloading extension, resolve modmail-dev#3223.

* Remove leftover modules loaded from `plugins` path when purging.

---------

Co-authored-by: Taku <[email protected]>
khakers pushed a commit to khakers/OpenModmail that referenced this issue Jun 18, 2024
* Cleanup after unloading extension, resolve modmail-dev#3223.

* Remove leftover modules loaded from `plugins` path when purging.

---------

Co-authored-by: Taku <[email protected]>

(cherry picked from commit a784f82)
Signed-off-by: Khakers <[email protected]>
khakers pushed a commit to khakers/OpenModmail that referenced this issue Jun 18, 2024
* Cleanup after unloading extension, resolve modmail-dev#3223.

* Remove leftover modules loaded from `plugins` path when purging.

---------

Co-authored-by: Taku <[email protected]>

(cherry picked from commit a784f82)
Signed-off-by: Khakers <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug This is a confirmed bug
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants