Skip to content

Own pending shell service loads across reloads - #11492

Open
bavanchun wants to merge 3 commits into
omacom:quattrofrom
bavanchun:fix/service-load-ownership-pr
Open

bavanchun wants to merge 3 commits into
omacom:quattrofrom
bavanchun:fix/service-load-ownership-pr

Conversation

@bavanchun

@bavanchun bavanchun commented Sep 12, 2026 •

Copy link
Copy Markdown

Two related defects in the shell's service loader let an enabled service-kind plugin end up with no live service, or with a service that can never admit itself. Both surface on third-party plugins that pair a service entry point with a bar widget or panel.

1. Pending service loads are not owned across reloads

ensureService compiled a component and registered whatever its finalize callback produced, with no claim on the in-flight load. When an asynchronous load completed after a reload had already started a fresh load for the same plugin, the obsolete finalizer still ran _services[key] = inst, displacing the fresh owner with a stale, inactive instance. _syncServices then skipped the key as occupied, so nothing recovered it until the next shell restart — where the same race could repeat.

The fix adds a per-key pending claim plus a reload generation:

  • one claim per key, so duplicate ensureService/_syncServices requests share a single in-flight load rather than racing;
  • revalidation of the enabled manifest, entry-point URL and claim ownership before construction, between every property injection and before registration, discarding a stale instance instead of publishing it;
  • callback detachment and component cleanup on cancellation, and a generation bump in unloadPluginServices so pending loads from a previous generation can never register.

Public service APIs, the injection order and synchronous creation are unchanged.

2. publicPluginManifest re-clones per call site

Every call independently JSON.parse(JSON.stringify(...))-ed the raw manifest, so a plugin's own injected manifest and the copy handed to its scoped PluginRegistryApi facade were different objects. A plugin that compares the two — to notice that its registration was superseded by a newer scan — saw them differ on every load, not just a stale one.

Reference equality is not a dependable contract here anyway: reading a plain JS object back out of a property var through the facade does not preserve strict identity (it arrives as a V4ReferenceObject wrapper). So the copy is now memoized per raw manifest object and stamped with __registryRevision, a plain number pinned to the scan generation the raw manifest was first seen in. Numbers survive the property round trip, so plugins have something reliable to compare. Memoization also removes the redundant re-cloning on every facade construction and rescan.

Tests

  • test/shell.d/service-lifecycle-test.sh — 45 Node cases driving the extracted production loader: late/duplicate completion order, disable/remove/rescan invalidation, loading/error/null status, reentrant creation and injection, cancellation, and unaffected neighbours. They fail against the unpatched loader and pass with it.
  • test/shell.d/service-lifecycle-runtime-test.sh — the same production block inside a real QtQuick host via qmltestrunner, covering component cleanup, real statusChanged disconnection and reload-during-construction/injection. Skips cleanly when qmltestrunner is absent.
    The existing authentication-boundary coverage in plugin-auth-boundary-test.sh still passes.

Verification

Built as a local omarchy package and installed on a 4.0.3 desktop. Before: an enabled third-party service plugin never got a live service instance, and once the loader race was fixed it was still rejected permanently by its own stale-registration check. After both fixes the service registers, admits itself and its helper process runs.

Happy to split this into two PRs or adjust naming — whatever suits.

Two independent call sites (ensureService's inst.manifest injection and
pluginRegistryFor's PluginRegistryApi facade) each called
publicPluginManifest(manifest) separately, producing two distinct
JSON-cloned copies for the same third-party plugin registration.

A keepLoaded, service-kind third-party plugin that compares its own
injected manifest against pluginRegistry.installedPlugins[pluginId] by
reference - to detect a stale registration surviving past a rescan - saw
that comparison fail on every load, never just a genuinely stale one,
because the two clones were never the same object.

Cache the scrubbed copy per raw manifest object (stable within one scan
generation; naturally invalidated by garbage collection once a rescan
replaces it) so every caller for the same raw manifest gets back the
identical copy, restoring reference-comparability without weakening the
isolation from the raw, internal-only manifest fields.
@bavanchun
bavanchun force-pushed the fix/service-load-ownership-pr branch from a135bf7 to 6458b79 Compare September 12, 2026 15:40
@omarchybot omarchybot added the bug Something isn't working label Sep 27, 2026
@omarchybot

Copy link
Copy Markdown
Collaborator

Reviewed at 6458b79 by Claude Opus 5.5 and, as a second opinion, Codex Medium, with the shell run on a disposable Omarchy ISO VM (Quickshell 0.3.1, Qt 6.11.2) linked to this branch and to its merge base. It is not ready to merge: the manifest cache in the second commit crashes the shell, and the race the first commit fixes did not show up on a stock install.

The WeakMap in publicPluginManifest segfaults quickshell. With a third-party service plugin enabled, touching its QML (the local-plugin reload) and running omarchy-shell shell rescanPlugins every few seconds crashed quickshell 7 times in 15 cycles, and 6 times in a second run of 20, until the launcher gave up relaunching it — the desktop is left with no bar and no shell. Every crash faults in QV4::Value::sameValueZero, the key comparison behind WeakMap.has/get/set. The same branch with only the _publicManifestCache.has/get and .set lines (shell/shell.qml:337 and :343) removed ran 20 cycles with no crash, and the merge base ran 33 with none. The new tests do not reach this path because both fixtures use first-party manifests (__isFirstParty: true at tst_service_lifecycle.qml:45), which return before the cache.

The load race was not reproduced. Instrumenting ensureService on the base showed every service component — first-party, a third-party service, and a service plus bar-widget plugin loaded from ~/.config/omarchy/plugins — coming back Ready from Qt.createComponent(..., PreferSynchronous), never Loading, and those services stayed alive through every reload, rescan and restart. service-lifecycle-test.sh fails on the base only because its fake createComponent returns Loading. Codex agrees the local-file case loads synchronously and points out two real ways it could still happen: a service that imports a remote QML module, and a service whose constructor or property handlers call back into the host (for example mutateShellConfig) before it is registered. Neither was seen to happen here. If a plugin of yours goes stale on 4.x, the plugin and the steps that make it fail would settle it.

Scope. The second change is an API addition, not a fix: docs/omarchy-shell.md promises third-party plugins detached manifest copies, not shared identity, so a plugin comparing two of them with === was relying on something the shell never offered. __registryRevision may still be worth having, but as its own pull request with defined semantics. For the first change, +819 lines, 489 of them a Node harness that extracts functions from shell.qml with a regex, is a lot for a race that needs a reproduction first. The smallest version would be a single ownership check before a load is published, cleared in unloadPluginServices, with a test that runs the real Qt loader.

./test/cli passed on the VM (112 ok), as did service-lifecycle-test.sh, service-lifecycle-runtime-test.sh and plugin-auth-boundary-test.sh. Nothing was pushed to the branch: dropping the cache would also drop the second change's purpose, and that call belongs to you. Waiting on you: remove the WeakMap (or split the manifest change into its own pull request), and show the service race failing on quattro.

@aholbreich

Copy link
Copy Markdown
Contributor

Thanks @bavanchun. The pending-service ownership race this addressed is fixed on quattro another way: 0810fdb90 (#19961) added the _pendingServices token scheme, so a stale in-flight load can no longer displace a fresh service after a reload, and pending loads are cancelled when their manifest/URL/enabled state changes. Recommend closing as delivered, crediting your ownership analysis and the service-lifecycle fixtures.

@omarchybot

Copy link
Copy Markdown
Collaborator

Thanks, @bavanchun, for the service lifecycle fixtures. GPT-6.2 Sol High in Codex, Opus 5.5 High, and muse-wonder via pi (high) compared this branch with current quattro. #19961 prevents a stale asynchronous load from registering after a reload. Opus traced a remaining case: ensureService drops its token before it injects properties, so a third-party service that saves its settings during initialization can trigger a second load before the first is registered. Muse Wonder considered this unlikely in practice. This pass was source-only because the release branches lack push protection.

Could you narrow the fix to keeping the existing token through registration and add a regression test that saves settings through the third-party API during initialization? Please leave out the manifest cache. It crashed Quickshell in the earlier worker review, and this branch still removes __sourceDir, which #11925 preserves so third-party plugins can find their bundled scripts.

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

Labels

bug Something isn't working

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants