swc: Segmentation fault on require("@swc/core") in a Node.js worker thread

Running require("@swc/core") inside a Node.js worker thread causes a segfault. (This causes problems with terser-webpack-plugin’s new swcMinify function.)

// test.js
const { Worker } = require("worker_threads");
new Worker('require("@swc/core")', { eval: true });
$ node test.js
Segmentation fault (core dumped)

I’m running Node 14.17.6 on Linux x86-64 (NixOS 21.11). In case this is system-dependent, here’s a Dockerfile that should reproduce reliably:

# Dockerfile
FROM node:14.17.6
WORKDIR /tmp
RUN npm i @swc/core@1.2.88
RUN node -e 'new worker_threads.Worker(`require("@swc/core")`, { eval: true })'
$ docker build .
Sending build context to Docker daemon  57.85MB
Step 1/4 : FROM node:14.17.6
 ---> 9dc0768a6295
Step 2/4 : WORKDIR /tmp
 ---> Using cache
 ---> ade3fef59802
Step 3/4 : RUN npm i @swc/core@1.2.88
 ---> Using cache
 ---> e1b2d22eae67
Step 4/4 : RUN node -e 'new worker_threads.Worker(`require("@swc/core")`, { eval: true })'
 ---> Running in 25299ad85030
Segmentation fault (core dumped)
The command '/bin/sh -c node -e 'new worker_threads.Worker(`require("@swc/core")`, { eval: true })'' returned a non-zero code: 139

About this issue

  • Original URL
  • State: closed
  • Created 3 years ago
  • Reactions: 1
  • Comments: 20 (17 by maintainers)

Most upvoted comments

This issue only occurs on Linux GNU platform with napi-rs. I’ve tried Linux musl / macOS / Windows, all these platform is fine. And I’ve also tried this example https://github.com/nodejs/node-addon-examples/tree/main/async_pi_estimate/node-addon-api on Linux GNU, also works fine.

I need more time to dig into it.

In worker threads, Node.js addons should be able to perform synchronous and asynchronous operations, provided they follow certain rules about thread safety and context awareness.

Yes. I will dig into this issue today.