Skip to content

Commit

Permalink
[ESM] Fix crash with worker threads (#285)
Browse files Browse the repository at this point in the history
  • Loading branch information
lehni authored and bjornstar committed Mar 29, 2022
1 parent 09089b1 commit 920dcb3
Showing 1 changed file with 10 additions and 2 deletions.
12 changes: 10 additions & 2 deletions lib/wrap.js
Original file line number Diff line number Diff line change
@@ -1,22 +1,30 @@
const { dirname, extname } = require('path');
const childProcess = require('child_process');
const { sync: resolve } = require('resolve');
const { isMainThread } = require('worker_threads');

const { getConfig } = require('./cfg');
const hook = require('./hook');
const { relay, send } = require('./ipc');
const resolveMain = require('./resolve-main');
const suppressExperimentalWarnings = require('./suppress-experimental-warnings');

// Experimental warnings need to be suppressed in worker threads as well, since
// their process inherits the Node arguments from the main thread.
suppressExperimentalWarnings(process);

// When using worker threads, each thread appears to require this file through
// the shared Node arguments (--require), so filter them out here and only run
// on the main thread.
if (!isMainThread) return;

const script = process.argv[1];
const { extensions, fork, vm } = getConfig(script);

if (process.env.NODE_DEV_PRELOAD) {
require(process.env.NODE_DEV_PRELOAD);
}

suppressExperimentalWarnings(process);

// We want to exit on SIGTERM, but defer to existing SIGTERM handlers.
process.once('SIGTERM', () => process.listenerCount('SIGTERM') || process.exit(0));

Expand Down

0 comments on commit 920dcb3

Please sign in to comment.