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

Buggy close() routine - Node "hangs" #150

Closed
bminer opened this issue Mar 29, 2013 · 22 comments
Closed

Buggy close() routine - Node "hangs" #150

bminer opened this issue Mar 29, 2013 · 22 comments
Labels
Milestone

Comments

@bminer
Copy link

bminer commented Mar 29, 2013

The serial port does not properly close on Debian Linux. The "close" event is fired, however. In addition, Node will hang even after close() is called.

Code to reproduce:

var SerialPort = require("SerialPort").SerialPort;
var port = new SerialPort("/dev/ttySerial0", {
    "baudRate": 57600
});
port.on("open", function() {
    console.log("open", arguments);
    setTimeout(function() {
        console.log("Closing serial port");
        port.close();
    }, 2000);
});
port.on("error", function() {
    console.log("error", arguments);
});
port.on("data", function() {
    console.log("data", arguments);
});
port.on("close", function() {
    console.log("close", arguments);
});

Output when you run this program:

open {}
Closing serial port
close {}

Then... Node just hangs forever... (undesirably)

Furthermore, if I remove the serial port (i.e. /dev/ttySerial0 goes away), I get the following error:

events.js: 71
    throw arguments[1]; // Unhandled 'error' event
Error EIO, read

Then Node crashes. How could there be an unhandled error event if I am binding an event listener to the "error" event, as shown in the code above?

This is a bit buggy. Maybe the close() routine doesn't really release the serial port... I've found that if I try to re-use the port, an error occurs. Thoughts?

@Starefossen
Copy link

I am experiencing the exact same problem.

@Starefossen
Copy link

Any thoughts on this one @voodootikigod?

@Starefossen
Copy link

If you need your app to quit – a workaround is to use process.kill(process.pid, 'SIGINT');

@bminer
Copy link
Author

bminer commented Apr 26, 2013

@Starefossen - process.kill or process.exit is a valid workaround... yet, it's really REALLY REALLY lame. Excuse me while I go puke. :)

@voodootikigod
Copy link
Collaborator

Hey all,

Sorry been busy planning JSConf and having a baby. @bminer the bold "really" and calling things lame isn't cool. While I fully get the frustration, I can definitely use a bit more data about what is going on, can someone please provide an strace/dtrace output of the execution. This will drastically help expedite a fix.

@Starefossen
Copy link

Hi @voodootikigod,

Congrats on becoming a father! I wish I lived closer to the US so I could participate in JSConf!

Here is my strace log: http://pastebin.com/PXhYKPTs
Here is my demo program: http://pastebin.com/YZyc73GW

Tested on NodeJS v0.10.5 on Ubuntu Server 12.04.2 32 bit

@voodootikigod
Copy link
Collaborator

awesome! thanks, will dig into.

On Fri, Apr 26, 2013 at 12:11 PM, Hans Kristian Flaatten <
notifications@github.com> wrote:

Hi @voodootikigod https://github.com/voodootikigod,

Congrats on becoming a father! I wish I lived closer to the US so I could
participate in JSConf!

Here is my strace log: http://pastebin.com/PXhYKPTs
Here is my demo program: http://pastebin.com/YZyc73GW

Tested on NodeJS v0.10.5 on Ubuntu 12.04.2


Reply to this email directly or view it on GitHubhttps://github.com//issues/150#issuecomment-17084012
.

Chris Williams

@voodootikigod http://twitter.com/voodootikigod |
GitHubhttp://github.com/voodootikigod

The things I make that you should check out:
SaferAging http://www.saferaging.com/ | JSConf http://jsconf.com/ |
PromoteJS http://promotejs.com/ | Minute With http://minutewith.com/

Help me end the negativity on the internet, share
thishttp://jsconf.eu/2011/an_end_to_negativity.html
.

@bminer
Copy link
Author

bminer commented Apr 26, 2013

@voodootikigod - Congrats on the infant, and I understand that you're busy. I wasn't trying to offend anyone by pulling the lame card.

Thanks @Starefossen for providing the strace. I'll try to dig into this, as well, time permitting.

@jamesjenner
Copy link

I've just tested this and I didn't have a problem. I'm using Ubuntu 12.04 64bit version and node.js v0.10.5.

I should note that the example code posted by the op doesn't work on Linux by default. I had to change

var SerialPort = require("SerialPort").SerialPort;

to

var SerialPort = require("serialport").SerialPort;

@Starefossen is your system 32 or 64 bit? Otherwise I'm not sure what is different between our systems.

:edit:

Just realised that one difference is that I'm receiving data on the port.

:edit 2:

Yep, just retested with my device disconnected and I get the problem. So it only occurs if no data has been received (or maybe it's no data on the input buffer) when you close the serial port.

If you are receiving data then it works fine. In my case I'm receiving telemetry from a UAV and it's very rapid.

Hope that helps in finding the problem.

Cheers,
James.

@Starefossen
Copy link

@jamesjenner I am actually running the 32 bit version of Ubuntu.

I am receiving data from the device with no problems; and the device is connected during the entire session. However; the example program never terminates, it just hangs when attempting to cose the port.

@jamesjenner
Copy link

Hmm strange. In my case i can close the port and the program will terminate if I'm receiving data. The program only hangs if I attempt to close the port when the port has not received data.

@bminer
Copy link
Author

bminer commented May 7, 2013

I can also confirm that whether I receive data or not, the port does not seem to close successfully, and Node will hang. Using Debian Wheezy (7.0) 64-bit.

@giseburt
Copy link
Contributor

I'm seeing this issue on OS X as well. Sometimes it won't close the connection (when I forcefull kill node), which leaves me no choice but to physically disconnect the device. Since the device is an FTDI chip, this often kernel panics my Mac.

I looked at the code and could not find the issue cleanly, other that the close(fd) doesn't while if it returns an error. (It can be interrupted.)

@D1plo1d
Copy link

D1plo1d commented Aug 10, 2013

I'm seeing the same issue on OSX 10.7.5. Like @amesjenner I am only able to close the port and have node exit properly once I've received some data from the serial port.

giseburt added a commit to giseburt/node-serialport that referenced this issue Aug 19, 2013
Flow control options haven't been added to windows yet.
@secesh
Copy link

secesh commented Sep 13, 2013

@giseburt, that fix doesn't work for me.

I haven't really looked into the matter. I just want a fix/work-around. I publish solution here in case it might help anyone else.

I use USB serial port. As mentioned here, communication can have a tendency to lock up (stops receiving data and can prevent process from exiting).
When it locks up, the solutions are 1) unplug/replug the USB device. aside: I tried rmmod followed by modprobe, but I have a dual DB9 dongle, and this would only fix port1. 2) restart the PC.

Neither of those provided an desired solution. I found this:
http://askubuntu.com/questions/645/how-do-you-reset-a-usb-device-from-the-command-line
the solution referenced there works for me.

When node/serialport locks up the ports, this reset provides an effective work-around that restores communication.

@palamccc
Copy link

I'm facing same issue in beaglebone black (node 0.8.22, node-serialport 1.1.3). Node is not quitting after closing the port.

@voodootikigod
Copy link
Collaborator

Please try this with the latest 1.2.1 release.

@JayBeavers
Copy link
Collaborator

I tried with 1.2.1. I confirm that the hang on close is gone. I also performed a second open in the same process and it opened and closed as expected. I also aborted the port in mid test and sometimes I saw the error surface via the node-serialport error event, but sometimes I also hit a crash.

I used socat on ubuntu 13.04 to test, creating the port pair via:

socat -d -d pty,raw,echo=0 pty,raw,echo=0

and testing by changing the port in the sample above to

/dev/pts/1

@secesh
Copy link

secesh commented Oct 9, 2013

I missed 1.2.1 but confirm 1.2.2 works for me. Process closes/kills without hanging. Subsequent runs receive and transmit data as expected.

Super Awesome! Thanks much!

@voodootikigod
Copy link
Collaborator

Closing ticket.

@bminer
Copy link
Author

bminer commented Nov 4, 2013

Thanks, guys! What a major improvement!

@reconbot
Copy link
Member

reconbot commented Nov 4, 2013

=D 👍

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

No branches or pull requests

10 participants