From a68d39b46d04cc09b849bcb0ebd0a9f069d54dd4 Mon Sep 17 00:00:00 2001 From: Tim O'Farrell Date: Thu, 21 Nov 2024 13:16:36 -0700 Subject: [PATCH] Fire close --- openhands/server/session/manager.py | 28 ++++++++++++++-------------- 1 file changed, 14 insertions(+), 14 deletions(-) diff --git a/openhands/server/session/manager.py b/openhands/server/session/manager.py index 0debb19b0caf..f6b0c045482e 100644 --- a/openhands/server/session/manager.py +++ b/openhands/server/session/manager.py @@ -143,35 +143,35 @@ async def disconnect_from_session(self, connection_id: str): session = self.local_sessions_by_sid.get(sid) if session: if should_continue(): - asyncio.create_task(self._check_and_close_session_later(session)) + asyncio.create_task(self._close_orphaned_session_later(session)) else: - await self._check_and_close_session(session) + await self._close_orphaned_session(session, True) - async def _check_and_close_session_later(self, session: Session): + async def _close_orphaned_session_later(self, session: Session): # Once there have been no connections to a session for a reasonable period, we close it try: await asyncio.sleep(self.config.sandbox.close_delay) finally: # If the sleep was cancelled, we still want to close these - await self._check_and_close_session(session) + await self._close_orphaned_session(session, False) - async def _check_and_close_session(self, session: Session): + async def _close_orphaned_session(self, session: Session, force: bool): # Get local connections - has_connections_for_session = next(( + has_local_connections = next(( True for v in self.local_connection_id_to_session_id.values() if v == session.sid ), False) # If no local connections, get connections through redis - if not has_connections_for_session: - redis_client = self._get_redis_client() - if redis_client: - key = _CONNECTION_KEY.format(sid=session.sid) - connections_for_session = await redis_client.lrange(key, 0, -1) - if not connections_for_session: - await redis_client.delete(key) + redis_connections = None + redis_client = self._get_redis_client() + if redis_client: + key = _CONNECTION_KEY.format(sid=session.sid) + redis_connections = await redis_client.lrange(key, 0, -1) + if not redis_connections: + await redis_client.delete(key) # If no connections, close session - if not has_connections_for_session: + if force or (not has_local_connections and not redis_connections): session.close() self.local_sessions_by_sid.pop(session.sid, None)