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

Very high CPU load with serialport input #2928

Closed
0x80 opened this issue Sep 28, 2015 · 12 comments
Closed

Very high CPU load with serialport input #2928

0x80 opened this issue Sep 28, 2015 · 12 comments

Comments

@0x80
Copy link

0x80 commented Sep 28, 2015

I have a problem with Electron in combination with serialport. I need to talk to a device which emits frames of 519 bytes at an interval of 27ms and a baudrate of 115200. So nothing spectacular really.

When I run the test code directly in Node it runs fine and uses less than 2% cpu. If I then run the exact same code in Electron my CPU goes through the roof at 130%.

I've tested the code on OSX with iojs v1, v2 and more recently node v4.1.1. The behavior is exactly the same. Also it doesn't make a difference if I run the code in the Electron main or renderer process.

I've shared my test code here. Of course without the device you can't use the serialport test code without altering it a little.

I'm hoping someone can give me pointers on how to investigate further, maybe by profiling the main process? I would love to use Electron for the project but with this bug it's not an option.

@zcbenz
Copy link
Member

zcbenz commented Sep 29, 2015

We have some issues with low level modules like serialport since the way they post events is keep waking up our Node.js message loop, fixing this is impossible or extremely hard.

A popular way to work around this is to fork a Node process to use serialport and then post things back, in Electron child_process.fork should work perfectly in this case. Maybe this will work for you?

@0x80
Copy link
Author

0x80 commented Sep 29, 2015

Thanks for the feedback. A process fork seems like a plausible workaround. Could you be a bit more specific about what is going on with these events? I'd love to understand the problem with serialport a bit more in-depth. How are these events in Electron handled differently from a regular Node environment and what kind of modules are affected? How can you detect when these events walk up the message loop?

@zcbenz
Copy link
Member

zcbenz commented Sep 30, 2015

Electron has its own message loop, so we get notified of Node's events by listening to Node's message loop's backend fd. However when some modules like serialport or pty.js add a few special events to Node's message loop, we will keep getting notified in Electron even when there is nothing happened. This problem is OS X only and I think it is a bug of OS X's kqueue implementation.

If you are interested in this you can look at the atom/common/node_bindings_* to have an idea of the implementation of Node integration.

@0x80
Copy link
Author

0x80 commented Sep 30, 2015

Thanks for this info. Do you think this is something that can be solved by the libraries by not using these special events? Then I would report this to serialport so they can consider it for a future update.

The fork works well. In a previous naive attempt I launched two node processes manually and had them talk over TCP. A fork is much more convenient and efficient.

@zcbenz
Copy link
Member

zcbenz commented Sep 30, 2015

No, those special events are required for their implementations, it is not possible for those libraries to remove that.

@0x80
Copy link
Author

0x80 commented Sep 30, 2015

OK thanks a lot for your help!

@0x80 0x80 closed this as completed Sep 30, 2015
@0x80 0x80 reopened this Sep 30, 2015
mlwmlw added a commit to mlwmlw/esp-editor that referenced this issue Oct 11, 2015
@leyenda
Copy link

leyenda commented Feb 2, 2016

I am getting this error when I try child_process.fork with serialport, and I get really lost about it.

"Uncaught Error: The system cannot find message text for message number 0x%1 in the message file for %2. \\?\C:\Users\user\Documents\electron-quick-start\node_modules\serialport\build\Release\node-v47-win32-x64\serialport.node", source: ATOM_SHELL_ASAR.js (159)

Im using Windows 7x64 and 0.36.7

@ruipimentel
Copy link

@leyenda, were you able to get this to work? :D
I'm having similar trouble while in normal execution ("require"), without even forking, and on this exact same platform.
Thank you in advance!

@leyenda
Copy link

leyenda commented Feb 13, 2016

Hello @ruipimentel, unfortunately I give up on forking, and that was the specific action giving this error message to me. I end up sending the data to my other program over tcp. I am sorry for not being helpful.

@ruipimentel
Copy link

I understand! (so much that I'm actually reconsidering the whole stuff, too!)
And I appreciate that you answered, anyway, so thank you!

@weiway
Copy link
Contributor

weiway commented Feb 29, 2016

for those of you who needs help on using serialport, i built a little tool here for serialport run inside electron https://github.com/weiway/node-serialport-worker

@zcbenz
Copy link
Member

zcbenz commented May 6, 2016

This should have been fixed with #5378.

@zcbenz zcbenz closed this as completed May 6, 2016
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

5 participants