Home > Back-end >  Best way to trigger worker_thread OOM exception in Node.js
Best way to trigger worker_thread OOM exception in Node.js

Time:02-11

I would like to test code that reports when a worker_thread OOMs. Or at least that the parent is OK despite the thread crashing due to OOM reasons. I would like to specifically test Node.js killing a worker_thread.

I'm not even sure this is particularly testable since the environment in which Node.js is running seems to make a difference. Setting low old generation size (docs) using resource limits does not behave the way I thought it would, it seems Node.js and the OS are doing a lot of clever things to keep the process from blowing up in memory. The closest I have gotten is that both Node.js AND the worker are killed. But again, this is not the behaviour I was expecting - Node.js should be killing the worker.

Is there any way to reliably test for something like this? Currently running v16.13.2.

[EDIT]

Here is some sample JS for the worker:

const { isMainThread } = require('worker_threads');

if (!isMainThread) {
  let as = [];

  for (let i = 0; i < 100; i  ) {
    let a = '';
    for (let i = 0; i < 9000000; i  ) a  = i.toString();
    as.push(a);
  }
}

CodePudding user response:

OK, it looks like adding maxYoungGenerationSizeMb in my case is resulting in the behaviour I was looking for: e.g. { maxOldGenerationSizeMb: 10, maxYoungGenerationSizeMb: 10 }.

  • Related