da03 commited on
Commit
e7ad005
·
1 Parent(s): 7bc09fd
Files changed (1) hide show
  1. main.py +14 -1
main.py CHANGED
@@ -297,6 +297,7 @@ async def websocket_endpoint(websocket: WebSocket):
297
  last_user_activity_time = time.perf_counter()
298
  timeout_warning_sent = False
299
  timeout_task = None
 
300
 
301
  # Start timing for global FPS calculation
302
  connection_start_time = time.perf_counter()
@@ -384,10 +385,15 @@ async def websocket_endpoint(websocket: WebSocket):
384
 
385
  # Add timeout checking function
386
  async def check_timeout():
387
- nonlocal timeout_warning_sent, timeout_task
388
 
389
  while True:
390
  try:
 
 
 
 
 
391
  current_time = time.perf_counter()
392
  time_since_activity = current_time - last_user_activity_time
393
 
@@ -701,12 +707,19 @@ async def websocket_endpoint(websocket: WebSocket):
701
 
702
  finally:
703
  # Clean up timeout task
 
 
704
  if timeout_task and not timeout_task.done():
 
705
  timeout_task.cancel()
706
  try:
707
  await timeout_task
 
708
  except asyncio.CancelledError:
 
709
  pass
 
 
710
 
711
  # Print final FPS statistics when connection ends
712
  if frame_num >= 0: # Only if we processed at least one frame
 
297
  last_user_activity_time = time.perf_counter()
298
  timeout_warning_sent = False
299
  timeout_task = None
300
+ connection_active = True # Flag to track if connection is still active
301
 
302
  # Start timing for global FPS calculation
303
  connection_start_time = time.perf_counter()
 
385
 
386
  # Add timeout checking function
387
  async def check_timeout():
388
+ nonlocal timeout_warning_sent, timeout_task, connection_active
389
 
390
  while True:
391
  try:
392
+ # Check if WebSocket is still connected and connection is still active
393
+ if not connection_active or websocket.client_state.value >= 2: # CLOSING or CLOSED
394
+ print(f"[{time.perf_counter():.3f}] Connection inactive or WebSocket closed, stopping timeout check for client {client_id}")
395
+ return
396
+
397
  current_time = time.perf_counter()
398
  time_since_activity = current_time - last_user_activity_time
399
 
 
707
 
708
  finally:
709
  # Clean up timeout task
710
+ print(f"[{time.perf_counter():.3f}] Cleaning up connection {client_id}")
711
+ connection_active = False # Signal that connection is being cleaned up
712
  if timeout_task and not timeout_task.done():
713
+ print(f"[{time.perf_counter():.3f}] Cancelling timeout task for client {client_id}")
714
  timeout_task.cancel()
715
  try:
716
  await timeout_task
717
+ print(f"[{time.perf_counter():.3f}] Timeout task cancelled successfully for client {client_id}")
718
  except asyncio.CancelledError:
719
+ print(f"[{time.perf_counter():.3f}] Timeout task cancelled with CancelledError for client {client_id}")
720
  pass
721
+ else:
722
+ print(f"[{time.perf_counter():.3f}] Timeout task already done or doesn't exist for client {client_id}")
723
 
724
  # Print final FPS statistics when connection ends
725
  if frame_num >= 0: # Only if we processed at least one frame