denoland / deno

A modern runtime for JavaScript and TypeScript.
https://deno.com
MIT License
98.3k stars 5.41k forks source link

Deno ignores `ResourceLimits` for `node:worker_threads` #26156

Open josephrocca opened 1 month ago

josephrocca commented 1 month ago

Version: Deno v2.0.0 (tested as far back as v1.41.3)

Below is a simplified version of this test:

import worker_threads from 'node:worker_threads';
const worker = new worker_threads.Worker(new URL("./worker.mjs", import.meta.url), {
  resourceLimits: {
    maxYoungGenerationSizeMb: 4,
    maxOldGenerationSizeMb: 16,
    codeRangeSizeMb: 16,
    stackSizeMb: 1,
  },
});

And here's ./worker.mjs:

import { resourceLimits } from 'node:worker_threads';
console.log(resourceLimits);

In Node.js it logs this:

resourceLimits: {
  maxYoungGenerationSizeMb: 4,
  maxOldGenerationSizeMb: 16,
  codeRangeSizeMb: 16,
  stackSizeMb: 1
}

And a simple large string allocation test shows that the limit is respected - i.e. it crashes with FATAL ERROR: Reached heap limit Allocation failed if the limit is hit.

In Deno it logs the default values:

resourceLimits: {
  maxYoungGenerationSizeMb: 48,
  maxOldGenerationSizeMb: 2048,
  codeRangeSizeMb: 0,
  stackSizeMb: 4
}

And note that the 16 MB value is not respected. I.e. it seems that the resourceLimits log in the log shown above are "accurate" to the actual limits that the thread is held to, but obviously these are not the limits we asked for when creating the worker.

Related Context:

lucacasonato commented 1 month ago

@josephrocca Are you suggesting that Deno is respecting the set resource limits, but it is not showing the correct values? Or are you saying it does neither?

josephrocca commented 1 month ago

@lucacasonato Deno seems to be ignoring the user-specified limits, and using its own. So it's showing 2048, and I limiting to 2048, but during creation I specified 16. I've updated the issue to try to make that a bit more clear, thanks!