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

SetInterval does not clear on HMR reload / code change #91

Open
nikhil-swamix opened this issue Dec 24, 2023 · 3 comments
Open

SetInterval does not clear on HMR reload / code change #91

nikhil-swamix opened this issue Dec 24, 2023 · 3 comments

Comments

@nikhil-swamix
Copy link

Describe the bug

this i use to observe changed value
$: {
filterSearchResults(searchTerm);
console.log(selections);
}
------ but this goes out of control and keeps printing forever
setInterval(() => {
console.log(selections);
}, 1000);

Reproduction

this i use to observe changed value
$: {
filterSearchResults(searchTerm);
console.log(selections);
}
------ but this goes out of control and keeps printing forever
setInterval(() => {
console.log(selections);
}, 1000);

Logs

No response

System Info

System:
    OS: Windows 10 10.0.19045
    CPU: (12) x64 Intel(R) Core(TM) i7-8700K CPU @ 3.70GHz
    Memory: 7.16 GB / 15.94 GB
  Binaries:
    Node: 20.6.1 - C:\Program Files\nodejs\node.EXE       
    Yarn: 1.22.19 - ~\AppData\Roaming\npm\yarn.CMD        
    npm: 9.8.1 - C:\Program Files\nodejs\npm.CMD
    pnpm: 8.7.6 - ~\AppData\Roaming\npm\pnpm.CMD
  Browsers:
    Edge: Chromium (120.0.2210.91)
  npmPackages:
    @sveltejs/adapter-auto: ^3.0.1 => 3.0.1 
    @sveltejs/adapter-node: ^2.0.1 => 2.0.1
    @sveltejs/kit: ^2.0.4 => 2.0.4
    @sveltejs/vite-plugin-svelte: ^3.0.0 => 3.0.1
    svelte: ^4.2.8 => 4.2.8
    vite: ^5.0.10 => 5.0.10

Severity

serious, but I can work around it

Additional Information

image

@Conduitry Conduitry transferred this issue from sveltejs/kit Feb 27, 2024
@Conduitry
Copy link
Member

Transferring this to the HMR repository. However, as mentioned on the similar issue sveltejs/sites#146 I'm not really sure what would be a reasonable way to resolve this.

@rixo
Copy link
Collaborator

rixo commented Jul 8, 2024

HMR just destroys and recreates your component. If you have a leaking setInterval that keeps running, you would have the same problem in production. HMR just makes it more obvious, because components might get recreated less often, or never destroyed at all in production.

As a general rule that suffers few exceptions, you should make sure that your components always clean after themselves when they get destroyed, and don't leave things like setInterval running.

The general pattern is pretty basic, just implement such resources holders in onMount and use the cleanup callback (return value) to release them:

onMount(() => {
  const interval = setInterval(() => { ... })

  return () => {
    clearInterval(interval)
  }
}

Note that having setInterval calls directly at the root of your components instead of an onMount callback, like your example seems to suggest, would not play nice with SSR either. It would probably end up eating all your server's memory over time.

@nikhil-swamix
Copy link
Author

HMR just destroys and recreates your component. If you have a leaking setInterval that keeps running, you would have the same problem in production. HMR just makes it more obvious, because components might get recreated less often, or never destroyed at all in production.

As a general rule that suffers few exceptions, you should make sure that your components always clean after themselves when they get destroyed, and don't leave things like setInterval running.

The general pattern is pretty basic, just implement such resources holders in onMount and use the cleanup callback (return value) to release them:

onMount(() => {
  const interval = setInterval(() => { ... })

  return () => {
    clearInterval(interval)
  }
}

Note that having setInterval calls directly at the root of your components instead of an onMount callback, like your example seems to suggest, would not play nice with SSR either. It would probably end up eating all your server's memory over time.


i see, i was able to arrive at the same conclusion, and later used the clear interval in onDestroy....
i have a proposal let me know if its feasible.

  1. creating a scoped set interval like the other variables in component, by modding the original setInterval,
  2. for example window.setInterval is different than set interval
  3. this way by some "internal modding" it will be auto managed for set and clear intervals, and maybe some other commonly used functions which subject to leaks.
  4. this will result in improved dev exp, and avoid pitfalls, and fallback to window global methods if anyone really wants it.

@rixo @Rich-Harris , this is just primitive idea, will it consider a bad architecture/practice? let me know.

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

No branches or pull requests

3 participants