Skip to content

Child $effect of $derived is not destroyed in conditional expression when not called #13683

@bdpartridge

Description

@bdpartridge

Describe the bug

When a child $effect is conditionally called by a $derived, the effect cleanup is deferred until right before the effect is called again or the component is destroyed. See the linked reproduction for a simple example. Notice how the effect cleanup isn't run until right before the getValue function is called again; try toggling the checkbox.

Since the getValue function effectively becomes unused by the component when the condition becomes false, it seems like the effect should be destroyed. If the effect were being used to track a subscription, for example, this could be problematic because there'd be no immediate signal when the component had lost interest in the results of getValue.

I'm not sure if this is a bug or expected behavior, so apologies for the noise if this is working as expected.

Reproduction

https://svelte-5-preview.vercel.app/#H4sIAAAAAAAAE22RTW7CMBCFrzKasggSSvYhUPUMrbqpKxHiMZgaO4ongSrK3Ss7gSCVlT32--bv9ai0IY_5V4-2PBPm-FbXuEL-rUPgOzJMuELv2qYKL4WvGl3zVljB-ly7hqE_EH-WpqUBVOPOIDDNuhCnI56evMC1sAExxOCP7hL1sIGF55Ip4aal5fom6G6fkhrdkUxm4hVuxRKByjmBS8ihtZKUtiRDjiKbW7SF1F3stTDlnky8Ci60rVuGMORGYHWk6mfvrgJhr63MY0xy09_LDpBN5PvRXcb-YtLslrXIpkK2f9FqHnGIsnqiP440Dac99PE2pGOiIOkzrQZhcYVnJ7XSJDEPmxlWd3ce9zrbdPKPFtE12qJaW7F2dt5YhJfQh4oLUooqTpIlbLbjk-DKWe8MpcYdkt0oyGExNbobDRLcELeNhZF8gkBlqLRt_Q8dxmPi49da2KcTfw9_25uWMpsCAAA=

Logs

No response

System Info

Svelte 5.0.0-next.269

Severity

annoyance

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions