You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
In some cases, calling write() while the SerialPort is being closed and a concurrent write is in progress can result in an uncaught exception. I'm not exactly sure why this is happening, but here's my hunch: the 2nd write is queued until the 1st write completes, and when the 2nd write is dequeued after the SerialPort has already closed, we have an exception.
TypeError: There's no write queue for that file descriptor (after write)!
at TypeError (native)
This error is apparently thrown in EIO_AfterWrite. Even if the SerialPort has an "error" event handler registered and even if the call to write(buf, callback) has a callback specified, this error is still thrown as an uncaught exception.
Code to reproduce:
varSerialPort=require("serialport").SerialPort;varport=newSerialPort("/dev/ttyUSB0");port.on("error",function(e){console.log("error event",e.stack);});port.on("open",function(){// Close the SerialPort after a short period of timesetTimeout(function(){port.close();},10);// Start writing datawrite(30);});functionwrite(timesLeft){if(timesLeft<=0)return;console.log("write",timesLeft);try{if(port.isOpen()){port.write(newBuffer([1,2,3,4,5]),function(err){console.log("write callback",err ? err : "no error");});}}catch(e){console.log("catch error",e.stack);}/* Note that we didn't wait for the write to complete before we call `write(timesLeft)` again. Chances are that 1 ms. isn't going to be long enough for the write to finish, causing the next write to be queued */setTimeout(write.bind(null,timesLeft-1),1);}
Sample output from the above program looks like this:
write 30
write 29
write callback no error
write callback [Error: Error Bad file descriptor calling write(...)]
undefined:0
TypeError: There's no write queue for that file descriptor (after write)!
at TypeError (native)
Interestingly, undefined:0 is printed from somewhere? It doesn't appear to be a call to console.log or console.error either... Then, of course, the TypeError is thrown and stack trace is printed.
Note that the exception from EIO_AfterWrite does not occur if you do not pass a callback to the write() routine. That is because of #638; the program will instead crash beforehand due to an Unhandled 'error' event.
Also note that the exception from EIO_AfterWrite does not occur if the timing is changed. This bug might not occur on all systems, and the behavior might change if the baud rate is changed. For example, if the delay to close the serial port is increased to 100 ms. and the delay between queued writes is increased to 10 ms, this bug occurs about 50% of the time.
Anyway, I'd be happy to provide more information about this bug if needed. The fix for this isn't very obvious to me because I'm not familiar with Node addons or libuv, but I'd be happy to help as much as possible.
The text was updated successfully, but these errors were encountered:
In some cases, calling
write()
while the SerialPort is being closed and a concurrent write is in progress can result in an uncaught exception. I'm not exactly sure why this is happening, but here's my hunch: the 2nd write is queued until the 1st write completes, and when the 2nd write is dequeued after the SerialPort has already closed, we have an exception.This error is apparently thrown in
EIO_AfterWrite
. Even if the SerialPort has an "error" event handler registered and even if the call towrite(buf, callback)
has acallback
specified, this error is still thrown as an uncaught exception.Code to reproduce:
Sample output from the above program looks like this:
Interestingly,
undefined:0
is printed from somewhere? It doesn't appear to be a call toconsole.log
orconsole.error
either... Then, of course, the TypeError is thrown and stack trace is printed.Note that the exception from
EIO_AfterWrite
does not occur if you do not pass a callback to thewrite()
routine. That is because of #638; the program will instead crash beforehand due to an Unhandled 'error' event.Also note that the exception from
EIO_AfterWrite
does not occur if the timing is changed. This bug might not occur on all systems, and the behavior might change if the baud rate is changed. For example, if the delay to close the serial port is increased to 100 ms. and the delay between queued writes is increased to 10 ms, this bug occurs about 50% of the time.Anyway, I'd be happy to provide more information about this bug if needed. The fix for this isn't very obvious to me because I'm not familiar with Node addons or libuv, but I'd be happy to help as much as possible.
The text was updated successfully, but these errors were encountered: