serialport / node-serialport

Access serial ports with JavaScript. Linux, OSX and Windows. Welcome your robotic JavaScript overlords. Better yet, program them!
https://serialport.io
MIT License
5.79k stars 1.01k forks source link

Cannot work in a worker thread... please save me #2748

Open 61go opened 6 months ago

61go commented 6 months ago

SerialPort Version

12+

Node Version

v20

Electron Version

No response

Platform

No response

Architecture

64

Hardware or chipset of serialport

No response

What steps will reproduce the bug?

`

// main.ts

import { Worker } from 'worker_threads';

if (require.main === module) { const worker = new Worker('./dist/serialWorker.js');

worker.on('message', (message) => { console.log('Received message from worker:', message); });

worker.postMessage({ type: 'START_SERIAL_PORT', port: '/dev/ttyUSB0', baudRate: 9600 }); }`

`// serialWorker.ts

import { parentPort } from 'worker_threads'; import { SerialPort, SerialPortOpenOptions } from 'serialport';

if (parentPort) { parentPort.on('message', (message) => { if (message.type === 'START_SERIAL_PORT') { // const { port, baudRate } = message;

  const portOptions: SerialPortOpenOptions<any> = {
    path: 'COM4',
    baudRate: 921600,
    dataBits: 8,
    parity: 'none',
    stopBits: 1,
    autoOpen: false,
  };
//   if (!port) {
    let  port = new SerialPort(portOptions , () => {
      console.log('serial port opened');
    });
//   }
  port.open(() => {
      port.on("data", (data: Buffer) => {
        console.log(data.toString());
      });
    });

  parentPort.postMessage({ type: 'SERIAL_PORT_STARTED' });
}

}); }

`

What happens?

when i use seriaport in worker thread, it error:

D:\codes\desktop-mmb>node dist/index.js Received message from worker: { type: 'SERIAL_PORT_STARTED' } FATAL ERROR: HandleScope::HandleScope Entering the V8 API without proper locking in place 1: 00007FF6A8D7436F node::SetCppgcReference+15695 2: 00007FF6A8CEC686 EVP_MD_meth_get_input_blocksize+76102 3: 00007FF6A8CEE57C node::OnFatalError+252 4: 00007FF6A974F549 v8::HandleScope::Initialize+137 5: 00007FF6A97376FE v8::HandleScope::HandleScope+14 6: 00007FF6A8D3FD8A napi_open_handle_scope+154 7: 00007FFE7CD0AFEA 8: 00007FF6A8DD8A6B uv_update_time+491 9: 00007FF6A8DD85E4 uv_run+900 10: 00007FF6A8DA94D2 node::SpinEventLoop+402 11: 00007FF6A8C9CAB3 DH_get0_priv_key+151875 12: 00007FF6A8D2E925 node::Start+3989 13: 00007FF6A8D2D9C0 node::Start+48 14: 00007FF6A8B0D76C AES_cbc_encrypt+150940 15: 00007FF6A9F3BCDC inflateValidate+17308 16: 00007FFE9AF2257D BaseThreadInitThunk+29 17: 00007FFE9B7CAA58 RtlUserThreadStart+40

What should have happened?

when i use it in mainthread, it works. but i really need to use it in worker thread....

Additional information

aaaa

jsharf commented 6 months ago

Looks related to #1938.

lbasedow-cw commented 3 months ago

The very same happens for me with SerialPort v12 and Node v20 in x64 (WSL) or inside Docker on arm64 platform.

FATAL ERROR: HandleScope::HandleScope Entering the V8 API without proper locking in place
----- Native stack trace -----

 1: 0xb82b0c node::OnFatalError(char const*, char const*) [node]
 2: 0xeed116 v8::Utils::ReportApiFailure(char const*, char const*) [node]
 3: 0xeee7dc v8::HandleScope::HandleScope(v8::Isolate*) [node]
 4: 0xc5b8ea napi_open_handle_scope [node]
 5: 0x7faca44185eb Poller::onData(uv_poll_s*, int, int) [./node_modules/@serialport/bindings-cpp/prebuilds/linux-x64/node.napi.glibc.node]
 6: 0x18c368b  [node]
 7: 0x18af937 uv_run [node]
 8: 0xbc7be6 node::SpinEventLoopInternal(node::Environment*) [node]
 9: 0xd0a924  [node]
10: 0xd0b3bd node::NodeMainInstance::Run() [node]
11: 0xc6fc0f node::Start(int, char**) [node]
12: 0x7faccd69fd90  [/lib/x86_64-linux-gnu/libc.so.6]
13: 0x7faccd69fe40 __libc_start_main [/lib/x86_64-linux-gnu/libc.so.6]
14: 0xbc5c8e _start [node]
FATAL ERROR: v8::HandleScope::CreateHandle() Cannot create a handle without a HandleScope
----- Native stack trace -----

 1: 0xb82b0c node::OnFatalError(char const*, char const*) [node]
 2: 0xeed116 v8::Utils::ReportApiFailure(char const*, char const*) [node]
 3: 0x10932e2 v8::internal::HandleScope::Extend(v8::internal::Isolate*) [node]
 4: 0xeee9f8 v8::EscapableHandleScope::EscapableHandleScope(v8::Isolate*) [node]
 5: 0xcba046 node::GetCurrentStackTrace(v8::Isolate*, int) [node]
 6: 0xc0d79b node::DumpJavaScriptBacktrace(_IO_FILE*) [node]
 7: 0xb82b18 node::OnFatalError(char const*, char const*) [node]
 8: 0xeed116 v8::Utils::ReportApiFailure(char const*, char const*) [node]
 9: 0xeee7dc v8::HandleScope::HandleScope(v8::Isolate*) [node]
10: 0xc5b8ea napi_open_handle_scope [node]
11: 0x7faca44185eb Poller::onData(uv_poll_s*, int, int) [./node_modules/@serialport/bindings-cpp/prebuilds/linux-x64/node.napi.glibc.node]
12: 0x18c368b  [node]
13: 0x18af937 uv_run [node]
14: 0xbc7be6 node::SpinEventLoopInternal(node::Environment*) [node]
15: 0xd0a924  [node]
16: 0xd0b3bd node::NodeMainInstance::Run() [node]
17: 0xc6fc0f node::Start(int, char**) [node]
18: 0x7faccd69fd90  [/lib/x86_64-linux-gnu/libc.so.6]
19: 0x7faccd69fe40 __libc_start_main [/lib/x86_64-linux-gnu/libc.so.6]
20: 0xbc5c8e _start [node]
Aborted
FATAL ERROR: HandleScope::HandleScope Entering the V8 API without proper locking in place
----- Native stack trace -----
 1: 0xb7d164 node::OnFatalError(char const*, char const*) [node]
 2: 0xeb1d78 v8::Utils::ReportApiFailure(char const*, char const*) [node]
 3: 0xeb33ec v8::HandleScope::HandleScope(v8::Isolate*) [node]
 4: 0xc45d00 napi_open_handle_scope [node]
 5: 0xffff9c276d44 Poller::onData(uv_poll_s*, int, int) [./node_modules/@serialport/bindings-cpp/prebuilds/linux-arm64/node.napi.armv8.node]
 6: 0x1803d34  [node]
 7: 0x17f11ac uv_run [node]
 8: 0xbba140 node::SpinEventLoopInternal(node::Environment*) [node]
 9: 0xce511c  [node]
10: 0xce5a68 node::NodeMainInstance::Run() [node]
11: 0xc585a0 node::Start(int, char**) [node]
12: 0xffff9fc573fc  [/lib/aarch64-linux-gnu/libc.so.6]
13: 0xffff9fc574cc __libc_start_main [/lib/aarch64-linux-gnu/libc.so.6]
14: 0xbb8460  [node]
FATAL ERROR: v8::HandleScope::CreateHandle() Cannot create a handle without a HandleScope
----- Native stack trace -----
 1: 0xb7d164 node::OnFatalError(char const*, char const*) [node]
 2: 0xeb1d78 v8::Utils::ReportApiFailure(char const*, char const*) [node]
 3: 0x105344c v8::internal::HandleScope::Extend(v8::internal::Isolate*) [node]
 4: 0xeb3668 v8::EscapableHandleScope::EscapableHandleScope(v8::Isolate*) [node]
 5: 0xc9cadc node::GetCurrentStackTrace(v8::Isolate*, int) [node]
 6: 0xbfd700 node::DumpJavaScriptBacktrace(_IO_FILE*) [node]
 7: 0xb7d16c node::OnFatalError(char const*, char const*) [node]
 8: 0xeb1d78 v8::Utils::ReportApiFailure(char const*, char const*) [node]
 9: 0xeb33ec v8::HandleScope::HandleScope(v8::Isolate*) [node]
10: 0xc45d00 napi_open_handle_scope [node]
11: 0xffff9c276d44 Poller::onData(uv_poll_s*, int, int) [./node_modules/@serialport/bindings-cpp/prebuilds/linux-arm64/node.napi.armv8.node]
12: 0x1803d34  [node]
13: 0x17f11ac uv_run [node]
14: 0xbba140 node::SpinEventLoopInternal(node::Environment*) [node]
15: 0xce511c  [node]
16: 0xce5a68 node::NodeMainInstance::Run() [node]
17: 0xc585a0 node::Start(int, char**) [node]
18: 0xffff9fc573fc  [/lib/aarch64-linux-gnu/libc.so.6]
19: 0xffff9fc574cc __libc_start_main [/lib/aarch64-linux-gnu/libc.so.6]
20: 0xbb8460  [node]

My code is pretty much the same as the example above.

Is there any way to get this working?

egidiusmengelberg commented 1 week ago

Yeah, having the same issue. I'm trying to integrate serialport in my iot nuxt app.

But nuxt is using workers so getting the same error