Skip to content

Commit 93da1da

Browse files
committed
flush() is only called if the stream is open
This ensures flush() is only called if the stream is open, and logs (but ignores) any exceptions during flush. This should prevent the "Bad file descriptor" error. I see this error a lot on a rak unit, I dont know this is the way but .. you be the judge.
1 parent 7554c03 commit 93da1da

1 file changed

Lines changed: 9 additions & 5 deletions

File tree

meshtastic/serial_interface.py

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -85,10 +85,14 @@ def __repr__(self):
8585

8686
def close(self) -> None:
8787
"""Close a connection to the device"""
88-
if self.stream: # Stream can be null if we were already closed
89-
self.stream.flush() # FIXME: why are there these two flushes with 100ms sleeps? This shouldn't be necessary
90-
time.sleep(0.1)
91-
self.stream.flush()
92-
time.sleep(0.1)
88+
if hasattr(self, "stream") and self.stream and getattr(self.stream, "is_open", False):
89+
try:
90+
self.stream.flush()
91+
time.sleep(0.1)
92+
# FIXME: why are there these two flushes with 100ms sleeps? This shouldn't be necessary
93+
self.stream.flush()
94+
time.sleep(0.1)
95+
except Exception as e:
96+
logger.debug(f"Exception during flush: {e}")
9397
logger.debug("Closing Serial stream")
9498
StreamInterface.close(self)

0 commit comments

Comments
 (0)