Skip to content

collect_plugins_static_files() has no locking, so concurrent django-q processes corrupt plugin static output #12769

Closed

Description

@Kamaar

Please verify that this bug has NOT been raised before.

  • I checked and didn't find a similar issue

Describe the bug*

Related, and why this is not a duplicate

  • Server Error after restart with static files #12130 (closed as not planned) reports the same symptom. Its diagnosis, in a comment,
    is that a race occurs because invoke plugins was being run externally at the same time as
    the server started. That is one way to trigger it, but the concurrency is internal and needs
    no external trigger: see below. Nothing was changed, so the behaviour is still present.
  • Automatic plugin static files collection broken after install  #7709 (closed as completed, via Docker venv #8502) is the older umbrella issue for plugin static
    files. It fixed the "files never collected" symptom; it did not make the collector safe to run
    from several processes at once, which is what this is about.

Description

install_plugins_file() calls plugin.staticfiles.collect_plugins_static_files() with no
synchronisation, and the guard that is supposed to stop it running repeatedly is process-local:

# plugin/registry.py
def install_plugin_file(self):
    file_hash = plugins_file_hash()
    if file_hash != settings.PLUGIN_FILE_HASH:
        install_plugins_file()
        settings.PLUGIN_FILE_HASH = file_hash

settings.PLUGIN_FILE_HASH starts as '' (set in settings.py) and is only ever assigned in
memory, so every process sees a mismatch on every start and runs the installer. With a
server plus django-q workers, that is N concurrent pip install runs and N concurrent static
collections per restart — not once per plugins-file change, as the guard implies.

registry.set_ready() reaches the same call for any process that is either the main thread or a
worker thread:

if InvenTreeSetting.get_setting('PLUGIN_ON_STARTUP', create=False, cache=False):
    ...
    elif InvenTree.ready.isInWorkerThread() or InvenTree.ready.isInMainThread():
        registry.install_plugin_file()

So no external invoke plugins is needed to collide with startup — the processes collide with
each other. The tracebacks below come from django_q/worker.py in some cases and
django_q/monitor.py in others, in a single restart.

copy_plugin_static_files() clears the destination directory and then copies into it, so two
processes interleaving those phases interfere with each other in three distinct ways:

  1. clear_static_dir() → staticfiles_storage.delete(path) → os.rmdir fails, because another
    process has already begun writing files into the directory being removed.
  2. copy_plugin_static_files() → staticfiles_storage.save() → os.chmod fails, because
    another process deleted the file between os.open creating it and os.chmod running on it.
  3. Files are written under Django's collision-avoidance names (name_XXXXXXX.ext), which only
    happens when the target name already exists — i.e. another process has already written it.

The caller wraps the whole thing in try/except and calls log_error(...), then returns
True. So installation is reported as successful while the static directory is left partially
populated, and the plugin's UI silently fails to load. The errors are only visible in the error
log.

Steps to Reproduce

  1. Deploy with Docker using the standard compose file, so the server and the
    django-q worker run as separate processes.
  2. Enable plugins (INVENTREE_PLUGINS_ENABLED=true), and in Settings → Plugins
    enable app, URL and interface integration. Leave "Check plugins on startup"
    at its default, which is on for Docker deployments.
  3. Install any plugin that ships a static/ directory — for example
    inventree-bulk-plugin, or any plugin generated by the plugin creator with the
    UserInterfaceMixin. The more files it ships, the wider the window; a plugin
    with 29 static files reproduced this consistently.
  4. Restart: docker compose restart inventree-server inventree-worker
  5. Check Settings → Error Reports. There are several entries from
    plugin/staticfiles.py, and the tracebacks originate in different processes —
    django_q/worker.py in some, django_q/monitor.py in others.
  6. Request one of the plugin's static files:
    curl -I http:///static/plugins//Panel.js
    It returns 404 while other /static/ paths return 200, and the interface shows
    "Error Loading Plugin Content — Failed to load module".
  7. Repeat step 4 a few times. It does not fail every time, which is what makes
    it a race rather than a deterministic bug.

Expected behaviour

Collecting a plugin's static files should happen once per restart regardless of
how many processes are running, and should never leave the destination directory
partially populated or empty.

Two related expectations:

  • The guard in registry.install_plugin_file() reads as "run this once per change
    of the plugins file", but settings.PLUGIN_FILE_HASH is process-local and starts
    empty, so it actually runs once per process per start.
  • install_plugins_file() logs a failed static collection and then returns True,
    so an installation whose UI assets are incomplete is reported as successful.

Deployment Method

Docker

Version Information

InvenTree-Version: 1.5.2
Django Version: 5.2.16
Commit Hash: f15e35c
Commit Date: 2026-08-25
Commit Branch: null
Database: django.db.backends.postgresql
Debug-Mode: False
Deployed using Docker: True
Platform: Linux-5.15.0-190-generic-x86_64-with-glibc2.41
Installer: DOC
Active plugins: [{"name":"InvenTreeBarcode","slug":"inventreebarcode","version":"2.2.0"},{"name":"BOM Exporter","slug":"bom-exporter","version":"1.1.0"},{"name":"InvenTree Exporter","slug":"inventree-exporter","version":"1.0.0"},{"name":"Parameter Exporter","slug":"parameter-exporter","version":"2.0.0"},{"name":"InvenTreeEmailNotifications","slug":"inventree-email-notification","version":"1.0.0"},{"name":"InvenTreeUINotifications","slug":"inventree-ui-notification","version":"1.0.0"},{"name":"InvenTreeCurrencyExchange","slug":"inventreecurrencyexchange","version":"1.0.0"},{"name":"InvenTreeMachines","slug":"inventree-machines","version":"1.0.0"},{"name":"InvenTreeLabel","slug":"inventreelabel","version":"1.1.0"},{"name":"InvenTreeLabelMachine","slug":"inventreelabelmachine","version":"1.0.0"},{"name":"InvenTreeLabelSheet","slug":"inventreelabelsheet","version":"1.0.1"},{"name":"DigiKeyBarcodePlugin","slug":"digikeyplugin","version":"1.0.1"},{"name":"LCSCBarcodePlugin","slug":"lcscplugin","version":"1.0.1"},{"name":"MouserBarcodePlugin","slug":"mouserplugin","version":"1.0.1"},{"name":"TMEBarcodePlugin","slug":"tmeplugin","version":"1.0.1"},{"name":"BatchCodePlugin","slug":"batchcode","version":"2.0.2"},{"name":"KiCadLibraryPlugin","slug":"kicad-library-plugin","version":"2.0.4"}]

Try to reproduce on the demo site

I did not try to reproduce

Is the bug reproducible on the demo site?

Not reproducible

Relevant log output

OSError: [Errno 39] Directory not empty: '/home/inventree/data/static/plugins/<slug>'
  File "/home/inventree/src/backend/InvenTree/plugin/installer.py", line 157, in install_plugins_file
    plugin.staticfiles.collect_plugins_static_files()
  File "/home/inventree/src/backend/InvenTree/plugin/staticfiles.py", line 45, in collect_plugins_static_files
    copy_plugin_static_files(slug, check_reload=False)
  File "/home/inventree/src/backend/InvenTree/plugin/staticfiles.py", line 90, in copy_plugin_static_files
    clear_static_dir(destination_prefix)
  File "/home/inventree/src/backend/InvenTree/plugin/staticfiles.py", line 33, in clear_static_dir
    staticfiles_storage.delete(path)
  File "/root/.local/lib/python3.14/site-packages/django/core/files/storage/filesystem.py", line 182, in delete
    os.rmdir(name)

FileNotFoundError: [Errno 2] No such file or directory: '/home/inventree/data/static/plugins/<slug>/Settings.js.map'
  File "/home/inventree/src/backend/InvenTree/plugin/installer.py", line 157, in install_plugins_file
    plugin.staticfiles.collect_plugins_static_files()
  File "/home/inventree/src/backend/InvenTree/plugin/staticfiles.py", line 45, in collect_plugins_static_files
    copy_plugin_static_files(slug, check_reload=False)
  File "/home/inventree/src/backend/InvenTree/plugin/staticfiles.py", line 112, in copy_plugin_static_files
    staticfiles_storage.save(destination_path, src)
  File "/root/.local/lib/python3.14/site-packages/django/core/files/storage/base.py", line 49, in save
    name = self._save(name, content)
  File "/root/.local/lib/python3.14/site-packages/django/core/files/storage/filesystem.py", line 156, in _save
    os.chmod(full_path, self.file_permissions_mode)

A third occurrence failed on 'assets/messages-BwzuZfs7_tcbtl27.js.map' — a name
carrying Django's collision-avoidance suffix, which only appears when the target
name already exists, i.e. another process had already written it.

Not reproducible on the demo site: this needs a multi-process deployment to be
restarted, which the demo does not allow.

Activity

  1. added
    bug
    question
    triage:not-checked
    on Sep 2, 2026
  2. wolflu05 commented on Sep 2, 2026

    @wolflu05
    Member

    If that's true, good catch, this might be related to my issue. Was searching it and then discovered that you actually linked it :)

  3. Kamaar commented on Sep 2, 2026

    @Kamaar
    Author

    Thanks — #12130 is what put me on the trail. Your comment there, about two
    collectors running at the same time and files being deleted from under another
    process, is exactly the mechanism. The only correction is that it needs no
    external invoke plugins: the processes collide with each other.

    That part is cheap to check, because install_plugins_file() logs on entry:

    # plugin/installer.py:133
    logger.info('Installing plugins from plugins file')
    

    If the PLUGIN_FILE_HASH guard did what its shape suggests — run once per
    change of the plugins file — that line would appear once. Restart a Docker
    stack with the worker running and count it:

    docker compose restart inventree-server inventree-worker
    docker compose logs --since 5m | grep -c "Installing plugins from plugins file"
    

    It appears once per process, on every restart, because PLUGIN_FILE_HASH is
    '' in settings.py:250 and is only ever assigned in memory — so no process
    ever observes another process's hash.

    Worth noting that this also means the pip install -U -r plugins.txt inside
    install_plugins_file() runs N times per restart, which is a cost of its own
    quite apart from the static files being corrupted.

    Two questions:

    • Would it be worth reopening Server Error after restart with static files #12130? It was closed on the assumption that a
      concurrent external invoke plugins was required, which turns out not to be
      the case.
    • Of the possible fixes, persisting the hash (a common setting, or the cache)
      looks like the one that removes the concurrency rather than tolerating it —
      it makes the installer run once per change, as intended. Locking
      collect_plugins_static_files() or building into a temporary directory and
      swapping it in would harden the collector, but N concurrent pip install
      runs per restart would remain.

    Happy to test a patch on the instance where this reproduces.

  4. added
    plugin
    and removed
    triage:not-checked
    on Sep 2, 2026
  5. added this to the 1.6.0 milestone on Sep 2, 2026
  6. SchrodingersGat commented on Sep 2, 2026

    @SchrodingersGat
    Member

    @Kamaar thanks for the report, certainly worth looking into deeper. I will review and get back to you.

  7. SchrodingersGat commented on Sep 3, 2026

    @SchrodingersGat
    Member

    A nominal fix for this is incoming in #12776 - please review

  8. matmair commented on Sep 6, 2026

    @matmair
    Member

    closed via #12776

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugpluginquestion

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions