Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

"Segmentation fault" in worker thread on ubuntu #377

Closed
ThisNils opened this issue Oct 23, 2021 · 5 comments
Closed

"Segmentation fault" in worker thread on ubuntu #377

ThisNils opened this issue Oct 23, 2021 · 5 comments
Assignees

Comments

@ThisNils
Copy link
Contributor

When running the library inside a worker thread on ubuntu (I had no problems running the exact same code on windows, same library and nodejs version), the program exits with the message "Segmentation fault". The image gets rendered and transferred to the main thread correctly, but as soon as the worker exits, the main thread will terminate too.

Example code:

// index.js
const { Worker } = require('worker_threads');
const { promises: fs } = require('fs');

const worker = new Worker('./worker.js');
worker.on('message', async (m) => {
  await fs.writeFile('./out.png', m);
});
// worker.js
const { parentPort } = require('worker_threads');
const { createCanvas } = require('@napi-rs/canvas');

const canvas = createCanvas(1000, 1000);
const ctx = canvas.getContext('2d');

ctx.fillStyle = 'black';
ctx.fillRect(0, 0, canvas.width, canvas.height);

const pngBuffer = canvas.toBuffer('image/png');
parentPort.postMessage(pngBuffer);

Output:

# node index.js
Segmentation fault

Versions:
NodeJS: v16.11.1
@napi-rs/canvas: 0.1.13
Ubuntu: 18.04.5 LTS

@Brooooooklyn Brooooooklyn self-assigned this Oct 24, 2021
@Brooooooklyn
Copy link
Owner

I'll dig into this problem. BTW, you don't need run @napi-rs/canvas in worker_thread, it provides non-blocking API which is running in libuv thread.

@rambo-panda
Copy link
Contributor

I also have this problem。When I terminate a worker, I will start this error

@Brooooooklyn
Copy link
Owner

@rambo-panda what's your Node.js version, crash on Node@12 is a known issue nodejs/node#42376

@Brooooooklyn
Copy link
Owner

Please don't use @napi-rs/canvas in worker_thread since @napi-rs/canvas already has the no-blocking API, which will schedule the tasks to cross threads:

Promise.all([
  canvas.encode('png'),
  canvas.encode('webp'),
  canvas.encode('jpg'),
  canvas.encode('avif'),
])

The code snippets will start 4 threads and resolve the result when the task is completed.

@rambo-panda
Copy link
Contributor

@Brooooooklyn

  {
  node: '14.15.4',
  v8: '8.4.371.19-node.17',
  uv: '1.40.0',
  zlib: '1.2.11',
  brotli: '1.0.9',
  ares: '1.16.1',
  modules: '83',
  nghttp2: '1.41.0',
  napi: '7',
  llhttp: '2.1.3',
  openssl: '1.1.1i',
  cldr: '37.0',
  icu: '67.1',
  tz: '2020a',
  unicode: '13.0'
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants