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

Closed
angelochen960 opened this Issue Oct 29, 2013 · 17 comments

Comments

Projects
None yet

hi,

serialPort.on('error', function(err) {
  console.log(err)
});

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

serialPort.open(function () {
  console.log('open');

    serialPort.on('data', function(data) {
    serialPort.close()
  });

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

the close() will trigger an error:

events.js:72
        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?
Thanks,
Angelo

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

Collaborator

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)

Collaborator

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 () {
sp.write(colfrabuf)
})
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) {
sp.close();

                setTimeout(cb,100,null,null)
             })

ERROR LOG

events.js:72
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.

findDevice
[]
closing from finddevice 2
worker 13748 exit code null
Contributor

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.

Collaborator

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
Contributor

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

.close

sp.close(function(err) {}

events.js:72
throw er; // Unhandled 'error' event
^
Error: EBADF, read

.flush cb -> .close

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

Aborted

// 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

notes

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.

Collaborator

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)

Collaborator

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