Skip to content

fix: recompute tracked owned deriveds after owner teardown#18513

Open
momomuchu wants to merge 1 commit into
sveltejs:mainfrom
momomuchu:fix/svelte-18489-derived-owner-destroyed
Open

fix: recompute tracked owned deriveds after owner teardown#18513
momomuchu wants to merge 1 commit into
sveltejs:mainfrom
momomuchu:fix/svelte-18489-derived-owner-destroyed

Conversation

@momomuchu

Copy link
Copy Markdown

Summary

This fixes a stale read from a class field $derived after the effect that originally owned it is destroyed.

In the failing case, a class instance is created inside a render effect. Its derived field is read while that effect is alive, then the effect is destroyed. After the source state changes from B to C, reading the derived should return CC, but it returned the cached BB.

Root cause

execute_derived treated every initialized derived with a destroyed or inert parent as inert. That returned the cached value and emitted derived_inert, even when the derived had already been tracked by the reactive graph.

Fix

The destroyed owner branch now checks the derived read version.

When rv === 0, the derived was never tracked by a reaction, so the existing inert warning and cached return behavior is preserved.

When rv !== 0, the derived was genuinely tracked before. It recomputes with no active parent, so the read is fresh without reconnecting to the destroyed effect.

Tests

Focused signals regression test passed in legacy mode and runes mode.

Signals suite passed with 108 passed and 0 failed.

Store plus runtime production suite passed with 47 passed and 0 failed.

Server side rendering suite passed with 32 passed and 0 failed.

Runtime runes plus runtime legacy suite passed with 5935 passed and 0 failed.

Package typecheck passed. ESLint passed for the touched runtime source. A changeset is included.

Closes #18489

@pkg-svelte-dev

pkg-svelte-dev Bot commented Jul 7, 2026

Copy link
Copy Markdown

Install the latest version of svelte from 60bed83:

pnpm add https://pkg.svelte.dev/svelte/c/60bed83391b41f6d7fd52aa4d21c1b4de7003630

Open in pkg.svelte.dev: https://pkg.svelte.dev/repos/svelte/pr/18513

Note

This PR is from a fork. A maintainer must approve approve each commit before it can be built and installed.

@changeset-bot

changeset-bot Bot commented Jul 7, 2026

Copy link
Copy Markdown

🦋 Changeset detected

Latest commit: 60bed83

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 1 package
Name Type
svelte Patch

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

$derived on a class instance returns a stale value after the effect that incidentally owned it at construction is destroyed

1 participant