Skip to content

fix: ensure unowned deriveds can add themselves as reactions while connected #16249

New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Merged
merged 2 commits into from
Jun 27, 2025

Conversation

dummdidumm
Copy link
Member

Fixes #15829 and potentially #15853

I can't quite articulate yet why this is right, but I think it is. Basically, an unowned derived should still add itself as a reaction while it is properly connected, which means it can be cleaned up correctly - and this connection is indicated by it having reactions.

Before submitting the PR, please make sure you do the following

  • It's really useful if your PR references an issue where it is discussed ahead of time. In many cases, features are absent for a reason. For large changes, please create an RFC: https://github.com/sveltejs/rfcs
  • Prefix your PR title with feat:, fix:, chore:, or docs:.
  • This message body should clearly illustrate what problems it solves.
  • Ideally, include a test that fails without this PR but passes with it.
  • If this PR changes code within packages/svelte/src, add a changeset (npx changeset).

Tests and linting

  • Run the tests with pnpm test and lint the project with pnpm lint

Copy link

changeset-bot bot commented Jun 26, 2025

🦋 Changeset detected

Latest commit: 065690b

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

@svelte-docs-bot
Copy link

Copy link
Contributor

Playground

pnpm add https://pkg.pr.new/svelte@16249

@@ -112,6 +112,45 @@ describe('signals', () => {
};
});

test('unowned deriveds are not added as reactions but trigger effects', () => {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is the name of the test correct? Isn't the change that we actually add the derived as a reaction? I think it makes sense: the problem is probably that we check if the derived is UNOWNED before executing it so if the execution changes that (connecting it) when we reach the point of the code you changes skip_reaction should be false (because now the derived it's not unowned anymore) but it's still true and you can check that by looking if it has reactions (or i suppose we could also check if (reaction.f & UNOWNED) !== 0 in line?)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Btw scratch the last sentence, I confused it and also the flag is never set to false

Copy link
Member

@paoloricciuti paoloricciuti left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I tried to spend the whole day to figure out if there was a different way we could get the hint that "this unowned derived can be cleared so add it anyway" and i couldn't...but I think the important bit is that it works and doesn't leak reactions...so i would say let's go with it?

Copy link
Member

@paoloricciuti paoloricciuti left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I tried to spend the whole day to figure out if there was a different way we could get the hint that "this unowned derived can be cleared so add it anyway" and i couldn't...but I think the important bit is that it works and doesn't leak reactions...so i would say let's go with it?

Copy link
Member

@paoloricciuti paoloricciuti left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I tried to spend the whole day to figure out if there was a different way we could get the hint that "this unowned derived can be cleared so add it anyway" and i couldn't...but I think the important bit is that it works and doesn't leak reactions...so i would say let's go with it?

@dummdidumm dummdidumm merged commit 7019894 into main Jun 27, 2025
14 checks passed
@dummdidumm dummdidumm deleted the derived-unowned-fix branch June 27, 2025 23:37
@github-actions github-actions bot mentioned this pull request Jun 27, 2025
# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

derived value is not updating correctly
2 participants