Joffrey Thomas commited on
Commit
a8babaa
·
1 Parent(s): 5cc830b

increase sessions cleanup

Browse files
Files changed (1) hide show
  1. app.py +35 -5
app.py CHANGED
@@ -136,6 +136,8 @@ class UserSession:
136
  self.last_audio_time = None
137
  self._start_lock = threading.Lock()
138
  self._task = None # Track the async task
 
 
139
 
140
  @property
141
  def audio_queue(self):
@@ -297,11 +299,15 @@ async def send_silence(ws, duration=2.0):
297
 
298
  async def websocket_handler(session):
299
  """Connect to WebSocket and handle audio streaming + transcription."""
 
300
  try:
301
  # Add connection timeout to prevent hanging
302
  async with asyncio.timeout(10): # 10 second connection timeout
303
  ws = await websockets.connect(ws_url)
304
 
 
 
 
305
  async with ws:
306
  await asyncio.wait_for(ws.recv(), timeout=5)
307
  await ws.send(json.dumps({"type": "session.update", "model": model}))
@@ -393,11 +399,15 @@ async def websocket_handler(session):
393
  session.status_message = "error"
394
  finally:
395
  session.is_running = False
396
- # Remove from active sessions
397
- with _sessions_lock:
398
- _active_sessions.pop(session.session_id, None)
399
- active_count = len(_active_sessions)
400
- print(f"Session {session.session_id[:8]} ended. Active sessions: {active_count}")
 
 
 
 
401
 
402
 
403
  def start_websocket(session):
@@ -481,6 +491,16 @@ def stop_session(session_id):
481
  if session.is_running:
482
  session.is_running = False
483
  session.last_audio_time = None
 
 
 
 
 
 
 
 
 
 
484
 
485
  # Cancel the running task if any
486
  if session._task is not None:
@@ -507,6 +527,16 @@ def clear_history(session_id):
507
  session = ensure_session(session_id)
508
  session.is_running = False
509
  session.last_audio_time = None
 
 
 
 
 
 
 
 
 
 
510
 
511
  # Cancel the running task if any
512
  if session._task is not None:
 
136
  self.last_audio_time = None
137
  self._start_lock = threading.Lock()
138
  self._task = None # Track the async task
139
+ self._websocket = None # Store websocket for forced closure
140
+ self._stopped_by_user = False # Track if user explicitly stopped
141
 
142
  @property
143
  def audio_queue(self):
 
299
 
300
  async def websocket_handler(session):
301
  """Connect to WebSocket and handle audio streaming + transcription."""
302
+ ws = None
303
  try:
304
  # Add connection timeout to prevent hanging
305
  async with asyncio.timeout(10): # 10 second connection timeout
306
  ws = await websockets.connect(ws_url)
307
 
308
+ # Store websocket reference so it can be closed externally
309
+ session._websocket = ws
310
+
311
  async with ws:
312
  await asyncio.wait_for(ws.recv(), timeout=5)
313
  await ws.send(json.dumps({"type": "session.update", "model": model}))
 
399
  session.status_message = "error"
400
  finally:
401
  session.is_running = False
402
+ session._websocket = None
403
+
404
+ # Only remove and log if not already handled by stop_session
405
+ if not session._stopped_by_user:
406
+ with _sessions_lock:
407
+ removed = _active_sessions.pop(session.session_id, None)
408
+ active_count = len(_active_sessions)
409
+ if removed:
410
+ print(f"Session {session.session_id[:8]} ended. Active sessions: {active_count}")
411
 
412
 
413
  def start_websocket(session):
 
491
  if session.is_running:
492
  session.is_running = False
493
  session.last_audio_time = None
494
+ session._stopped_by_user = True # Mark as user-stopped to avoid duplicate logging
495
+
496
+ # Close the websocket immediately to force cleanup
497
+ if session._websocket is not None:
498
+ loop = get_event_loop()
499
+ try:
500
+ asyncio.run_coroutine_threadsafe(session._websocket.close(), loop)
501
+ except Exception:
502
+ pass # Ignore errors during close
503
+ session._websocket = None
504
 
505
  # Cancel the running task if any
506
  if session._task is not None:
 
527
  session = ensure_session(session_id)
528
  session.is_running = False
529
  session.last_audio_time = None
530
+ session._stopped_by_user = True # Mark as user-stopped
531
+
532
+ # Close the websocket immediately
533
+ if session._websocket is not None:
534
+ loop = get_event_loop()
535
+ try:
536
+ asyncio.run_coroutine_threadsafe(session._websocket.close(), loop)
537
+ except Exception:
538
+ pass
539
+ session._websocket = None
540
 
541
  # Cancel the running task if any
542
  if session._task is not None: