Skip to content
New issue

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

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

Already on GitHub? Sign in to your account

fix: store access on component destroy #14968

Merged
merged 1 commit into from
Jan 10, 2025
Merged

Conversation

paoloricciuti
Copy link
Member

Closes #14950

While working on this initially with @oscard0m i actually found out of a separate bug, a memory leak: basically if you did something like this

<script>
    let { store } = $props();

    $effect(()=>{
        return ()=>{
            $store;
        }
    });
</script>

the store was accessed for the first time after the teardown of the stores executed. So a new subscription was created for the store that generally would've been cleaned up by the teardown but since it already ran it was stuck in memory.

This was even worse for stuff that was accessed on component destroy asynchronously.

This solution does two things:

  1. the setup_stores function doesn't register the teardown immediately but returns a function that does that. This function is called after pop so that register even after every user effect.
  2. now that we know that the teardown will be executed last, on teardown uses the stores variable as a way of communication...it register a symbol on it so that inside get_store we can check if the component was already unmounted and, in that case, it prevents the creation of a subscription in the store while at the same time accessing the store with get from svelte/store, less efficient but more consistent (the bug we are closing was about the consistency of the read) and not leaky. Furthermore this will only really happen when someone is accessing stuff after the component unmounts so should not be a problem.

I didn't found a way to test the memory leak but if you have ideas it could be good to add.

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 Jan 9, 2025

🦋 Changeset detected

Latest commit: 696b50f

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

@Rich-Harris
Copy link
Member

preview: https://svelte-dev-git-preview-svelte-14968-svelte.vercel.app/

this is an automated message

Copy link
Contributor

github-actions bot commented Jan 9, 2025

Playground

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

Copy link
Member

@dummdidumm dummdidumm left a comment

Choose a reason for hiding this comment

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

👍

@paoloricciuti paoloricciuti merged commit 41fb513 into main Jan 10, 2025
11 checks passed
@paoloricciuti paoloricciuti deleted the fix-store-access-on-destroy branch January 10, 2025 10:28
@github-actions github-actions bot mentioned this pull request Jan 10, 2025
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.

onDestroy doesnt update global stores value (at the same onDestroy function).
3 participants