Skip to content

Increase thread stack size limit on Windows #43630

Closed
@tniessen

Description

@tniessen

What is the problem this feature will solve?

Node.js uses a hard-coded default stack size limit on Windows, which is 1 MiB:

Dump of file C:\Program Files\nodejs\node.exe

...

OPTIONAL HEADER VALUES
             ... ...
          100000 size of stack reserve
            1000 size of stack commit

On other platforms, the limit is usually around 8 or 10 MiB and can be changed using ulimit -s.

V8 stack size

The default V8 stack size limit on 64-bit systems is 984 kBytes, which is below the thread stack size limit on all supported operating systems. On Linux, the V8 stack size can usually be increased almost arbitrarily by passing --stack-size=... if needed, and the maximum thread stack size can usually be changed without modifying the node executable (ulimit -s). On Windows, however, raising V8's stack size beyond the hard-coded thread stack size limit of 1 MiB will crash the process:

C:\Users\Tobias>node --stack-size=1100
Welcome to Node.js v18.3.0.
Type ".help" for more information.
> let n = 0; function stackExplode() { n++; return stackExplode(); }; stackExplode()

C:\Users\Tobias>echo %ERRORLEVEL%
-1073741571

Raising the hard-coded thread stack size limit will allow greater manual adjustments through --stack-size=... beyond the default value.

Native addons

Native addons are not necessarily limited by the V8 stack size limit, but they are limited by the hard-coded thread stack size limit (unless they create new threads or fibers). While the stack size limit of 1 MiB is often sufficient, some libraries perform stack allocations exceeding the limit. Such libraries work just fine when using Node.js on Linux but overflow the stack on Windows.

Raising the hard-coded thread stack size limit will simplify creating portable native addons that rely on large stack allocations (however questionable large stack allocations might be).

What is the feature you are proposing to solve the problem?

Increase the thread stack size limit beyond the default value of 1 MiB. See /STACK.

Only increase the reserve size, not the commit size, to avoid an increase in the actual memory usage.


For comparison, Chrome, Edge, and Firefox all use a hard-coded thread stack size limit of 8 MiB.

Dump of file C:\Program Files\Google\Chrome\Application\chrome.exe

...

OPTIONAL HEADER VALUES
             ... ...
          800000 size of stack reserve
            1000 size of stack commit
Dump of file C:\Program Files (x86)\Microsoft\Edge\Application\msedge.exe

...

OPTIONAL HEADER VALUES
             ... ...
          800000 size of stack reserve
            1000 size of stack commit
Dump of file C:\Program Files\Mozilla Firefox\firefox.exe

...

OPTIONAL HEADER VALUES
             ... ...
          800000 size of stack reserve
            1000 size of stack commit

What alternatives have you considered?

  • node-mceliece-nist and node-pqclean switch to a WebAssembly module instead of a native addon on Windows, to support cryptographic implementations that rely on large stack allocations (which are mapped to heap memory in WebAssembly).

  • editbin.exe can be used to change the hard-coded stack size of node.exe:

    editbin.exe /STACK:800000 node.exe
    

Metadata

Metadata

Assignees

No one assigned

    Labels

    addonsIssues and PRs related to native addons.buildIssues and PRs related to build files or the CI.feature requestIssues that request new features to be added to Node.js.windowsIssues and PRs related to the Windows platform.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions