Repository navigation
fix(toolbar): remoteData never updates script scope (state_unsafe_mutation on flush) - #2017
Conversation
Toolbar items are created inside ItemsGroup's derived `entries`, and the toolbar flushes constantly (clock ticks once a second, scope updates, etc.). When a `remoteData` fetch completion lands while such a flush is in progress, the current reaction is a derived, and the `fetchedData[key] = data` write is rejected with Svelte's `state_unsafe_mutation` — the fetched payload never lands in the script scope and templates read `undefined` forever. The write is intentionally untracked: it reads no reactive state, it only publishes the fetched payload.
[Medium impact] The latest commit appears safe to merge; the remaining comment error is non-blocking. SummaryThe latest commit rewrites the explanation around
Reviews (2) · Last reviewed commit: "fix(toolbar): reword remoteData comment ..." · Reviewed by Greptile |
| // Item components (and this resolver) are created inside ItemsGroup's | ||
| // derived `entries`, and the toolbar flushes constantly (clock ticks, | ||
| // scope updates). When a fetch completion lands while such a flush is | ||
| // in progress, the current reaction is a derived and this write gets | ||
| // rejected with `state_unsafe_mutation`, so the payload never reaches | ||
| // the script scope and the item renders `undefined` forever. The write | ||
| // is intentionally untracked: it reads no reactive state. |
There was a problem hiding this comment.
Comment misstates execution order
ItemsGroup builds plain entries inside $derived.by; it creates components afterward in {#each}. A fetch continuation also cannot interrupt synchronous derived code on the same JavaScript thread. Remove or rewrite this explanation so future maintainers do not chase the wrong cause.
Note: If this suggestion doesn't match your team's coding style, reply to this and let me know. I'll remember it for next time!
There was a problem hiding this comment.
Good catch, agreed — components are created in the {#each} render effects (not inside the derived evaluation), and a fetch continuation can't interrupt synchronous derived code. Rewritten in cf6e85a: the comment now only states verifiable facts — the set() guard itself (DERIVED/BLOCK_EFFECT/ASYNC/EAGER_EFFECT, sources.js), that the toolbar flushes such reactions constantly, and that the write reads no reactive state. The PR body's root-cause paragraph was updated the same way (it no longer claims the ItemsGroup-derived mechanism).
The bug itself is independent of the exact carrier — the repro in the PR description fails consistently without the patch (fresh 2.8.10 install, template reading the remote key) and the payload lands with the untrack().
Greptile rightly pointed out that the previous comment claimed components are created inside ItemsGroup's derived evaluation and that a fetch continuation can interrupt synchronous derived code — neither is true. Rewrite the comment to state only verifiable facts: the set() guard (DERIVED/BLOCK_EFFECT/ASYNC/EAGER_EFFECT), the constant flushing, and that the write itself reads no reactive state.
|
@greptileai could you re-review the latest commit (cf6e85a)? Your P2 note in #2017 (comment) was addressed there — the in-code comment was rewritten to only state verifiable facts, and the PR description was updated the same way. Posting this as a top-level mention because the Retrigger link hasn't produced a new review. |
Live verification report (Windows 11, Seelen UI 2.8.11, WebView2 157.0.4341.60)Bug reproduction on 2.8.11 — confirmed. With a toolbar item declaring repeating exactly every 5 s on both toolbar instances (one per monitor), matching the fetch interval. The item never renders — the payload never reaches the template scope, consistent with the analysis above. Fix — verified in an isolated simulation and by CI; a live binary E2E was not safely completable. A local |
Summary
remoteDatais currently unusable for toolbar plugins: every fetch completion throws Svelte'sstate_unsafe_mutationand the fetched payload never reaches the plugin script scope. This PR wraps the scope write inuntrack()so completions arriving inside a write-rejecting reaction context are not rejected.The bug (repro + analysis)
Any toolbar item declaring
remoteData:Expected: after the fetch completes,
zenbecomes available in the script scope and the item re-renders with the fetched data.Actual:
Network tab: the request completes with 200 OK — networking and CORS are fine (tested against
https://api.github.com/zenand anAuthorization-bearinghttps://api.z.ai/...endpoint; the latter passes CORS preflight withAccess-Control-Allow-Originreflecting the webview originhttp://tauri.localhost).Console: every fetch completion throws
The throw comes from
fetchedData[key] = dataincreateRemoteDataResolver(src/ui/svelte/fancy-toolbar/remoteData.svelte.ts), and it is flaky rather than strictly deterministic: right after some restarts a write lands before the flush loop warms up (fetched data rendered exactly once), then it starts failing consistently.The script scope therefore never contains the remote key: templates reading it fail with
ReferenceErrorbefore the first fetch, and observeundefinedforever after (guarded templates render"undef"indefinitely).Root cause: Svelte's
set()rejects$statewrites whenever the active reaction has theDERIVED/BLOCK_EFFECT/ASYNC/EAGER_EFFECTflags (sources.js), and the toolbar flushes such reactions constantly (the clock ticks once a second, scopes update, item content re-derives). The fetch-completion microtask can land inside such a write-rejecting context, the write throws, and the payload is lost. Workarounds likerequestInit.cache: "no-store", shorterupdateIntervalSeconds, or re-adding the item all fail because the resolver write itself is the broken step.The fix
Wrap the write in
untrack()— it reads no reactive state, it only publishes the fetched payload, anduntrack()clears the rejecting reaction context for its duration:Testing
npm run type-check,npm test,deno lint/deno fmtand the repo's lefthook gates (file-format, ts-type-check, js-linter, commit-msg) all pass.Related: #2016 adds a
Windowsscope as a scope-based alternative data path; with this fix both mechanisms work.