175175CMD_MIN = CMD_STAT
176176CMD_MAX = CMD_READLINE
177177
178+ # CMD names for diagnostic logging
179+ _CMD_NAMES = {
180+ CMD_STAT : 'STAT' ,
181+ CMD_LISTDIR : 'LISTDIR' ,
182+ CMD_OPEN : 'OPEN' ,
183+ CMD_CLOSE : 'CLOSE' ,
184+ CMD_READ : 'READ' ,
185+ CMD_WRITE : 'WRITE' ,
186+ CMD_MKDIR : 'MKDIR' ,
187+ CMD_REMOVE : 'REMOVE' ,
188+ CMD_RENAME : 'RENAME' ,
189+ CMD_SEEK : 'SEEK' ,
190+ CMD_READLINE : 'READLINE' ,
191+ }
192+
178193# Prebuilt bytes for hot path
179194_ESCAPE_BYTE = bytes ([ESCAPE ])
180195
@@ -361,10 +376,14 @@ class _VFSReader:
361376 read from underlying connection if buffer is exhausted.
362377 """
363378
364- def __init__ (self , data , offset , underlying_conn ):
379+ # Diagnostic timeout - set to detect hangs (None = no timeout)
380+ DIAG_TIMEOUT = 30 # seconds
381+
382+ def __init__ (self , data , offset , underlying_conn , log = None ):
365383 self ._data = data
366384 self ._offset = offset
367385 self ._conn = underlying_conn
386+ self ._log = log
368387
369388 def read_bytes (self , n , timeout = None ):
370389 """Read n bytes from buffer + underlying connection"""
@@ -376,7 +395,11 @@ def read_bytes(self, n, timeout=None):
376395 self ._offset += chunk
377396 n -= chunk
378397 if n > 0 :
379- result .extend (self ._conn .read_bytes (n , timeout ))
398+ # Use diagnostic timeout if none specified
399+ actual_timeout = timeout if timeout is not None else self .DIAG_TIMEOUT
400+ if self ._log :
401+ self ._log .debug ("VFS read_bytes: waiting for %d bytes" , n )
402+ result .extend (self ._conn .read_bytes (n , actual_timeout ))
380403 return bytes (result )
381404
382405 def write (self , data ):
@@ -1057,13 +1080,25 @@ def process(self, data):
10571080 # Send ACK
10581081 self ._conn .write (_ESCAPE_BYTE )
10591082
1083+ # Diagnostic logging
1084+ cmd_name = _CMD_NAMES .get (self ._cmd , str (self ._cmd ))
1085+ self ._log .debug (
1086+ "VFS dispatch: CMD=%s MID=%d START" , cmd_name , self ._mid )
1087+
10601088 # Read params and dispatch using _VFSReader
1061- reader = _VFSReader (self ._buf , 0 , self ._conn )
1089+ reader = _VFSReader (self ._buf , 0 , self ._conn , log = self . _log )
10621090 orig_conn = handler ._conn
10631091 handler ._conn = reader
10641092 self ._dispatching = True
10651093 try :
10661094 handler .dispatch (self ._cmd )
1095+ self ._log .debug (
1096+ "VFS dispatch: CMD=%s MID=%d DONE" , cmd_name , self ._mid )
1097+ except Exception as e :
1098+ self ._log .warning (
1099+ "VFS dispatch: CMD=%s MID=%d FAILED: %s" ,
1100+ cmd_name , self ._mid , e )
1101+ raise
10671102 finally :
10681103 self ._dispatching = False
10691104 handler ._conn = orig_conn
0 commit comments