{"record":{"id":"7bad00447dd7f1c3","repo":"siyuan-note/siyuan","slug":"view-state-service-has-been-destroyed","errorCode":null,"errorMessage":"View state service has been destroyed","messagePattern":"View state service has been destroyed","errorType":"exception","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"app/src/util/viewState.ts","lineNumber":260,"sourceCode":"\n    private scheduleFlush() {\n        this.clearFlushTimer();\n        this.flushTimer = setTimeout(() => {\n            this.flushTimer = undefined;\n            this.flush().catch((error) => console.error(error));\n        }, this.flushDelay);\n    }\n\n    private clearFlushTimer() {\n        if (this.flushTimer !== undefined) {\n            clearTimeout(this.flushTimer);\n            this.flushTimer = undefined;\n        }\n    }\n\n    private ensureActive() {\n        if (this.destroyed) {\n            throw new Error(\"View state service has been destroyed\");\n        }\n    }\n}\n","sourceCodeStart":242,"sourceCodeEnd":264,"githubUrl":"https://github.com/siyuan-note/siyuan/blob/9f775e8a12daef8255556097396f9b2739078892/app/src/util/viewState.ts#L242-L264","documentation":"ensureActive is called by set and patch; once destroy() has been invoked (destroyed=true) the service no longer accepts new writes, because destroy flushes pending state and the instance must not be reused. Writing to a destroyed service is a programming error, so it throws instead of silently dropping data.","triggerScenarios":"Calling set(), patch(), remove(), or indirectly remove() after ViewStateService.destroy() has returned (or while a second destroy-triggered flush is in flight and the caller keeps writing through a stale reference).","commonSituations":"Editor/tab close handlers destroy the service but async callbacks (debounced saves, websocket push handlers) still hold the old reference and try to write; component re-mount reuses a cached destroyed instance instead of creating a new one.","solutions":["Stop writing after destroy: guard call sites so no set/patch happens post-destroy","Create a new ViewStateService instance if the surface is reused after being closed","Check a lifecycle flag before calling set/patch, or route writes through a wrapper that recreates the service on demand","Catch the error in fire-and-forget writers and discard the write"],"exampleFix":"// before\ntab.onDestroy = () => service.destroy();\ntab.onPush = (data) => service.set(\"state\", data); // may run after destroy\n// after\nlet destroyed = false;\ntab.onDestroy = () => { destroyed = true; return service.destroy(); };\ntab.onPush = (data) => { if (!destroyed) service.set(\"state\", data); };","handlingStrategy":"try-catch","validationCode":"let service: ViewStateService | null = svc;\nif (!service || isDestroyed) { service = createService(identity); }","typeGuard":"const isServiceActive = (s: ViewStateService) => !s.destroyed; // expose or track via wrapper","tryCatchPattern":"try {\n  service.set(\"field\", value);\n} catch (e) {\n  if (e.message === \"View state service has been destroyed\") {\n    service = new ViewStateService(identity);\n    service.set(\"field\", value);\n  }\n}","preventionTips":["Null out service references in destroy/close handlers","Route all writes through a lifecycle-aware wrapper","Cancel debounced timers and async callbacks before destroy","Re-create the service instead of caching instances across remounts"],"tags":["lifecycle","invalid-state","frontend"],"backgroundTag":"invalid-state-transition","analyzedSha":"9f775e8a12daef8255556097396f9b2739078892","analyzedAt":"2026-09-19T03:17:15.984Z","contentChangedAt":"2026-09-19T03:17:15.984Z","schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}