Skip to content

Plugin load fix - #12776

Merged
SchrodingersGat merged 17 commits into
inventree:masterfrom
SchrodingersGat:plugin-load-fix
Sep 6, 2026
Merged

SchrodingersGat merged 17 commits into
inventree:masterfrom
SchrodingersGat:plugin-load-fix

Conversation

@SchrodingersGat

Copy link
Copy Markdown
Member

Addresses concurrency issues related to installing plugins from file, as identified in #12769

Additionally ensures that static file installation (via plugins) is now concurrency safe - preventing partial file updates or file corruption.

@SchrodingersGat SchrodingersGat added this to the 1.6.0 milestone Sep 3, 2026
@SchrodingersGat SchrodingersGat added bug Identifies a bug which needs to be addressed plugin Plugin ecosystem backport Apply this label to a PR to enable auto-backport action backport-to-1.5.x labels Sep 3, 2026
@netlify

netlify Bot commented Sep 3, 2026 •

Copy link
Copy Markdown

✅ Deploy Preview for inventree-web-pui-preview canceled.

Name Link
🔨 Latest commit a541b4e
🔍 Latest deploy log https://app.netlify.com/projects/inventree-web-pui-preview/deploys/6a9d5be02f2d890008cc4408

@codecov

codecov Bot commented Sep 3, 2026 •

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 76.25755% with 118 lines in your changes missing coverage. Please review.
✅ Project coverage is 86.61%. Comparing base (4a0e285) to head (a541b4e).

❌ Your project check has failed because the head coverage (91.17%) is below the target coverage (92.00%). You can increase the head coverage or adjust the target coverage.

Additional details and impacted files
@@            Coverage Diff             @@
##           master   #12776      +/-   ##
==========================================
- Coverage   87.15%   86.61%   -0.54%     
==========================================
  Files        1484     1486       +2     
  Lines      100861   102460    +1599     
  Branches    11524    11519       -5     
==========================================
+ Hits        87901    88745     +844     
- Misses      12898    13650     +752     
- Partials       62       65       +3     
Flag Coverage Δ
backend 91.18% <89.82%> (+0.03%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

Components Coverage Δ
Backend Apps 91.17% <76.16%> (-1.27%) ⬇️
Backend General 94.71% <ø> (ø)
Frontend 79.85% <ø> (+0.06%) ⬆️
🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@Kamaar

Kamaar commented Sep 3, 2026

Copy link
Copy Markdown

Reviewed the diff — this looks right, and it addresses the root cause rather than the symptom. Confirmed there are no settings.PLUGIN_FILE_HASH references left on the branch.

One thing I would raise: _claim_hash_setting() writes the new hash before the work is done, and _release_hash_claim() reverts it on failure. The finally covers exceptions, but not the process being killed outright — a container stopped mid-install, or the OOM killer. In that case _PLUGIN_FILE_HASH is left at the new value with the install never having run, and once the lease expires every other process sees current == new_value and skips. Plugins would then stay uninstalled until the plugins file changes again.

That is a narrow regression against today's behaviour, where every process retries on every start. Writing the hash only after a successful install would avoid it: the lease alone is enough to stop concurrent runs, since a process that cannot acquire it skips anyway, and a kill would simply leave the hash unchanged so the next start retries.

Minor: staticfiles.py now imports _acquire_lease_blocking and _release_lease from plugin.registry. Since they have become cross-module API, they might be worth promoting out of the underscore namespace, or into a small shared module.

I have an instance that reproduces the original bug reliably (Docker, server plus worker, a plugin shipping 29 static files). Happy to run this branch there and report back — it would take re-enabling PLUGIN_ON_STARTUP, which is currently off as the workaround.

@Kamaar

Kamaar commented Sep 3, 2026

Copy link
Copy Markdown

Tested this branch on the instance where the original bug reproduces.

Method. I could not run master wholesale on a 1.5.2 deployment — master's registry.py imports get_wellknown_urls from plugin/urls.py, which does not exist in 1.5.2 — so I applied only this PR's hunks for plugin/registry.py and plugin/staticfiles.py onto the 1.5.2 files. They applied cleanly, with line offsets only, and staticfiles.py is byte-identical between 1.5.2 and master before this PR. The patched files went into both the server and the worker container, since the collision between those two processes is the thing being tested. PLUGIN_ON_STARTUP was re-enabled, so the code path under test is live.

Result. Five consecutive docker compose restart inventree-server inventree-worker cycles:

run 1: http=200  files=15  staticfiles errors=0
run 2: http=200  files=15  staticfiles errors=0
run 3: http=200  files=15  staticfiles errors=0
run 4: http=200  files=15  staticfiles errors=0
run 5: http=200  files=15  staticfiles errors=0

Verified independently through the API afterwards: all 12 files listed in the plugin's .vite/manifest.json are served with 200, the panel bundle imports and exports the expected symbol, and the plugin's own endpoints respond. Before this branch, the same instance lost the static directory reliably across restarts.

Caveat worth stating: a race is probabilistic, so five clean restarts is a good signal rather than a proof. But the previous behaviour was consistent enough that five failures would have been the expected outcome without the fix.

Unrelated finding, while getting the instance into a testable state. My plugins.txt had accumulated three entries for the same package — @v2.0.0, @v2.0.1 and @main — which made pip install -r plugins.txt fail with ResolutionImpossible, so no plugin was installed at all. The cause is in update_plugins_file():

def compare_line(line: str):
    return re.match(rf'^{package_reference}[\s=@]', line.strip())

For a VCS install package_reference is name@git+url@tag, so installing the same plugin at a different tag does not match the existing line and gets appended instead of replacing it. package_reference is also interpolated into the regex unescaped, and it contains +, . and / — in git+https the + becomes a quantifier — so the comparison is unreliable even for an identical reference. Happy to open that as its own issue if useful; it seems distinct from this one.

Kamaar added a commit to Kamaar/inventree-batchcode-plugin that referenced this pull request Sep 3, 2026
inventree/InvenTree#12776 replaces the process-local PLUGIN_FILE_HASH with a
persisted setting plus a cross-process lease, and makes the static collection
stage into a temporary directory before writing to the live destination. It is
milestoned 1.6.0 and labelled for backport to 1.5.x, but is not merged yet.

Tested on the instance the analysis came from: with both production files
patched into the server and worker containers and PLUGIN_ON_STARTUP re-enabled,
five consecutive restarts left the plugin's static directory complete and
served, where the unpatched code lost it reliably.

The README now says to check whether your version already carries the fix
before following the workaround, rather than presenting it as unconditional.

Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
@matmair

matmair commented Sep 3, 2026

Copy link
Copy Markdown
Member

@Kamaar stop posting verbatim AI chat slop into discussion with other humans; these ultra long texts that wonder of in unrelated issues waste everyone’s time

@matmair

matmair commented Sep 6, 2026

Copy link
Copy Markdown
Member

@SchrodingersGat once this is merged I would love to have a point release (I have prepped #12795) as this could explain some hard-to-reproduce deployment issues

@SchrodingersGat
SchrodingersGat merged commit b4cb9a9 into inventree:master Sep 6, 2026
44 of 45 checks passed
@SchrodingersGat
SchrodingersGat deleted the plugin-load-fix branch September 6, 2026 21:45
@github-actions

github-actions Bot commented Sep 6, 2026

Copy link
Copy Markdown
Contributor

💚 All backports created successfully

Status Branch Result
✅ 1.5.x

Questions ?

Please refer to the Backport tool documentation and see the Github Action logs for details

SchrodingersGat added a commit that referenced this pull request Sep 7, 2026
* Robustify plugin file loading

* Lock static file copying

* Improve robustness of static file copying

* Add regression tests

* Handle database not being ready

* Fix identified limitation and refactor code

* Attempt to fix CI job

* instrumentation for CI - will revert

* Another bugfix attempt

(cherry picked from commit b4cb9a9)

Co-authored-by: Oliver <[email protected]>
@matmair

matmair commented Sep 8, 2026

Copy link
Copy Markdown
Member

question: how does one auqire a lock if there is no database (during initial setup)

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

Labels

backport Apply this label to a PR to enable auto-backport action backport-to-1.5.x bug Identifies a bug which needs to be addressed plugin Plugin ecosystem

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants