Skip to content

[Bug]: Session socket can send an error frame before sync while a run holds the state lock #6745

Open

Description

@enyst

I'm an AI agent helping Engel Nyst (@enyst) with project work.

Is there an existing issue for the same bug?

Bug Description

On /sockets/session/{conversation_id}, the first server frame is no longer always sync. Since #6709 (commit 79844ce) the socket reads client frames from the moment it connects, before subscribe_to_events returns, and subscribe_to_events waits for the conversation's state lock. When a client sends an invalid frame (for example a Message with role: "assistant") while a run holds that lock, _inbound_loop puts the error frame into the writer queue at once, and sync follows only when the step ends. A client that reads sync as the start of the stream (cursor bookkeeping, through_seq) gets a frame before it. The session protocol's own map entry (F07.sync-first) promises sync first.

Expected Behavior

sync is the first frame the server sends on every connection; an error reply to an early inbound frame comes after it. A valid user message sent during a running step is still saved (the point of #6709).

Actual Behavior

With the agent inside a 60 s sleep tool call, an assistant message gets {"type": "error", "code": "AssertionError", "detail": "Only user messages are allowed to be sent to the agent."} at 08:56:28 and sync only at 08:57:15, when the step ends: independent-main-error-before-sync.json,`` and the same order in a second run: main-error-before-sync.json.```` On the commit before #6709 (5c03bde) the same recipe gives sync first: pre6709-sync-first.json.````` The valid message sent during the step is saved on main (independent-main-valid-message-saved.json)`````` and was lost before #6709.

Reproduced 4 times on main (two of them by an independent agent) and never on 5c03bde. Under load the same race also failed the map bullet F07.reject-non-user once without a running step.

Steps to Reproduce

Checkout 5ff99f1b6ca28f7d16870b69f57a31351e59bcd0 (main), uv sync --dev, DEEPSEEK_API_KEY set:

export PATH=$PWD/.agents/skills/verify-agent-server/scripts:$PATH
export AGENT_SERVER_VERIFY_RUN=$(control-agent-server launch --new --print-run)
control-agent-server doctor && control-agent-server llm preset deepseek
CID=$(control-agent-server conversation start --prompt 'Run exactly this terminal command and nothing else: sleep 25 && echo QA_DONE' --print-id)
control-agent-server conversation wait "$CID" --until running --timeout 60
until control-agent-server conversation events "$CID" --kinds ActionEvent --contains 'sleep 25' >/dev/null; do sleep 1; done
control-agent-server ws listen "/sockets/session/$CID" --send '{"role":"assistant","content":[{"type":"text","text":"x"}]}' \
  --until-kind sync --duration 60 --save F07.sync-first/race
control-agent-server state cat evidence/F07.sync-first/race.json --check frames.2.kind eq sync   # fails: frames.2 is the error
control-agent-server stop

The full script used for the evidence: sync_first.sh. Regression: introduced by #6709; launch --new --checkout <worktree at 5c03bde> gives sync first.

Acceptance Criteria

  • On a connection opened while a run holds the state lock, an invalid inbound frame's error reply is sent after sync (and after the replay when after_seq is given), never before it.
  • A valid user message sent on that connection during the step is still saved, including when the client disconnects before the step ends (the fix(agent-server): read session-socket messages while a step runs #6709 tests keep passing).
  • A test in tests/agent_server/test_session_socket.py sends an invalid frame while subscribe is blocked and asserts sync is the first frame received.
  • control-agent-server map run --file F07 --fresh passes (F07.sync-first, F07.reject-non-user).

Installation Method

uv sync in a checkout of main

SDK Version

openhands-agent-server 1.61.0 (main 5ff99f1)

Version Confirmation

  • I have confirmed this bug exists on the LATEST version of OpenHands SDK

Python Version

3.13

Model Name (if applicable)

deepseek/deepseek-flash (only to keep a step running; any model reproduces)

Operating System

Linux

Evidence and Additional Context

Found by the daily verify-agent-server map pass (feature map bullets F07.sync-first, F07.reject-non-user). Canvas (conversation-websocket-context.tsx) shows an error frame whenever it arrives, so the UI impact is small; consumers that key cursor state off sync are the ones at risk.


Generated by Claude Code


OpenHands AI triage

The following comments and acceptance criteria were added by the OpenHands AI agent.

Triage

Confirmed from source and reproduced in the issue evidence: since #6709 (commit 79844ce), session_socket.py starts _inbound_loop (line 408) before subscribe_to_events returns, and subscribe_to_events waits for the conversation's state lock via _create_initial_state_sync. _inbound_loop sends an ErrorFrame immediately when a client frame fails Message validation, while SyncFrame is only sent at line 450 after subscribe returns. An invalid frame arriving while a run holds the lock therefore reaches the client before sync, breaking the F07.sync-first promise in .agents/skills/verify-agent-server/references/feature-map/F07-session-socket.md ("the first server frame is {"type": "sync"}") and the cursor bookkeeping of session-socket clients. The committed evidence (main-error-before-sync.json: error at 08:46:03, sync at 08:46:28 when the step ended) matches this mechanism, and the parent commit 5c03bde shows sync first.

Bounded scope: restore sync-first frame ordering on /sockets/session/{conversation_id} without undoing the #6709 fix. The exact mechanism (defer error replies versus hold inbound) is left to the implementer, provided both behaviors below hold.

Non-goals: the legacy /sockets/events endpoint (sockets.py) keeps its current behavior; error-frame contents, validation semantics, and frame schemas are unchanged; no TypeScript client or Canvas changes; no API-contract changes beyond frame ordering.

Acceptance Criteria

  • On a connection to /sockets/session/{conversation_id} opened while a run holds the conversation's state lock, an inbound frame that fails Message validation (for example role: "assistant") produces an error frame after the sync frame; with after_seq set, the error frame also follows the replayed durable frames. First frame received on that connection is sync.
  • A valid user Message sent on such a connection during the running step is still saved and run once the lock frees, including when the client disconnects before the step ends; the existing fix(agent-server): read session-socket messages while a step runs #6709 tests in tests/agent_server/test_session_socket.py (test_a_message_sent_while_a_step_runs_is_saved_after_the_client_leaves, test_a_frame_that_arrives_with_the_close_is_still_saved) still pass.
  • tests/agent_server/test_session_socket.py contains a new test that opens the socket while subscribe_to_events is blocked by the state lock, sends an invalid frame, and asserts the first frame received is sync (error frame follows).
  • control-agent-server map run --file F07 --fresh passes F07.sync-first and F07.reject-non-user.

Activity

  1. added
    bug
    on Nov 11, 2026
  2. added
    priority:medium
    ready-for-dev
    on Nov 11, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugpriority:mediumready-for-dev

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions