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

Node event loop stops while the write operation is in progress #1363

Closed
kimushu opened this issue Oct 16, 2017 · 5 comments
Closed

Node event loop stops while the write operation is in progress #1363

kimushu opened this issue Oct 16, 2017 · 5 comments

Comments

@kimushu
Copy link
Contributor

kimushu commented Oct 16, 2017

  • SerialPort Version: 6.0.0

  • NodeJS Version: 6.9.5 (64-bit)

  • Operating System and Hardware Platform: Windows 10 Pro (10.0.15063) x64

    • Serial port: FTDI USB-Serial converter cable TTL-232R-3V3 (FT232RQ chip)
  • Have you checked the right version of the api docs?: yes

  • Are you having trouble installing and you checked the Installation Special Cases docs? no

  • Are you using Electron and have you checked the Electron Docs?: no

Summary of Problem

Event loop stops even if there is an incomplete write operation.
For example, simple open & write program (here) does not work.
I guess the new I/O for windows (#1221) does not preserve any AsyncWorker in write operation.

Steps and Code to Reproduce the Issue

  • Event loop stops before message written message if there are no pending callbacks nor timers.
$ cat open_write.js
var SerialPort = require('serialport');
var port = new SerialPort('COM8');

port.write('main screen turn on', function(err) {
  if (err) {
    return console.log('Error on write: ', err.message);
  }
  console.log('message written');
});

// Open errors will be emitted as an error event
port.on('error', function(err) {
  console.log('Error: ', err.message);
});

$ node open_write.js
                                        # <- No output here!
$ echo $?
0                                       # <- node stopped without error...
  • If there are some pending callbacks or timers, write operation & callback seem to be ok. However, event loop does not stop even if write operation has been finished
$ cat open_write_with_timer.js
var SerialPort = require('serialport');
var port = new SerialPort('COM8');

port.write('main screen turn on', function(err) {
  if (err) {
    return console.log('Error on write: ', err.message);
  }
  console.log('message written');
});

// Open errors will be emitted as an error event
port.on('error', function(err) {
  console.log('Error: ', err.message);
});

setTimeout(() => { console.log("workaround!"); }, 1000);

$ node open_write_with_timer.js
message written                         # <- Write callback is ok...
workaround!
                                        # However, event loop seems be frozen (Node is still running...)
@reconbot
Copy link
Member

reconbot commented Oct 16, 2017

So if we have a pending write the event loop still appears empty and node will quit. And possibly we keep something in the loop queue after the write finishes so we never quit.

@kimushu
Copy link
Contributor Author

kimushu commented Oct 17, 2017

Correct. There are two problems.
As for the former one (node quit during write), I have an idea to fix it by adding uv_idle (kimushu@f3a94bf)

@kimushu
Copy link
Contributor Author

kimushu commented Oct 18, 2017

uv_async_t handle is not correctly closed (here). This disturbs finish of event loop.
I have a proposal to solve both problems in this issue. See #1367.

reconbot pushed a commit that referenced this issue Oct 18, 2017
@reconbot
Copy link
Member

This is was great, thank you so much. It's always nice when people give good bug reports, and it's even more amazing when they close them too. =)

@kimushu
Copy link
Contributor Author

kimushu commented Oct 19, 2017

It's a pleasure to contribute :-)

@lock lock bot locked and limited conversation to collaborators Apr 24, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Development

No branches or pull requests

2 participants