Skip to content

Commit

Permalink
Windows: handle serial errors when the device is removed (#38)
Browse files Browse the repository at this point in the history
* Windows: detect serial port error in _poll_read()

On windows, _poll_read() is typically the first function to throw an exception when the serial port disappears.
The exception is currently lost on the event loop, this catches it and escalates to a fatal_error

* Windows: when closing, ignore errors in flush
  • Loading branch information
andrewleech authored and rob-smallshire committed Jul 27, 2018
1 parent df2e42f commit cf256c6
Showing 1 changed file with 11 additions and 11 deletions.
22 changes: 11 additions & 11 deletions serial_asyncio/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -275,9 +275,12 @@ def _write_ready(self):
if os.name == "nt":
def _poll_read(self):
if self._has_reader:
if self.serial.in_waiting:
self._loop.call_soon(self._read_ready)
self._loop.call_later(self._poll_wait_time, self._poll_read)
try:
if self.serial.in_waiting:
self._loop.call_soon(self._read_ready)
self._loop.call_later(self._poll_wait_time, self._poll_read)
except serial.SerialException as exc:
self._fatal_error(exc, 'Fatal write error on serial transport')

def _ensure_reader(self):
if (not self._has_reader) and (not self._closing):
Expand Down Expand Up @@ -389,15 +392,12 @@ def _call_connection_lost(self, exc):
assert self._closing
assert not self._has_writer
assert not self._has_reader
if os.name == "nt":
try:
self._serial.flush()
else:
try:
self._serial.flush()
except termios.error:
# ignore termios errors which may happen if the serial device was
# hot-unplugged.
pass
except (serial.SerialException if os.name == "nt" else termios.error):
# ignore serial errors which may happen if the serial device was
# hot-unplugged.
pass
try:
self._protocol.connection_lost(exc)
finally:
Expand Down

0 comments on commit cf256c6

Please sign in to comment.