sveltejs / sveltejs/kit

memory leak in adapter node with unresolved promises

Open
#10,714 4 comments 6 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

pkg:adapter-node
Dominant language
JavaScript
Stars
20.8k
Forks
2.3k
Avg merge
1d 16h
Merged PRs (30d)
156

Description

Describe the bug

Rendering a page that depends on a never-solving promise leaks memory.

Svelte seems to keep handlers around for the resolution of promises even after the page has been fully rendered and served to the client, preventing the js engine from freeing the associated memory.

Reproduction

https://github.com/lovasoa/sveltekit-memory-leak-bugreport

+page.svelte

<script>
    import c from './const';
</script>

{#await c.waitForMe()}
    <p>loading...</p>
{:then x}
    <p>loaded {x}!</p>
{:catch error}
    <p style="color: red">{error.message}</p>
{/await}
const.js
class MyClass {
    constructor() {
        this.x = neverSolve();
    }
    async waitForMe() {
        const x = await this.x;
        return x+1;
    }
}

function neverSolve() {
    return new Promise(()=>{});
}

export default new MyClass;

mem-graph

Logs

No response

System Info
System:
    OS: Linux 6.2 Ubuntu 22.04.3 LTS 22.04.3 LTS (Jammy Jellyfish)
    CPU: (8) x64 11th Gen Intel(R) Core(TM) i7-11390H @ 3.40GHz
    Memory: 1.03 GB / 15.36 GB
    Container: Yes
    Shell: 5.1.16 - /bin/bash
  Binaries:
    Node: 16.20.2 - /usr/bin/node
    Yarn: 1.22.19 - /usr/bin/yarn
    npm: 8.19.4 - /usr/bin/npm
    pnpm: 8.6.3 - /usr/bin/pnpm
  Browsers:
    Chrome: 116.0.5845.187
  npmPackages:
    svelte: ^4.0.5 => 4.2.0
Severity

annoyance

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the linked sveltekit-memory-leak-bugreport reproduction, especially +page.svelte and const.js, and run it with the reported Node setup. Trace the adapter's server-side rendering of the unresolved promise and verify that rendered pages no longer retain promise-resolution handlers or the associated memory.

Written by the indexing model from the issue text.

Assessment

Tech stack
javascript, node.js
Domain
backend, web-dev
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.