Repository navigation
Description
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)
- Select the Flow tool.
- Press on empty canvas, drag the sink onto a stock (it highlights green), release -> a flow is created attached to the stock.
- Press Escape (dismiss the inline name editor).
- 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 throwinggetElementByUid(point.attachedToUid). It guards against a falsyattachedToUid(returns early) but NOT against anattachedToUidthat points to an element absent from the current elements map. So ifdisplayElementsever 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 bumpsprojectVersion(+0.01) andprojectGenerationand callsnotify()-- but it does NOT updatethis.projectsynchronously; the real project rebuild from the restored engine snapshot happens asynchronously inreopenForUndoRedoviasetTimeout->openEngineProject(project-controller.ts:861, 455). So between the synchronousnotifyand the async rebuild there is a render whereprojectVersionchanged (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 inCanvas.tsxandflow-attach.ts) or a stale optimistic element lingering indisplayElementswhen the version bump forces a re-derive;flow()then callsgetElementByUid(sentinel-or-missing)and throws. - Contrast: the name-editor render path was already hardened in commit 545e022 to use the non-throwing
tryGetElementByUidand skip rather than crash. Theflow()renderer (and the other ~10getElementByUidcall sites inCanvas.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/fauxCloudTargetUidsentinels)src/core/common.ts(defined()throws)
Possible fixes to evaluate (both may be warranted)
- Root cause: ensure
undoRedodoes not emit a render with a stale/transient view -- e.g. don't bumpprojectVersionsynchronously beforethis.projectis consistent, or clear any in-creation/optimistic flow before/at undo. Make the synchronousnotifyand the async project swap consistent. - Defense in depth: make
flow()(and the other element renderers) tolerate a missing endpoint by returningundefined/ skipping (mirroring the existingtryGetElementByUidname-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/flowgetElementByUidresolution. 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
diagram: harden the editor against dangling refs, undo races, and NaN…
bpowers commentedon Jun 29, 2026
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.