Skip to content

Commit

Permalink
Feat - browser client can now close sessions. (#6088)
Browse files Browse the repository at this point in the history
  • Loading branch information
tofarr authored Jan 6, 2025
1 parent cebd391 commit 9515ac5
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 5 deletions.
2 changes: 1 addition & 1 deletion openhands/server/routes/manage_conversations.py
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@ async def delete_conversation(
return False
is_running = await session_manager.is_agent_loop_running(conversation_id)
if is_running:
return False
await session_manager.close_session(conversation_id)
await conversation_store.delete_metadata(conversation_id)
return True

Expand Down
22 changes: 19 additions & 3 deletions openhands/server/session/manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,10 @@ async def _process_message(self, message: dict):
flag = self._has_remote_connections_flags.get(sid)
if flag:
flag.set()
elif message_type == 'close_session':
sid = data['sid']
if sid in self._local_agent_loops_by_sid:
await self._on_close_session(sid)
elif message_type == 'session_closing':
# Session closing event - We only get this in the event of graceful shutdown,
# which can't be guaranteed - nodes can simply vanish unexpectedly!
Expand Down Expand Up @@ -419,7 +423,7 @@ async def disconnect_from_session(self, connection_id: str):
if should_continue():
asyncio.create_task(self._cleanup_session_later(sid))
else:
await self._close_session(sid)
await self._on_close_session(sid)

async def _cleanup_session_later(self, sid: str):
# Once there have been no connections to a session for a reasonable period, we close it
Expand Down Expand Up @@ -451,10 +455,22 @@ async def _cleanup_session(self, sid: str) -> bool:
json.dumps({'sid': sid, 'message_type': 'session_closing'}),
)

await self._close_session(sid)
await self._on_close_session(sid)
return True

async def _close_session(self, sid: str):
async def close_session(self, sid: str):
session = self._local_agent_loops_by_sid.get(sid)
if session:
await self._on_close_session(sid)

redis_client = self._get_redis_client()
if redis_client:
await redis_client.publish(
'oh_event',
json.dumps({'sid': sid, 'message_type': 'close_session'}),
)

async def _on_close_session(self, sid: str):
logger.info(f'_close_session:{sid}')

# Clear up local variables
Expand Down
2 changes: 1 addition & 1 deletion tests/unit/test_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -286,7 +286,7 @@ async def test_cleanup_session_connections():
}
)

await session_manager._close_session('session1')
await session_manager._on_close_session('session1')

remaining_connections = session_manager.local_connection_id_to_session_id
assert 'conn1' not in remaining_connections
Expand Down

0 comments on commit 9515ac5

Please sign in to comment.