-
-
Notifications
You must be signed in to change notification settings - Fork 1k
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
Serial port commnunication does not work inside a Worker Thread #1938
Comments
I don't know if this would be realted to N-API. I do think you might have found a bug in workers however. =) |
Ok. I submitted a bug report at node: |
Not a bug in Node.js. :) Quoting the Worker threads documentation:
In short, your addon needs to be declared as context-aware and support multiple threads natively. For N-API, it’s assumed that that’s the case. Just taking a quick look at the source code here, at least the usage of |
Thanks @addaleax, my knowledge around figuring out how to fix this is not great. There’s no reason why we need the default event loop. And there’s no shared context between serial ports so they could in theory work in workers. I imagine the only thing we’d need to do in the cleanup hook is close the port if it’s open. (Open state is normally kept in JS) Any pointers would be much appreciated. Thanks! 🙏 |
@reconbot You can probably use Node’s |
Got to start moving to NAPI anyway 🤷♂️ |
Really tried to understand what has to be done to implement the cleanupHooks, but actually I'm lost in all this c++. This is not my domain :) |
Hello. Sad news for me. const SerialPort = require('serialport')
const port = new SerialPort('/dev/tty.usbserial-A7TRF2V', { baudRate: 9600 })
port.on('data', (data) => { console.log(data) }) But if I run the same code through worker_threads I get a very sad message const { Worker } = require('worker_threads')
const worker = new Worker('./test.js', {})
worker.on('message', () => { })
In some ways, I found out the essence of the error
It would be great if there was someone who could fix it. It's bad that there is code that can kill the main process. OS: MacOS Thanks you. |
@ponikrf and anyone hitting this, you can use |
Based on the discussion, it sounds like migration from NAN to N-API is a stepping stone to resolving this. Is that still understood to be the case? I believe there was a previous endevour to migrate to N-API #1186 While that attempt was a while ago, I wonder what code or lessons can be gleaned from that previous work? |
Hi @hugo-a-garcia would you be able to take a look at PR #2305 and give it a try? While that PR isn't directly trying to address this issue, I think it might be easier to fix this issue in the N-API version. As such if you are able to share info of any errors you find, or perhaps a sample project you are trying to get to work then it might help fix this issue in the next major version of the package :-) |
I am encountering this in a different situation. The callstack seems different as well than what was posted here previously. On the other hand I am not trying to use any worker threads in code that uses My setup is something like this:
Now the rust napi native module that I am attempting to test does use threads, so I think the two somehow conflict, but I hardly grasp how. |
Hello guys, so it is working now? |
Doesn't look like it. I'm getting the same error:
|
Interested in this |
We landed napi support and support workers as of 2 years ago. |
This still isn't working for me. I get the same error as this:
|
Still using this library inside worker thread is not working for me with SerialPort v12 and Node v20 at x64 (WSL) or inside Docker on arm64 platform. Is there any way to get this working? |
Having this problem too, not using WorkerThreads though. When running in dev mode (using SvelteKit that uses Vite), everything works fine, but I get the following error when the app is built and right before the parser is supposed to receive data from Arduino in flowing mode. Any progress on this issue? Thank you. |
Yeah also having the same issue. Using Nuxt and starting the serialport communication inside a server plugin. The issue only exists during dev. After building the Nuxt application it seems to work. |
Summary of Problem
Opening a serial port from within a worker thread fails with:
Error: Module did not self-register.
Code to Reproduce the Issue
github repo: hugo-a-garcia/serial-worker
Versions, Operating System and Hardware
Possible related issue: N-API support for node-serialport #1186
The text was updated successfully, but these errors were encountered: