Svelte store subscriptions leaking after component unmount when subscribing manually in onMount
0 reputation · 03 Oct 2026, 09:15 UTC
Goal
I want to share state between several Svelte components using a writable store defined in a separate .js module, while guaranteeing that no subscriptions survive after a component is destroyed.
Constraints and uncertainty
The documented behavior is that the $store auto-subscription syntax inside .svelte files unsubscribes automatically on component destruction. However, some of my logic lives outside markup — for example, subscribing inside onMount or in a plain utility module — where the compiler cannot insert the cleanup for me. The documentation indicates manual subscribe() calls return an unsubscribe function that I must invoke myself, but it is unclear to me exactly which lifecycle hook is the correct place for that cleanup, and whether missing it produces a detectable failure (growing memory usage, duplicate handler invocations) or fails silently.
Assume Svelte 4/5-era store contracts (writable, derived from svelte/store); if runes-based state changes this answer, that distinction matters.
Questions
- When subscribing manually inside
onMount, is returning the unsubscribe function fromonMount's cleanup the documented and sufficient pattern? - Is there a supported way to verify at runtime that all subscriptions were released after unmount, short of heap profiling?
- Does a leaked subscription cause any observable warning in development mode, or is it always silent?