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

Incorrect unload order for submodules #38

Closed
ostr00000 opened this issue Jan 19, 2024 · 3 comments
Closed

Incorrect unload order for submodules #38

ostr00000 opened this issue Jan 19, 2024 · 3 comments
Assignees
Labels

Comments

@ostr00000
Copy link

I believe that this code:

# Unload submodules
for key in [key for key in sys.modules.keys()]:
if '{}.'.format(plugin) in key:
if hasattr(sys.modules[key], 'qCleanupResources'):
sys.modules[key].qCleanupResources()
del sys.modules[key]

should be executed after call to unloadPlugin method (but before loadPlugin):
startTime = time()
unloadPlugin(plugin)
loadPlugin(plugin)
pluginStarted = startPlugin(plugin)

This is because one could make local import in unload function to access for example a global object. If the submodule with the global object is unloaded first, then a new global object is created.
Current behavior is inconsistent with manual reload (menuBar->Plugins->Manage and Install Plugins...->uncheck a plugin and then check it again) - I have no error when reloading plugin from QGIS.


Note: Why should one perform local import in initGui and unload methods in QGIS plugin instead of global import?
Because submodules should only be loaded if the plugin is active.
It is especially severe, when a submodule have long load time (ex. fetching data from DB) and despite plugin been disabled, it still impacts QGIS startup time.

@borysiasty borysiasty self-assigned this Mar 8, 2024
@borysiasty
Copy link
Owner

Hi,

I apologize for the late reply (and the wrong order of commands). Thanks a lot for your investigation, I'll fix it within upcoming weeks!

@borysiasty borysiasty added the bug label Mar 8, 2024
@borysiasty
Copy link
Owner

Since QGIS 3.8.4, the unloading submodules is implemented in qgis.utils.unloadPlugin:

https://github.com/qgis/QGIS/pull/9851/files

so I'm just removing it from here. Will be released soon.

borysiasty added a commit that referenced this issue Jul 25, 2024
@borysiasty
Copy link
Owner

@ostr00000 Update: Finally I leave that code after unloadPlugin as you proposed, as it turns out that unloadPlugin sometimes leaves leftovers.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants