diff --git a/serialx/platforms/serial_rfc2217/__init__.py b/serialx/platforms/serial_rfc2217/__init__.py index ea07e16..d9f7eb9 100644 --- a/serialx/platforms/serial_rfc2217/__init__.py +++ b/serialx/platforms/serial_rfc2217/__init__.py @@ -665,7 +665,12 @@ def _get_modem_pins(self) -> ModemPins: return self._engine.get_modem_pins() def _flush(self) -> None: - """Flush write buffers (no-op, TCP handles buffering).""" + """Wait for the server to acknowledge all preceding writes.""" + if not self._engine.negotiated: + return + + # RFC2217 has no flush. Instead, we "flush" the pipe with a req/rsp sequence. + self._send_and_wait(SetBaudrateCmd(baudrate=self._baudrate)) class _RFC2217ProxyProtocol(asyncio.Protocol): @@ -1007,7 +1012,13 @@ def abort(self) -> None: self._tcp_connection_lost(None) async def flush(self) -> None: - """Flush write buffers (no-op, TCP transport handles buffering).""" + """Wait for the server to acknowledge all preceding writes.""" + assert self._serial is not None + if not self._serial._engine.negotiated: + return + + # RFC2217 has no flush. Instead, we "flush" the pipe with a req/rsp sequence. + await self._send_and_wait(SetBaudrateCmd(baudrate=self._serial._baudrate)) def get_write_buffer_size(self) -> int: """Get the number of bytes currently in the write buffer.""" diff --git a/tests/test_sync_transports.py b/tests/test_sync_transports.py index 641c7c8..27e04ad 100644 --- a/tests/test_sync_transports.py +++ b/tests/test_sync_transports.py @@ -869,6 +869,11 @@ def test_fast_open_close(serial_pair: SerialPair) -> None: with Serial.from_url(serial_pair.left, baudrate=115200) as left: with Serial.from_url(serial_pair.right, baudrate=115200) as right: right.write(message) + right.flush() + + # Some backends (notably complex chained RFC2217) lose data on close, making + # this test flaky without a tiny delay + time.sleep(0.01) assert left.readexactly(len(message)) == message