Nirav Madhani commited on
Commit
04d93df
·
1 Parent(s): 52afbdf

Close connections properly

Browse files
Files changed (1) hide show
  1. handler.py +16 -6
handler.py CHANGED
@@ -97,14 +97,24 @@ class AudioLoop:
97
  await self.startup(tools)
98
 
99
  async with asyncio.TaskGroup() as tg:
100
- tg.create_task(self.send_realtime())
101
- tg.create_task(self.receive_audio())
102
-
103
- # Keep running until canceled
104
- await asyncio.Future()
105
 
106
  except asyncio.CancelledError:
107
- pass
 
 
 
 
 
 
 
 
 
 
 
 
108
  except Exception as e:
109
  traceback.print_exc()
110
  raise
 
97
  await self.startup(tools)
98
 
99
  async with asyncio.TaskGroup() as tg:
100
+ send_task = tg.create_task(self.send_realtime())
101
+ receive_task = tg.create_task(self.receive_audio())
102
+ await asyncio.Future() # Keep running until canceled
 
 
103
 
104
  except asyncio.CancelledError:
105
+ print("[AudioLoop] Received cancel signal, cleaning up...")
106
+ # Cancel our tasks if they're still running
107
+ if 'send_task' in locals():
108
+ send_task.cancel()
109
+ if 'receive_task' in locals():
110
+ receive_task.cancel()
111
+ # Close Gemini connection if we have one
112
+ if self.ws and not self.ws.closed:
113
+ try:
114
+ await self.ws.close()
115
+ except Exception as e:
116
+ print(f"[AudioLoop] Error closing Gemini connection: {e}")
117
+ print("[AudioLoop] Cleanup complete")
118
  except Exception as e:
119
  traceback.print_exc()
120
  raise