nodejs / nodejs/node

OOM Error When Creating Many ShadowRealms with Heap Snapshots in Node.js

Đang mở
#62,433 0 bình luận 0 reaction 0 người được giao Xem trên GitHub

Chưa có ai nhận issue này.

Ngôn ngữ chính
JavaScript
Star
122k
Fork
37.3k
Merge trung bình
4 ngày 2 giờ
Pull request đã merge (30 ngày)
283

Mô tả

Version

24.13.1, 25.8.1

Platform
Linux KContainer 5.10.0-12-amd64 #1 SMP Debian 5.10.103-1 (2022-03-07) x86_64 x86_64 x86_64 GNU/Linux
Subsystem

No response

What steps will reproduce the bug?

The below PoC can reproduce the result

import { writeHeapSnapshot } from 'v8';

// This script simulates creating many ShadowRealms and taking heap snapshots

async function main() {
  if (typeof ShadowRealm === 'undefined') {
    console.error('ShadowRealm is not available in this Node build');
    process.exit(1);
  }

  const realms = [];

  for (let i = 0; i < 20000; i++) {
    const realm = new ShadowRealm();
    // realms.push(realm);

    // Take heap snapshots periodically
    if (i % 500 === 0) {
      writeHeapSnapshot(`heap-${i}.heapsnapshot`);
      console.log('Created ShadowRealm and heap snapshot at iteration', i);
    }
  }

  console.log('Total ShadowRealms created:', realms.length);
}

main();
How often does it reproduce? Is there a required condition?

No special condition

What is the expected behavior? Why is that the expected behavior?

Even though many objects are allocated, the runtime should handle memory pressure gracefully. The interaction between ShadowRealm creation and heap snapshotting appears to prevent effective garbage collection.

What do you see instead?
root@KContainer:~/49572# node --experimental-shadow-realm --max-old-space-size=1024 test-heapdump-shadow-realm.mjs
Created ShadowRealm and heap snapshot at iteration 0
Created ShadowRealm and heap snapshot at iteration 500

<--- Last few GCs --->

[6100:0x7db2000]    40950 ms: Mark-Compact 995.6 (1033.9) -> 987.2 (1052.6) MB, pooled: 0 MB, 438.54 / 0.00 ms  (average mu = 0.552, current mu = 0.272) allocation failure; scavenge might not succeed
[6100:0x7db2000]    41755 ms: Mark-Compact (reduce) 1002.5 (1054.6) -> 996.6 (1028.6) MB, pooled: 0 MB, 109.72 / 0.01 ms  (+ 572.7 ms in 115 steps since start of marking, biggest step 5.1 ms, walltime since start of marking 725 ms) (average mu = 0.398, cu
FATAL ERROR: Reached heap limit Allocation failed - JavaScript heap out of memory
----- Native stack trace -----

 1: 0x733eec node::OOMErrorHandler(char const*, v8::OOMDetails const&) [node]
 2: 0xbab3f0  [node]
 3: 0xbab4df  [node]
 4: 0xe43fd5  [node]
 5: 0xe5515c  [node]
 6: 0xd8ca98  [node]
 7: 0x12f5f1e  [node]
 8: 0x19ff636  [node]
Aborted

Code throws OOM after heap snapshot at iteration 500. Ideally, it should keep going as in the case of Bun.

Additional information

Related issue:
https://github.com/nodejs/node/issues/49572

Hướng dẫn đóng góp

Mở hướng dẫn đóng góp

Bắt đầu từ đâu

  1. Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
  2. Bình luận trên issue rằng bạn sẽ nhận — tránh hai người làm cùng một việc.
  3. Fork repository và làm thay đổi trên một nhánh.
  4. Mở pull request có tham chiếu số hiệu của issue.

Hướng nghiên cứu

Bắt đầu bằng cách chạy bản tái hiện được cung cấp test-heapdump-shadow-realm.mjs với Node 24.13.1 hoặc 25.8.1 và giới hạn heap được nêu, sau đó so sánh hành vi liên quan đến heap snapshot và việc tạo ShadowRealm với issue liên quan #49572. Được xem là hoàn tất khi bản tái hiện không còn dẫn đến lỗi hết bộ nhớ trong các điều kiện đã báo cáo.

Do mô hình lập chỉ mục viết ra từ nội dung của issue.

Đánh giá

Công nghệ
javascript, node.js
Lĩnh vực
backend
Loại issue
Lỗi
Độ khó
4/5
Thời gian dự kiến
3-5 ngày
Mức độ hoạt động
Ít trao đổi
Độ rõ ràng
Khá rõ ràng
Mức phù hợp với người mới
45/100

Nhận issue mới trong hộp thư của bạn

Bản tóm tắt ngắn những issue GitHub phù hợp với người mới.