Skip to content

v8: JSON.stringify fast path allocates memory proportional to string length before throwing on a cyclic object #67480

Open

Description

@panjinhe

Version

v26.13.1 (V8 14.6.202.34), v26.7.0 (V8 14.6.202.34), v28.0.0-v8-canary2026111459e40f5e3c (V8 16.0.40). v24.22.3 (V8 13.6.233.17) only with --json-stringify-fast-path.

Platform

Linux 7.0.0-42-generic x86_64 (Ubuntu 24.04)

Subsystem

v8 (JSON.stringify fast path)

What steps will reproduce the bug?

// repro.js
const len = Number(process.argv[2] ?? 6000)
let text = 'a'.repeat(len)
text = JSON.parse(JSON.stringify(text)) // flat sequential string
const cycle = { text }
cycle.self = cycle
const t0 = performance.now()
try {
  JSON.stringify(cycle)
} catch (err) {
  console.log(`${err.constructor.name} after ${Math.round(performance.now() - t0)} ms`)
}
$ /usr/bin/time -f 'peak %M KB' node repro.js 6000
TypeError after 2626 ms
peak 2043956 KB

How often does it reproduce? Is there a required condition?

Every run. The object must contain a cycle and a flat string. With a cons string (drop the JSON.parse(JSON.stringify(...)) line) the TypeError is immediate, presumably because cons strings go to the slow path.

Time and peak memory before the TypeError grow linearly with the string length, at roughly 340 bytes of peak RSS per character:

Build len 600 len 6000
v22.23.5 0 ms, 45 MB 0 ms, 45 MB
v24.22.3 0 ms, 48 MB 0 ms, 47 MB
v24.22.3 --json-stringify-fast-path 212 ms, 192 MB 2635 ms, 2.0 GB
v26.13.1 185 ms, 193 MB 2626 ms, 2.0 GB
v26.13.1 --no-json-stringify-fast-path 0 ms, 51 MB 0 ms, 51 MB
v28.0.0-v8-canary2026111459e40f5e3c 181 ms, 193 MB 2595 ms, 2.0 GB

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

TypeError: Converting circular structure to JSON right away with constant memory, as with the slow path and Node 22/24 defaults.

What do you see instead?

The fast path keeps serializing the cycle and only throws after allocating memory proportional to the string length. With a 280 KB string the process grows by about 200 MB/s and needs on the order of 90 GB, so in practice it never throws: it exhausts host memory and is OOM-killed. While it runs the main thread is inside v8::internal::FastJsonStringifier<...>::SerializeObject and does not service signals handled in JS, so node --test ignores SIGTERM until SIGKILL.

Additional information

Found through a unit test that asserts JSON.stringify rejects a cyclic input holding a large string (dofastted/vm2api#1061). #66489 is a different failure in the same fast path (abort on a very long flat string). --no-json-stringify-fast-path works around it.

Activity

  1. Renegade334 commented on Nov 15, 2026

    @Renegade334
    Member

    This has been deemed wontfix.

    https://issues.chromium.org/issues/572287937

  2. added
    wontfixIssues that will not be fixed.
    v8 engineIssues and PRs related to the V8 dependency.
    on Nov 15, 2026
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

    v8 engineIssues and PRs related to the V8 dependency.wontfixIssues that will not be fixed.

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions