Skip to content

Worker threads: the process keeps living on exception #22736

Closed
@wilk

Description

@wilk
  • Version: v10.9.0
  • Platform: Darwin 17.7.0 Darwin Kernel Version 17.7.0
  • Subsystem:

The problem is related to Node.js worker threads: https://nodejs.org/api/worker_threads.html
The worker thread is spawned as illustrated in the following example:

// main.js
const {Worker} = require('worker_threads')

try {
  new Worker('./worker.js', {
    workerData: {fn: () => {}}
  })
} catch (err) {
  console.log(err)
}

And then this is the actual worker:

// worker.js
const {parentPort, workerData} = require('worker_threads')

console.log(workerData)

parentPort.postMessage('ok')

Now, worker threads do not support elements such as functions and classes in worker data and an exception is thrown when main.js is launched:

$ node --experimental-worker main.js

The output is this:

$ node --experimental-worker main.js 
DataCloneError: () => {} could not be cloned.
    at new Worker (internal/worker.js:269:17)
    at Object.<anonymous> (/Users/wilk/Projects/mine/task/main.js:22:3)
    at Module._compile (internal/modules/cjs/loader.js:689:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:700:10)
    at Module.load (internal/modules/cjs/loader.js:599:32)
    at tryModuleLoad (internal/modules/cjs/loader.js:538:12)
    at Function.Module._load (internal/modules/cjs/loader.js:530:3)
    at Function.Module.runMain (internal/modules/cjs/loader.js:742:12)
    at startup (internal/bootstrap/node.js:266:19)
    at bootstrapNodeJSCore (internal/bootstrap/node.js:596:3)

Unfortunately, the process keeps alive instead of dying.
However, if I prepend the v8 serialization before the worker instantiation, then it terminates as expected:

// main.js
const {Worker} = require('worker_threads')
const v8 = require('v8')

try {
  v8.serialize({fn: () => {}})
  new Worker('./worker.js', {
    workerData: {fn: () => {}}
  })
} catch (err) {
  console.log(err)
}

I think the issue is with v8 serialization's error handling inside the Worker's constructor.

Metadata

Metadata

Assignees

No one assigned

    Labels

    workerIssues and PRs related to Worker support.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions