Selaa lähdekoodia

Fix NoneType context manager error in connection manager

Removed unsafe `self.lock = None` in close() methods that caused
"'NoneType' object does not support the context manager protocol"
errors when status queries ran after connection close. Added
connection validity check in get_status_response() for defense.

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
tuanchris 1 viikko sitten
vanhempi
sitoutus
d103a6fb90
1 muutettua tiedostoa jossa 5 lisäystä ja 4 poistoa
  1. 5 4
      modules/connection/connection_manager.py

+ 5 - 4
modules/connection/connection_manager.py

@@ -116,8 +116,6 @@ class SerialConnection(BaseConnection):
         with self.lock:
             if self.ser.is_open:
                 self.ser.close()
-        # Release the lock resources
-        self.lock = None
 
 ###############################################################################
 # WebSocket Connection Implementation
@@ -181,8 +179,7 @@ class WebSocketConnection(BaseConnection):
         with self.lock:
             if self.ws:
                 self.ws.close()
-        # Release the lock resources
-        self.lock = None
+                self.ws = None
                 
 def list_serial_ports():
     """Return a list of available serial ports."""
@@ -360,6 +357,10 @@ def get_status_response() -> str:
     """
     Send a status query ('?') and return the response if available.
     """
+    if state.conn is None or not state.conn.is_connected():
+        logger.warning("Cannot get status response: no active connection")
+        return False
+
     while True:
         try:
             state.conn.send('?')