Repository navigation
Description
Problem
A session socket (/sockets/session/{id}) opened on a conversation whose agent is running reads no client frame until the current step ends. session_socket awaits event_service.subscribe_to_events() before starting _inbound_loop. Subscribing builds the initial-state snapshot under the conversation's state lock (_create_initial_state_sync), and LocalConversation.run holds that lock for the whole agent step.
So for the length of a step (40 s in the case below) the socket sends nothing back and reads nothing in. If the client leaves meanwhile, the writer's close wins the race in _inbound_loop, and the person's message frame is dropped: no MessageEvent is saved.
Seen live
During a Projects demo, two messages sent to running worker conversations never arrived. The browser log shows the socket open, 2 frames sent (the first frame plus the message), 0 frames received for about 40 s, and then a close. An identical send to an idle conversation was delivered.
Expected
A message the client sent is saved and runs once the step ends, even if the client leaves first. A frame that arrives together with the close is still handled.
Acceptance criteria
- Inbound frames are read from connect, not only after the snapshot.
- A message already taken is saved even when the socket closes before the state lock frees.
- Tests cover both cases, and they fail without the fix.
OpenHands AI triage
The following comments and acceptance criteria were added by the OpenHands AI agent.
Triage
Verified in the code: session_socket awaits event_service.subscribe_to_events() before starting _inbound_loop, and subscribing builds the initial-state snapshot under the conversation's state lock (_create_initial_state_sync); LocalConversation.run/arun holds that lock across the whole agent step apart from its LLM-call release windows, so a client frame sent during a step is not read until the step ends. Separately, _inbound_loop discards a frame that completes together with the writer's close, and a send_message already in flight is abandoned when the loop returns on close, so the message is lost.
The live Projects-demo incident in this report (two messages sent to running conversations never arrived; an identical send to an idle conversation was delivered) is a real user-facing failure with unrecoverable message loss, so this is prioritized high.
Scope: the agent-server session-socket inbound path only. The fix must keep using the existing _inbound_loop/_ConnectionWriter plumbing and the existing event_service.send_message path, including its current run/rerun semantics for a message sent mid-step — no parallel inbound mechanism.
Non-goals: no changes to the legacy /sockets/events/{id} endpoint; no change to LocalConversation state-lock or agent-step semantics; no change to the session-socket frame protocol, snapshot behavior, or after_seq resume semantics; no TypeScript-client or Canvas changes.
Acceptance Criteria
- While an agent step is running on the conversation (state lock held), a client that opens
/sockets/session/{id}and sends a message frame has that frame read from the socket immediately: an automated test asserts the frame is consumed while the lock is still held, not after the step completes. (This currently fails: reading waits for the snapshot behindsubscribe_to_events.) - When the client closes the socket after sending that frame — the close racing or preceding the lock release — the message is still persisted as a
MessageEventin the conversation's event log, and the conversation processes it (a run picks it up) once the current step ends. - A message frame that completes together with the writer close in
_inbound_loop(both observed in the same wait) is validated and persisted, not discarded. - Existing inbound behavior is preserved: malformed frames still produce an
ErrorFrameand keep the connection open, auth control messages are still ignored, and the session-socket suites intests/agent_server/test_session_socket.pyandtests/agent_server/test_agent_server_wsproto.pypass.
Activity