siyuan-note/siyuan · error · Error

View state service has been destroyed

Error message

View state service has been destroyed

What it means

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.

Solutions

  1. Stop writing after destroy: guard call sites so no set/patch happens post-destroy
  2. Create a new ViewStateService instance if the surface is reused after being closed
  3. Check a lifecycle flag before calling set/patch, or route writes through a wrapper that recreates the service on demand
  4. Catch the error in fire-and-forget writers and discard the write

Example fix

// before
tab.onDestroy = () => service.destroy();
tab.onPush = (data) => service.set("state", data); // may run after destroy
// after
let destroyed = false;
tab.onDestroy = () => { destroyed = true; return service.destroy(); };
tab.onPush = (data) => { if (!destroyed) service.set("state", data); };
Defensive patterns

Strategy: try-catch

Validate before calling

let service: ViewStateService | null = svc;
if (!service || isDestroyed) { service = createService(identity); }

Type guard

const isServiceActive = (s: ViewStateService) => !s.destroyed; // expose or track via wrapper

Try / catch

try {
  service.set("field", value);
} catch (e) {
  if (e.message === "View state service has been destroyed") {
    service = new ViewStateService(identity);
    service.set("field", value);
  }
}

Prevention

When it happens

Trigger: 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).

Common situations: 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.

Understand the failure class

Background: "Invalid state transition" errors: "status must be X, actually Y", "already rejected/charging/uninstalled", "cannot ... while running" — what they mean when a library rejects your call — this error's family across 31 libraries.

Related errors


AI-assisted analysis of siyuan-note/siyuan@9f775e8a12 (2026-09-19). Data as JSON: /api/errors/7bad00447dd7f1c3. Report an issue: GitHub.

Appendix: source

Thrown at app/src/util/viewState.ts:260

    private scheduleFlush() {
        this.clearFlushTimer();
        this.flushTimer = setTimeout(() => {
            this.flushTimer = undefined;
            this.flush().catch((error) => console.error(error));
        }, this.flushDelay);
    }

    private clearFlushTimer() {
        if (this.flushTimer !== undefined) {
            clearTimeout(this.flushTimer);
            this.flushTimer = undefined;
        }
    }

    private ensureActive() {
        if (this.destroyed) {
            throw new Error("View state service has been destroyed");
        }
    }
}

View on GitHub (pinned to 9f775e8a12)