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)
pnpm 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)
pnpm 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:
TypeError: 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
System: 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.34Any additional comments?
This is a continuation of the regression chain that started in 3.5.31:
- PR #8619 (3.5.31) introduced
pendingMountsso teleports defer their mount whenparentSuspense.pendingBranchis set. This is what created the situation where a teleport's children can havevnode.component === null. - Issue #14640 + (3.5.32) added the check shown above. That gates DOM removal of children, not the call on children.
Assistant
Let me start by understanding the issue. The problem is about Vue.js Teleport + Suspense causing an unmount crash when a pending teleport has a component child. The error is
TypeError: Cannot read properties of null (reading 'type')which occurs inunmountComponent.Let me first explore the repository structure to understand what we're working with.
Assistant
Let me look at the directory structure more specifically for the Vue core runtime.
Assistant
This is the Vue.js core repository. The issue is about Teleport + Suspense. Let me look at the relevant files in
runtime-core.Assistant
Let me look at the Teleport.ts file since that's where the issue seems to be, based on the stack trace mentioning
TeleportImpl.remove.Assistant
Now I can see the issue. Let me look at the
removefunction more closely. The issue is in theTeleportImpl.removemethod around line 238-262:remove({ shapeFlag, el, props, targetStart, targetAnchor, anchor, children, target, vnode, }: TeleportVNode, ... ) { ... // an unmounted teleport should always unmount its children whether it's disabled or not doRemove && hostRemove(anchor!) if (shapeFlag & ShapeFlags.ARRAY_CHILDREN) { for (let i = 0; i < (children as VNode[]).length; i++) { const child = (children as VNode[])[i] unmount( child, parentComponent, parentSuspense, shouldRemove, !!child.dynamicChildren, ) } } },When there's a pending mount, the code sets
shouldRemove = false, but it still callsunmount()on each child. The problem is that when the child is a component vnode that hasn't been mounted yet (because of the pending mount),child.componentisnull. Theunmountfunction then tries to callunmountComponent(null, ...)which tries to readnull.typeand crashes.Let me check the
unmountfunction in renderer.ts:Assistant
Now I see the issue. In the
unmountfunction at line 2173, whenshapeFlag & ShapeFlags.COMPONENTis true, it calls:unmountComponent(vnode.component!, parentSuspense, doRemove)The
!asserts thatvnode.componentis not null, but when a Teleport with a component child is pending mount (hasn't been mounted yet),vnode.componentis actuallynull.Let me look at the
unmountComponentfunction:Assistant
Now let me look at the
TeleportImpl.removefunction more closely to understand the fix needed:Assistant
Now I understand the issue. When a Teleport has a pending mount and is unmounted:
- The
removefunction checks forpendingMountand setsshouldRemove = false - However, it still iterates over the children and calls
unmount()on each child - If a child is a component vnode that was never mounted (since the teleport was pending),
child.componentisnull - The
unmountfunction seesshapeFlag & ShapeFlags.COMPONENTis true and callsunmountComponent(vnode.component!, ...)withnull unmountComponenttries to accessinstance.type.__hmrIdand crashes
The fix should skip unmounting children that haven't been mounted yet when there's a pending mount. Let me check if the issue is that when
pendingMountexists, we should skip unmounting children entirely.Let me look at the test file mentioned in the issue to understand the reproduction better:
- The
Assistant
Let me look at the existing Teleport tests for Suspense-related scenarios:
Assistant
Let me also check Suspense.spec.ts for any Teleport tests:
Assistant
Now, let me create a test script to reproduce the issue first. Let me first understand the test setup used in this repo:
Assistant
I need to understand how to run the tests and create a reproduction script. Let me check the package.json and look at the test infrastructure.
Assistant
Let me create a reproduction script to confirm the issue before applying the fix.
Assistant
The test command timed out. Let me try a different approach - I'll create a minimal test script that reproduces the issue.
Assistant
Good, so
@vue/runtime-testexports everything from@vue/runtime-coreplus some test utilities. Let me look at the existing tests more to understand the pattern, and then write a minimal reproduction test that I can add to the test file temporarily to confirm the bug.Let me look at how the existing Suspense + Teleport tests are structured:
Assistant
Let me look at the rest of this test and see how the test framework works to understand how to write a reproduction test.
Assistant
Now let me create a minimal reproduction script to test the bug. Let me first understand the
defineAsyncComponentpattern used in tests:Assistant
Now let me write a minimal reproduction test. The issue is about unmounting a Suspense that contains a Teleport with a component child before the Teleport has been mounted. Let me create a test script:
Assistant
Error! I can only call one function at a time. That was my first call. Now I need to wait for the result and issue my next call.
Now let me run this test to reproduce the issue:
Assistant
Let me check how the runtime-test exports Suspense and what features are available:
Assistant
The issue is that Suspense may be feature-gated. Let me check how the tests import it:
Assistant
The test uses vitest and
@vitest-environment jsdom. The imports from@vue/runtime-testincludeSuspense. The problem might be that I need to build the packages first, or there's a specific configuration. Let me check the vitest config:Assistant
I see -
__FEATURE_SUSPENSE__is defined astruein vitest config. Let me try running the existing tests using vitest directly:Assistant
The test exists but I searched with the wrong name. Let me find the right test name:
Assistant
I see there are already some tests for teleport+suspense edge cases. Let me look at test
should not crash when moving disabled teleport with component children inside suspense:Assistant
Let me look at the issue more carefully. The error states that
unmountComponentis called withnullfor the instance. Looking at theTeleportImpl.removefunction, when there's a pending mount, the code setsshouldRemove = falsebut still callsunmounton children. The children may havevnode.component === nullif the teleport was never mounted.Let me now understand the exact scenario more clearly by looking at the current code flow:
Assistant
I see the issue now. The comment says "skip removing children because nothing has been mounted yet" but the code still iterates over children and calls
unmount()on them. TheshouldRemove = falseonly affects DOM removal, not the actual unmount logic.When a child is a component vnode that hasn't been mounted yet,
child.component === null. Theunmountfunction callsunmountComponent(vnode.component!, ...)which crashes becausevnode.componentisnull.The fix should be: when there's a pending mount, we should skip unmounting children entirely, since they were never mounted. Let me also check the
unmountfunction to see if it handles the case wherevnode.component === null:Assistant
I can see the issue clearly now. At line 2173, when
shapeFlag & ShapeFlags.COMPONENTis true, it callsunmountComponent(vnode.component!, ...)with the non-null assertion operator!. However, when a Teleport with a component child is pending mount,vnode.componentisnull, causing the crash.The fix should be in the
TeleportImpl.removefunction: when there's a pending mount, we should skip unmounting children entirely since they were never mounted. Let me implement this fix:Assistant
I need to call a function one at a time.
Assistant
Now let me apply the fix. The issue is in the
removemethod ofTeleportImpl. When there's a pending mount, the children have never been mounted, so we should skip unmounting them entirely. Let me look at the current code again:Assistant
I can see the Teleport.ts file content. Now I need to find the exact line numbers for the
removemethod and make the fix. The fix should be: when there's a pending mount, skip unmounting children entirely since they were never mounted.Let me find the exact line numbers for the remove method.
Assistant
Now let me see the full remove method and understand the exact fix needed.
Assistant
Now I understand the issue clearly. The comment on line 336-337 says "skip removing children because nothing has been mounted yet", but the code on lines 351-361 still unmounts the children. The fix is to skip the children unmount loop when there's a pending mount, since the children were never mounted.
The fix should add a check before unmounting children: if there was a pending mount, skip the unmount of children entirely. Let me apply this fix.
Assistant
Let me verify the change and then add the actual logic to skip unmounting children when there was a pending mount:
Assistant
Now I need to modify the condition to check if there was a pending mount and skip unmounting children in that case. The
pendingMountvariable is declared earlier and is truthy when there was a pending mount. I need to use this to conditionally skip the children unmount loop.Assistant
I need to use a different approach since python3 is not available. Let me use sed or another tool to make the edit.
Assistant
I'll use the
edittool to replace the specific condition that needs to be changed.Assistant
Wait, that might have replaced multiple occurrences. Let me check:
Assistant
Uh oh, the sed command replaced more than one occurrence. Line 400 was also changed, which is not what we want. Let me revert and apply a more targeted fix:
Assistant
Now let me use the
editcommand properly to make the targeted change. I need to find the exact line number and replace only that one.Assistant
Good, I reverted the file. Now I need to use the edit command to replace only line 352. Let me use a more precise approach with the
editcommand by targeting the exact line range.Assistant
Now I'll use the edit command to replace only the specific line 352 in the
removemethod:Assistant
I need to replace line 352 which is
if (shapeFlag & ShapeFlags.ARRAY_CHILDREN) {in theremovemethod withif (shapeFlag & ShapeFlags.ARRAY_CHILDREN && !pendingMount) {. I'll use a node script to make this change more precisely:Assistant
The
!in the string is being interpreted by bash. Let me escape it properly or use a different approach:Assistant
Let me check the exact structure of the remove method to fix my detection logic:
Assistant
The
remove(is on one line andvnode: VNodeis on the next. Let me adjust the detection:Assistant
The fix has been applied. Let me verify the change:
Assistant
The fix is applied. Now let me also add a comment to explain why we're checking for
!pendingMount:Assistant
Let me verify the final change:
Assistant
Now let me run the existing tests to make sure the fix doesn't break anything:
Assistant
Command timed out. Only got partial output. Let me try running just the Suspense tests related to teleport to validate the fix.
Assistant
Excellent! All 5 teleport-related tests in Suspense pass. Now let me write a specific test case that reproduces the bug scenario described in the issue - unmounting a Suspense with a pending teleport that has a component child:
Assistant
The existing tests pass, but they don't cover the specific bug scenario (unmounting a Suspense with a pending teleport that has a component child). Let me create a test script to verify my fix handles this specific case:
Assistant
The test ran but failed because of the assertion for the empty result - the test infrastructure leaves a comment node
<!---->when rendering null. But the important thing is that it didn't crash with theTypeError: Cannot read properties of null (reading 'type')error! The fix is working.Let me update the test to have the correct expected value:
Assistant
The test passes. Now let me verify that the fix also works with the element child case (which was already handled by the previous fix) and also make sure I didn't break any other tests:
Assistant
All 175 tests pass. Now let me run a broader set of tests to make sure nothing else broke:
Assistant
All 1014 tests pass! Let me also clean up the temporary test files I created:
Assistant
Let me verify my fix one more time by looking at the final state of the changed file:
Assistant
The fix looks correct. Let me summarize what I've done and verify the checklist:
Summary of the Fix
Problem: When a
<Teleport>inside a<Suspense>is unmounted before its mount can run (i.e., it has a pending mount), Vue crashes withTypeError: Cannot read properties of null (reading 'type').Root Cause: In
TeleportImpl.remove, when there's a pending mount, the code correctly clears the pending mount job and setsshouldRemove = falseto skip DOM removal. However, it still iterates over children and callsunmount()on each child. If a child is a component vnode that was never mounted (because the teleport's mount was deferred),child.component === null. Theunmount()function then callsunmountComponent(vnode.component!, ...)withnull, causing the crash.Fix: Added a check
&& !pendingMountto the condition that unmounts children. When there's a pending mount, the children were never mounted in the first place, so there's no need to unmount them.Let me run the tests one more time to confirm everything is working:
Assistant
Let me verify the current state of my changes:
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.