ISSUE DESCRIPTION Teleport + Suspense: unmount crashes on pending teleport with…
- User
ISSUE DESCRIPTION
Teleport + Suspense: unmount crashes on pending teleport with component child (regression since 3.5.31)
Vue version
3.5.34
Link to minimal reproduction
https://github.com/pont1s/vue-teleport-unmount-crashes
Steps to reproduce
Option 1 — Browser (primary)
bashpnpm dev- Open the browser DevTools console.
- Click "Unmount Suspense".
- Observe
TypeError: Cannot read properties of null (reading 'type')in the console
Option 2 — Tests (additional)
bashpnpm testThe repo also contains three Vitest tests in
tests/teleport-unmount.test.jsWhat is expected?
When a inside a is unmounted before its mount can run, Vue should remove it cleanly. This worked correctly in 3.5.30.
What is actually happening?
In 3.5.31..3.5.34, unmounting such a teleport throws:
codeTypeError: Cannot read properties of null (reading 'type') at unmountComponent (runtime-core.cjs.js) at unmount at Object.remove (TeleportImpl.remove) at unmount at unmountChildren at unmount at unmountComponent at unmount at Object.resolve (Suspense)System Info
shellSystem: OS: macOS 26.3.1 CPU: (8) arm64 Apple M1 Memory: 102.19 MB / 16.00 GB Shell: 5.9 - /bin/zsh Binaries: Node: 24.15.0 - /Users/user/.local/state/fnm_multishells/14407_1779493498078/bin/node npm: 11.12.1 - /Users/user/.local/state/fnm_multishells/14407_1779493498078/bin/npm pnpm: 11.1.3 - /Users/user/.local/state/fnm_multishells/14407_1779493498078/bin/pnpm Browsers: Chrome: 148.0.7778.179 Safari: 26.3.1 npmPackages: vue: 3.5.34 => 3.5.34 Assistant
Let me start by understanding the repository structure and the issue. The issue is about a Teleport component inside a Suspense that crashes when unmounted before its mount can run. The crash happens in
unmountComponentwithTypeError: Cannot read properties of null (reading 'type').Let me first explore the repository structure, then find the relevant code.
The key files mentioned:
runtime-core.cjs.js- but this is likely built output, the source is probably in TypeScript- The issue mentions
TeleportImpl.removein teleport pendingMountscheck was added inmoveTeleportbut not inTeleportImpl.remove
Let me explore the repository structure first.
Assistant
Let me look at the packages directory to find the relevant source code.
Assistant
Let me find the relevant source files. The issue mentions
TeleportImpl.removeandunmountComponent.Assistant
Let me find the Teleport component and the
unmountComponentfunctionAssistant
Let me find Teleport in the runtime-core source.
Assistant
Let me go back to the root and search
Assistant
Let me open the Teleport.ts file and find the relevant sections.
Assistant
Let me look at the file first.
Assistant
Now let me look at the
unmountfunction in renderer.ts, especially how it handlesunmountComponent.Assistant
Let me look at the
unmountfunction andunmountComponentin detailAssistant
Now I can see the issue. In the
unmountfunction:typescriptif (shapeFlag & ShapeFlags.COMPONENT) { unmountComponent(vnode.component!, parentSuspense, doRemove) }This directly accesses
vnode.component!with a non-null assertion. Ifvnode.componentisnull, this will crash. The issue is that when a teleport has a pending mount (because of suspense), its component children haven't had theirsetupfunction called yet, sovnode.componentisnull.But wait, the Teleport's
removefunction does check forpendingMountsand setsshouldRemove = false. However, it still iterates through children and callsunmount(child, ..., shouldRemove, ...). TheshouldRemoveonly controls whether the DOM element should be removed; it doesn't prevent theunmountComponentcall.Let me look at the
unmountComponentfunction to understand it better.Assistant
Now I understand the issue clearly. Let me trace through the code:
- When a Teleport is inside a Suspense with a
pendingBranch, the teleport's mount is deferred viapendingMounts. - If the teleport is unmounted before its mount job runs, the
removefunction is called. - In the
removefunction,pendingMountsis checked andshouldRemoveis set tofalse, but it still iterates over children and callsunmount(child, ...). - For component children,
unmountcallsunmountComponent(vnode.component!, ...)withvnode.componentbeingnull(since the component was never mounted). - The non-null assertion
!crashes becausenulldoesn't have a.typeproperty.
The fix: in Teleport's
remove, skip unmounting children entirely when there's a pending mount.Let me see the full
removefunction to understand the exact code- When a Teleport is inside a Suspense with a
Assistant
Let me find the
removefunctionAssistant
Let me look at the remove function from line 317.
Assistant
Now I can clearly see the bug. In the
removefunction:typescriptconst pendingMount = pendingMounts.get(vnode) if (pendingMount) { pendingMount.flags! |= SchedulerJobFlags.DISPOSED pendingMounts.delete(vnode) shouldRemove = false }The comment says "skip removing children because nothing has been mounted yet", but the code only sets
shouldRemove = false. The children loop still runs, and for component children,unmountcallsunmountComponent(vnode.component!, ...)which crashes becausevnode.componentisnull(the component was never mounted).The fix: skip unmounting children entirely when there's a pending mount. Let me edit the file
Assistant
Let me use the edit command to fix the issue
Assistant
Let me try opening the file
Assistant
Now I can see the file. I need to edit line 352 to add
!pendingMount &&guard. Let me do the edit.Assistant
Let me try using sed to make the edit
Assistant
Good. Now let me use sed to modify this line.
Assistant
Let me verify the
Assistant
The change looks correct. Now let me run the existing tests to verify.
Assistant
Let me run the tests specifically for Teleport and Suspense
Assistant
All 108 tests pass. Let me also run the tests in the vue package that might test this interaction
Assistant
The e2e tests fail because the browser can't be launched in this environment (no sandbox), but that's unrelated to our change. Let me write a reproduction test to verify the fix.
Assistant
Let me use vitest to write a quick test
Assistant
Let me look at the existing teleport tests to find the right place for our
Assistant
Let me look at the existing test for the suspense-related teleport behavior
Assistant
Let me look at the Suspense tests to find tests about teleport +
Assistant
Let me look at the "should not mount discarded teleport after suspense is resolved" test
Assistant
Let me look at the test about moving disabled teleport with component children inside suspense
Assistant
Let me run all unit tests to make sure my change doesn't
Assistant
The e2e failures are all due to browser sandbox issues (container environment). The one unit test failure (computed perf test) is likely a flaky test unrelated to our change.
Let me verify the diff:
Assistant
The diff looks correct. Let me verify the logic one more time by reviewing the complete
removeAssistant
Let me also double-check the
unmountfunction in renderer.ts to see if there's a need for an additional guardAssistant
The fix is minimal and correct. Let me also check that the
moveTeleportfunction already has a similar guard (to confirm we're following the same pattern):Assistant
The
moveTeleportfunction already has the!pendingMounts.has(vnode)guard for PR #14702. My fix follows the same pattern for theremovefunction.Let me now run the teleport and suspense tests one more time to confirm everything passes:
Assistant
All tests pass. Let me also run the full runtime-core test suite to be thorough:
Assistant
All 1014 tests in runtime-core pass. Let me run the vue package tests too (excluding e2
Assistant
All non-e2e tests pass. The e2e failures are all browser sandbox issues unrelated to our change.
Let me submit the fix now
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.