Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion openhands-agent-server/AGENTS.md
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ feature map (`control-agent-server map coverage`).
- `ConversationState` uses a synchronous `FIFOLock`. In async agent-server code, never do `with conversation._state` directly on the event loop when the conversation may be running.
- WebSocket reconnects call `EventService.subscribe_to_events()` immediately; if initial state snapshot creation blocks on the state lock in async context, the whole FastAPI event loop can stop serving `/ready` and similar probes.
- The same rule applies to metadata updates in `ConversationService.update_conversation()`: keep the locked mutation/snapshot semantics, but move the synchronous lock wait into a worker thread first.
- In async routes/services, move state-lock acquisition into `run_in_executor(...)` (or another worker-thread boundary) before awaiting network I/O.
- In async routes/services, move state-lock acquisition into `EventService.run_state_op(...)` before awaiting network I/O. It runs on the conversation's own executor: a run holds the lock for a whole tool step, and waiters on asyncio's default executor (`run_in_executor(None, ...)`, `asyncio.to_thread`) take the threads that event reads of every conversation need.


## REST API compatibility & deprecation policy
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import asyncio
import contextvars
import importlib
import json
import logging
Expand All @@ -7,6 +8,7 @@
from concurrent.futures import ThreadPoolExecutor
from contextlib import asynccontextmanager, suppress
from dataclasses import dataclass, field
from functools import partial
from pathlib import Path
from typing import TYPE_CHECKING, Any, cast
from uuid import UUID, uuid4
Expand Down Expand Up @@ -2000,7 +2002,6 @@ async def update_conversation(
if event_service is None:
return False

loop = asyncio.get_running_loop()
state = await event_service.get_state()
update: dict[str, Any] = {"updated_at": utc_now()}
if request.title is not None:
Expand All @@ -2015,8 +2016,8 @@ async def update_conversation(
if request.tags is not None:
# Keep the persisted ConversationState update under the state lock so
# autosave and state-change callbacks observe a consistent mutation.
state = await loop.run_in_executor(
None, _update_state_tags_sync, state, request.tags
state = await event_service.run_state_op(
_update_state_tags_sync, state, request.tags
)
if budget_updated:
# base_state.json owns the budget; the event service is loaded
Expand All @@ -2030,8 +2031,8 @@ async def update_conversation(
# Notify conversation webhooks about the updated conversation. Compose the
# full-state snapshot under the state lock, but do the synchronous wait in a
# worker thread so metadata updates cannot block the FastAPI event loop.
conversation_info = await loop.run_in_executor(
None, _compose_webhook_conversation_info_sync, event_service.stored, state
conversation_info = await event_service.run_state_op(
_compose_webhook_conversation_info_sync, event_service.stored, state
)
await self._notify_conversation_webhooks(conversation_info)

Expand Down Expand Up @@ -2146,12 +2147,15 @@ async def fork_conversation(
raise ValueError(f"Conversation with id {fork_id} already exists")

# fork() deep-copies events and state into ``fork_dir``.
fork_conv = await asyncio.to_thread(
source_conversation.fork,
conversation_id=fork_conv_id,
tags=tags,
reset_metrics=reset_metrics,
from_event_id=from_event_id,
fork_conv = await source_service.run_state_op(
partial(
contextvars.copy_context().run,
source_conversation.fork,
conversation_id=fork_conv_id,
tags=tags,
reset_metrics=reset_metrics,
from_event_id=from_event_id,
)
)
# Extract the persisted data, then discard the temporary conversation.
fork_agent = cast(AgentBase, fork_conv.agent)
Expand Down
Loading
Loading