Skip to content

vm.SourceTextModule memory leak #59118

Description

@legendecas

The OOM can be reproduced even without link and evaluate:

// Flags: --max-heap-size=16 --experimental-vm-modules
import {SourceTextModule, createContext} from 'vm';

globalThis.iter = 0;
setInterval(() => {
   // Do not capture any variables.
   globalThis.iter++;
   console.log("iterations", globalThis.iter);
   const context = createContext();
   for (let i = 0; i < 100; i++) {
     new SourceTextModule(`
       export default function() { }
     `, {
       identifier: 'foo',
       context,
     });
   }
}, 1);

This crashes after around tens of iterations deterministically.

Originally posted by @legendecas in #50113

Activity

  1. added
    vmIssues and PRs related to the vm subsystem.
    on Jul 18, 2025
  2. KernelDeimos commented on Sep 9, 2025

    @KernelDeimos

    It looks like @addaleax might have noticed something 4 years ago that could be related:

    bool IsNotIndicativeOfMemoryLeakAtExit() const override {
      // XXX: The garbage collection rules for ModuleWrap are *super* unclear.
      // Do these objects ever get GC'd? Are we just okay with leaking them?
      return true;
    }

    (this is from #35490)

    That said, my C++ is seriously rusty, so I'm really not sure.

    This comment in lib/internal/modules/esm/utils.js originally made me think the issue was here, but callbackReferrer doesn't seem to be leaking if I know how to correctly interpret the heaps (which I might not!).

    I've uploaded my heaps and the script I'm using to analyze the leak here: https://github.com/KernelDeimos/node-59118 - let me know if I'm on the right track or not; I've never contributed to node before but I really want to be able to use vm.Module without worrying that it will be experimental forever.

  3. joyeecheung commented on Jan 29, 2026

    @joyeecheung
    Member

    I think this was fixed by #59117 - at least, it no longer reproduces on main. Closing.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    vmIssues and PRs related to the vm subsystem.

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions