If a port is closed while data is pending, EBADF error is generated #241

angelochen960 opened this Issue Oct 29, 2013 · 17 comments


None yet


serialPort.on('error', function(err) {

serialPort.on('close', function (err) {
  console.log('on close', err)

serialPort.open(function () {

    serialPort.on('data', function(data) {

  serialPort.write([ 4, 48, 50, 5 ], function(err, results) {

the close() will trigger an error:

        throw er; // Unhandled 'error' event
Error: EBADF, read

in another app, similar code, error is:

{ [Error: EBADF, read] errno: 9, code: 'EBADF' }

any idea what I'm missing here?

putting the serialPort.close() in a setTimer function solve the problem for now.


JayBeavers commented Oct 29, 2013

You've hit on a race condition, probably data still coming in after the close has occurred. Reopening, we should investigate handling these races better.

@JayBeavers JayBeavers reopened this Oct 29, 2013

what close can do? for now I put the close in a setTimer to avoid that error. but if I reuse the same serialPort object, it will not read anything from the device, I have to do a :
serialPort = new SerialPort(portName, portOptions)


JayBeavers commented Nov 2, 2013

You've implemented a good workaround. I'm keeping the issue open so we can fix the underlying code so you don't need a workaround like this.

For example we could modify the code so that once a port is closed any pending writes or reads are ignored. I suspect in your case that you're writing four bytes, < than 4 get written, you receive ondata and close the port, the remaining bytes try to write, then a EBADF (port is closed) error gets generated.

thanks. the serial device will start sending data over to pc if the four bytes are received by the device, is there a way to know if the device has finished sending in the serialPort.on('data', function(cb) {}), the reason is, if I know device has finished sending, I can send another command to device, this will be more precise in controlling the device.

robinxc commented Nov 5, 2013

I found this bug .
what to do?
v1.1VERSION no this bug .
new version the bug & problem very much

robinxc commented Nov 5, 2013

sp.on("open", function () {
var readData = new Buffer(0)
sp.on("data", function (data) {
if (readData.length == 0) {
readData = data;
} else {
readData = Buffer.concat([readData, data]);
console.log('on--data back ::'+readData.toString('hex').toUpperCase()+'---'+readData.length);
if (readData.length === colregnum) {



throw er; // Unhandled 'error' event
Error: EBADF, read

SevenW commented Nov 23, 2013

I experience the same error, when I try to close an already disconnected SerialUSB port. There is no writing going on to the port, but data may be coming in.

However, more often I experience an unintended error free? exit of the program.

closing from finddevice 2
worker 13748 exit code null

programmarchy commented Nov 25, 2013

Ran into this issue also. I'm also thinking this occurs when attempting a read (as opposed to a write), due to the error (EBADF, read).

Could this be related to serialport_poller, trying to read after the port has been closed?

I tried to make _read bail if it self.closing = true, but no luck. Still investigating possible solutions. I haven't quite wrapped my head around how serialport_poller works yet.


reconbot commented Nov 25, 2013

@SevenW which os?

SevenW commented Nov 26, 2013

@ reconbot: Linux

Linux odroidxu 3.4.67 #1 SMP PREEMPT Sun Nov 17 17:03:03 CET 2013 armv7l armv7l armv7l GNU/Linux

ffissore commented Mar 10, 2014

I'm running into this issue as well. I'm stress testing serialport at 115200 baud with an arduino leonardo loaded with a modified asciitable example sketch (reprints the asciitable over and over)

Linux smilzo 3.11.0-18-generic #32~precise1-Ubuntu SMP Thu Feb 20 17:52:10 UTC 2014 x86_64 x86_64 x86_64 GNU/Linux

I am seeing the .close issues with a USB sierra modem /dev/ttyACM0 - when I try to close inside the data cbh.

sp.on('data', function(data) {

I tried the following .close methods


sp.close(function(err) {}

throw er; // Unhandled 'error' event
Error: EBADF, read

.flush cb -> .close

sp.flush(function(err) {
            sp.close(function(err) {


// No idea what this means

.flush cb -> timeout cb -> .close

sp.flush(function(err) {
            setTimeout(function() {
                sp.close(function(err) {
            }, 10);

//I don't see any errors


I hope this helps someone
Please give me feedback as I am new to node an may have made a mistake

rla commented Jul 27, 2014

In receive handler, setImmediate instead of setTimeout works for me (no need for delay) when closing the port.


voodootikigod commented Sep 22, 2014

This should be fixed in the system itself at this point, we revised and cleaned up the unhook process, can you verify/confirm and if still an issue, please reopen ticket or submit new.

i have an issue that
throw er; // Unhandled 'error' event

Error: Port is opening
at SerialPort.open (/home/polmon/Desktop/node_modules/serialport/lib/serialport.js:160:24)
at doNTCallback0 (node.js:428:9)


reconbot commented Nov 27, 2016

@pagalasoujanya you're port is already being opened, probably by the autoOpen option, please create a new issue if you keep having trouble

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment