Skip to content

diagram: undo after creating a flow crashes the editor (ErrorBoundary "expected non-undefined object") #817

Closed

Description

@bpowers

Problem

Creating a flow attached to a stock and then pressing Undo crashes the entire diagram editor. The Editor drops to the ErrorBoundary "Something went wrong / expected non-undefined object" fallback. Unlike #812 this is recoverable by a page reload (the bad state is transient, not persisted), which is the key signal that the root cause is in the undo state-transition path, not in stored model data.

Repro (live, http://localhost:3000/<user>/<model> with a stock present)

  1. Select the Flow tool.
  2. Press on empty canvas, drag the sink onto a stock (it highlights green), release -> a flow is created attached to the stock.
  3. Press Escape (dismiss the inline name editor).
  4. Click Undo (or trigger the undo path generally).

=> Editor crashes with the ErrorBoundary fallback "Something went wrong / expected non-undefined object".

Exact stack (live capture)

Error: expected non-undefined object
  at defined (common.ts)
  at getElementByUid (drawing/Canvas.tsx:617)   // getElementByUid = defined(tryGetElementByUid(uid)); throws on a missing uid
  at flow (drawing/Canvas.tsx:2237 or 2246)      // const source = getElementByUid(sourceId) / sink = getElementByUid(sinkId)
  at buildLayers (drawing/Canvas.tsx:2271)
  at Canvas (render)

(The error boundary then re-renders; the "Copy details" button + stack added in commit 2f00934 worked as intended.)

Severity

Crash -- takes down the whole editor view; recoverable only by reload. Pre-existing. NOT caused by the flow-attach geometry fix in commit 71752f3 (that fix only changes a newly created flow's sink-point coordinates; it does not touch undo, the controller, preserveLiveView, or the flow() renderer).

Root-cause direction (needs proper investigation, not just a defensive guard)

  • The flow() renderer (Canvas.tsx:2222-2269) resolves a flow's source/sink via the throwing getElementByUid(point.attachedToUid). It guards against a falsy attachedToUid (returns early) but NOT against an attachedToUid that points to an element absent from the current elements map. So if displayElements ever contains a flow whose endpoint references a now-missing element, render throws and the whole Canvas/Editor crashes.
  • This happens during the undo path. ProjectController.undoRedo (project-controller.ts:837-859) does a synchronous batch that bumps projectVersion (+0.01) and projectGeneration and calls notify() -- but it does NOT update this.project synchronously; the real project rebuild from the restored engine snapshot happens asynchronously in reopenForUndoRedo via setTimeout -> openEngineProject (project-controller.ts:861, 455). So between the synchronous notify and the async rebuild there is a render where projectVersion changed (invalidating Canvas's version-keyed render caches) while the view elements may be in a transient/inconsistent state.
  • A likely culprit is an in-creation flow with a sentinel endpoint uid (inCreationCloudUid = -4 / fauxCloudTargetUid = -5, declared in Canvas.tsx and flow-attach.ts) or a stale optimistic element lingering in displayElements when the version bump forces a re-derive; flow() then calls getElementByUid(sentinel-or-missing) and throws.
  • Contrast: the name-editor render path was already hardened in commit 545e022 to use the non-throwing tryGetElementByUid and skip rather than crash. The flow() renderer (and the other ~10 getElementByUid call sites in Canvas.tsx: lines 683, 826, 1236, 1303, 1774, 1919, 1940, 2015, 2057, 2167, 2168, 2237, 2246) were NOT given the same treatment, so they remain crash-prone on any dangling reference.

Why it matters

A common authoring action (create a flow, then undo) crashes the editor. The crash is sticky enough to lose unsaved work in the current session (only a reload recovers), and it points at a latent correctness bug in the undo state machine: the controller emits a render with a version bump before this.project is consistent.

Components affected

  • src/diagram/drawing/Canvas.tsx (flow() renderer ~2222-2269; getElementByUid ~617; the ~10 other unguarded call sites listed above)
  • src/diagram/project-controller.ts (undoRedo ~837-859; reopenForUndoRedo / openEngineProject ~455, ~861 -- the async rebuild + synchronous version bump)
  • src/diagram/drawing/flow-attach.ts (inCreationCloudUid / fauxCloudTargetUid sentinels)
  • src/core/common.ts (defined() throws)

Possible fixes to evaluate (both may be warranted)

  1. Root cause: ensure undoRedo does not emit a render with a stale/transient view -- e.g. don't bump projectVersion synchronously before this.project is consistent, or clear any in-creation/optimistic flow before/at undo. Make the synchronous notify and the async project swap consistent.
  2. Defense in depth: make flow() (and the other element renderers) tolerate a missing endpoint by returning undefined / skipping (mirroring the existing tryGetElementByUid name-editor pattern), so a dangling reference degrades gracefully instead of taking down the whole editor. This half overlaps with Diagram editor: dangling link/flow UID reference makes a model permanently uneditable #812, which already tracks hardening the unguarded link/flow getElementByUid resolution. The novel part of this issue is fix (1): the undo-path transient-state root cause, which Diagram editor: dangling link/flow UID reference makes a model permanently uneditable #812 does not cover (Diagram editor: dangling link/flow UID reference makes a model permanently uneditable #812 is about a persisted dangling UID making a model permanently uneditable).

Note for whoever picks this up

Characterize whether this reproduces on a clean freshly-created model (general undo bug) vs only on a model with pre-existing flows (data-specific). This was observed on a model that already had several previously-created flows.

Discovery

Found while testing flow creation in the diagram editor. Related to #812 (shared renderer-hardening defense-in-depth fix); distinct in root cause (transient undo-path state vs. persisted dangling data).

Activity

  1. bpowers commented on Jun 29, 2026

    @bpowers
    OwnerAuthor

    Fixed in a2ee852 (on main). Two parts: (1) the render-time hardening shared with #812 (flow()/connector() skip dangling refs instead of throwing); (2) the root cause -- ProjectController.undoRedo no longer bumps projectVersion/projectGeneration or notifies synchronously while this.project is still pre-undo content. The bump+notify now happen inside the async reopen, in the same batch as the content swap, so the Canvas never caches its uid map from a stale view. Only the undo cursor moves synchronously.

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

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions