inventree / InvenTree

Open Source Inventory Management System
https://docs.inventree.org
MIT License
4.04k stars 722 forks source link

Apps get initialized two times #6412

Closed wolflu05 closed 1 week ago

wolflu05 commented 6 months ago

Please verify that this bug has NOT been raised before.

Describe the bug*

The app configs ready code gets run twice (isPluginRegistryLoaded and isInMainThread is already applied before).

Steps to Reproduce

To see this behavior, e.g. just apply the following diff, run the app :

diff --git a/InvenTree/label/apps.py b/InvenTree/label/apps.py
index d40752d7f..346ef1670 100644
--- a/InvenTree/label/apps.py
+++ b/InvenTree/label/apps.py
@@ -40,6 +40,7 @@ class LabelConfig(AppConfig):
             and not InvenTree.ready.isImportingData()
         ):
             try:
+                print('===> INIT LABELS')
                 self.create_labels()  # pragma: no cover
             except (
                 AppRegistryNotReady,

Expected behaviour

The ready filters filter everything out, so that the ready code is only run once.

I already knew that I fixed this behavior in #5005 so I git bisected it and found out, that #5649 (06eb948528e945b41ea3ceb7ae75709eb1d639e0) is the first bad commit. Do you have any idea what is causing that?

Deployment Method

Version Information

Version Information:

InvenTree-Version: 0.14.0 dev Django Version: 3.2.23 Commit Hash: f88d557 Commit Date: 2024-02-05 Commit Branch: master Database: sqlite3 Debug-Mode: True Deployed using Docker: False Platform: Linux-6.5.11-linuxkit-x86_64-with-glibc2.31 Installer: GIT

Active plugins: []

Please verify if you can reproduce this bug on the demo site.

Relevant log output

Python version 3.10.11 - /workspaces/InvenTree/dev/venv/bin/python3
2024-02-05 10:02:06,026 INFO DB_ENGINE: django.db.backends.sqlite3
2024-02-05 10:02:06,026 INFO DB_NAME: /workspaces/InvenTree/dev/database.sqlite3
2024-02-05 10:02:06,026 INFO DB_HOST: ''
2024-02-05 10:02:06,032 INFO InvenTree running with DEBUG enabled
2024-02-05 10:02:06,033 INFO MEDIA_ROOT: '/workspaces/InvenTree/dev/media'
2024-02-05 10:02:06,033 INFO STATIC_ROOT: '/workspaces/InvenTree/dev/static'
Loading InvenTree plugins
Installing plugins from plugins file
Running pip command: /workspaces/InvenTree/dev/venv/bin/python3 -m pip install -r /workspaces/InvenTree/dev/plugins.txt
Plugin Registry: Reloading plugins - Force: True, Full: True, Collect: True
Collecting plugins
Added plugin directory: '/workspaces/InvenTree/dev/plugins' as '/workspaces/InvenTree/dev/plugins'
/workspaces/InvenTree/dev/venv/lib/python3.10/site-packages/allauth/exceptions.py:9: UserWarning: allauth.exceptions is deprecated, use allauth.core.exceptions
  warnings.warn("allauth.exceptions is deprecated, use allauth.core.exceptions")
Collected 34 plugins
Start unloading plugins
Loading InvenTree plugins
Finished unloading plugins
Loading plugins
Found 14 active plugins
Registering IntegrationPlugin apps
Loading InvenTree plugins
===> INIT LABELS
Starting background tasks...
Started 18 scheduled background tasks...
Checking for pending database migrations
Plugin registry hash has changed - reloading
There are 0 pending migrations
Enqueued [InvenTree] 15083
Found 3 notification methods
Registering UrlsMixin Plugin
Enqueued [InvenTree] 15084
Enqueued [InvenTree] 15085
Plugin Registry: Loaded 14 plugins
===> INIT LABELS
Starting background tasks...
Started 18 scheduled background tasks...
Checking for pending database migrations
There are 0 pending migrations
Enqueued [InvenTree] 15086
Found 3 notification methods
Watching for file changes with StatReloader
Watching for file changes with StatReloader
Performing system checks...

System check identified no issues (1 silenced).
February 05, 2024 - 10:02:17
Django version 3.2.23, using settings 'InvenTree.settings'
Starting development server at http://127.0.0.1:8000/
Quit the server with CONTROL-C.
SchrodingersGat commented 6 months ago

@wolflu05 thanks for giving this some attention. I remember there was an issue when @afkiwers was developing the inventree-kicad-plugin where a dual-load of custom plugin models caused some issues. This is probably the same "bug".

matmair commented 6 months ago

This is expected behaviour IMO. All (django) apps must be reloaded for model discovery once AppMixin plugins are activated thus running all ready() methods twice (once during "normal" startup and once during our forced "plugin" mode-rediscovery). I think this implementation is the only one that makes sense because prior to https://github.com/inventree/InvenTree/pull/5649/ we had random issues with half-patched model registries.

wolflu05 commented 6 months ago

But why can't we just wait until everything in the plugin registry is ready and then run the ready functions of the label app/.... this is what the isPluginRegistryLoaded function was for. It should early return in the ready function if the apps are not altered loaded correctly, and only run the rest if all reloads are done. And it was already working, but broke with the linked PR.

matmair commented 6 months ago

From what I rember we had multiple unexplainable problems with plugin models before the PR and I am not aware of any after. There were these random plugin errors in the CI that disappeared after that PR and problems with notifications plugins that used custom models sometimes raising error about not been initilaized.

github-actions[bot] commented 4 months ago

This issue seems stale. Please react to show this is still important.

wolflu05 commented 4 months ago

I think this Is still happening.

github-actions[bot] commented 2 months ago

This issue seems stale. Please react to show this is still important.

wolflu05 commented 2 months ago

I think this Is still happening.

github-actions[bot] commented 1 week ago

This issue seems stale. Please react to show this is still important.

wolflu05 commented 1 week ago

I have tried to check this again, cannot reproduce on a production setup anymore, so closing that out now.