Skip to content

Commit 6e1baf2

Browse files
authored
Make sure we run open/close in executor (#82)
1 parent 44d4701 commit 6e1baf2

File tree

2 files changed

+12
-8
lines changed

2 files changed

+12
-8
lines changed

serial_asyncio/__init__.py

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -379,7 +379,7 @@ def _close(self, exc=None):
379379
self._remove_reader()
380380
if self._flushed():
381381
self._remove_writer()
382-
self._loop.call_soon(self._call_connection_lost, exc)
382+
self._loop.create_task(self._call_connection_lost(exc))
383383

384384
def _abort(self, exc):
385385
"""Close the transport immediately.
@@ -393,9 +393,9 @@ def _abort(self, exc):
393393
self._closing = True
394394
self._remove_reader()
395395
self._remove_writer() # Pending buffered data will not be written
396-
self._loop.call_soon(self._call_connection_lost, exc)
396+
self._loop.create_task(self._call_connection_lost(exc))
397397

398-
def _call_connection_lost(self, exc):
398+
async def _call_connection_lost(self, exc):
399399
"""Close the connection.
400400
401401
Informs the protocol through connection_lost() and clears
@@ -405,16 +405,17 @@ def _call_connection_lost(self, exc):
405405
assert not self._has_writer
406406
assert not self._has_reader
407407
try:
408-
self._serial.flush()
408+
await self._loop.run_in_executor(None, self._serial.flush)
409409
except (serial.SerialException if os.name == "nt" else termios.error):
410410
# ignore serial errors which may happen if the serial device was
411411
# hot-unplugged.
412412
pass
413+
413414
try:
414415
self._protocol.connection_lost(exc)
415416
finally:
416417
self._write_buffer.clear()
417-
self._serial.close()
418+
await self._loop.run_in_executor(None, self._serial.close)
418419
self._serial = None
419420
self._protocol = None
420421
self._loop = None
@@ -445,7 +446,7 @@ async def create_serial_connection(loop, protocol_factory, *args, **kwargs):
445446
446447
Any additional arguments will be forwarded to the Serial constructor.
447448
"""
448-
serial_instance = serial.serial_for_url(*args, **kwargs)
449+
serial_instance = await loop.run_in_executor(None, serial.serial_for_url, *args, **kwargs)
449450
transport, protocol = await connection_for_serial(loop, protocol_factory, serial_instance)
450451
return transport, protocol
451452

test/test_asyncio.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ def test_asyncio(self):
4343
TEXT = b'Hello, World!\n'
4444
received = []
4545
actions = []
46+
done = asyncio.Event()
4647

4748
class Input(asyncio.Protocol):
4849

@@ -74,7 +75,7 @@ def data_received(self, data):
7475

7576
def connection_lost(self, exc):
7677
actions.append('close')
77-
self._transport.loop.stop()
78+
done.set()
7879

7980
def pause_writing(self):
8081
actions.append('pause')
@@ -90,7 +91,9 @@ def resume_writing(self):
9091

9192
client = serial_asyncio.create_serial_connection(self.loop, Output, PORT)
9293
self.loop.run_until_complete(client)
93-
self.loop.run_forever()
94+
self.loop.run_until_complete(done.wait())
95+
pending = asyncio.all_tasks(self.loop)
96+
self.loop.run_until_complete(asyncio.gather(*pending))
9497
self.assertEqual(b''.join(received), TEXT)
9598
self.assertEqual(actions, ['open', 'close'])
9699

0 commit comments

Comments
 (0)