Skip to content

fix: events search API blocks during agent step (FIFOLock contention) - #2296

Merged
neubig merged 2 commits into
mainfrom
fix/search-events-lock-contention
Mar 5, 2026
Merged

neubig merged 2 commits into
mainfrom
fix/search-events-lock-contention

Conversation

@rbren

@rbren rbren commented Mar 4, 2026 •

Copy link
Copy Markdown
Member

🧑‍🦱 Human description: the events search endpoint hangs while running a command. Minimal repro is to ask the agent to sleep 30, then refresh the page

Problem

GET /api/conversations/{id}/events/search blocks for the entire duration of an agent step (LLM call + tool execution) because the read path and the run loop contend on the same FIFOLock inside ConversationState.

When the agent runs a long command (e.g. sleep 30), the search API is unresponsive for 30+ seconds.

Root Cause

LocalConversation.run() holds self._state (a FIFOLock) for each iteration:

# local_conversation.py – run()
while True:
    with self._state:          # acquires FIFOLock
        ...
        self.agent.step(...)   # LLM call + tool execution (can take minutes)

EventService._search_events_sync() acquires the same lock to iterate events:

# event_service.py – _search_events_sync()
with self._conversation._state as state:   # blocks here
    for event in state.events:
        ...

Both run in thread executors (run_in_executor), so they contend on the same FIFOLock. The search thread blocks in FIFOLock.acquire() until agent.step() finishes and releases the lock.

Also affects _count_events_sync, _get_event_sync, and any other code path that acquires ConversationState as a context manager.

Affected Endpoints

  • GET .../events/search
  • GET .../events/count
  • GET .../events/{event_id}
  • GET .../events (batch get)

Reproduction

The new test TestSearchEventsBlockedByRunLoop::test_search_events_not_blocked_by_state_lock reproduces this directly:

  1. Sets up an EventService with a conversation whose _state uses a real FIFOLock
  2. Holds the lock in a background thread for 2 seconds (simulating run() → agent.step())
  3. Calls search_events() concurrently
  4. Asserts it returns in <0.5s

The test currently fails — search_events takes the full 2s because it's blocked by the lock:

AssertionError: search_events took 2.001s, but should return in <0.5s
even while the run loop holds the state lock for 2.0s.
The read path is blocked by the write lock (see HANG_REPRO.md).

Changes

  • Added failing test in tests/agent_server/test_event_service.py that reproduces the lock contention.

Agent Server images for this PR

• GHCR package: https://github.com/OpenHands/agent-sdk/pkgs/container/agent-server

Variants & Base Images

Variant Architectures Base Image Docs / Tags
java amd64, arm64 eclipse-temurin:17-jdk Link
python amd64, arm64 nikolaik/python-nodejs:python3.12-nodejs22 Link
golang amd64, arm64 golang:1.21-bookworm Link

Pull (multi-arch manifest)

# Each variant is a multi-arch manifest supporting both amd64 and arm64
docker pull ghcr.io/openhands/agent-server:102f1dc-python

Run

docker run -it --rm \
  -p 8000:8000 \
  --name agent-server-102f1dc-python \
  ghcr.io/openhands/agent-server:102f1dc-python

All tags pushed for this build

ghcr.io/openhands/agent-server:102f1dc-golang-amd64
ghcr.io/openhands/agent-server:102f1dc-golang_tag_1.21-bookworm-amd64
ghcr.io/openhands/agent-server:102f1dc-golang-arm64
ghcr.io/openhands/agent-server:102f1dc-golang_tag_1.21-bookworm-arm64
ghcr.io/openhands/agent-server:102f1dc-java-amd64
ghcr.io/openhands/agent-server:102f1dc-eclipse-temurin_tag_17-jdk-amd64
ghcr.io/openhands/agent-server:102f1dc-java-arm64
ghcr.io/openhands/agent-server:102f1dc-eclipse-temurin_tag_17-jdk-arm64
ghcr.io/openhands/agent-server:102f1dc-python-amd64
ghcr.io/openhands/agent-server:102f1dc-nikolaik_s_python-nodejs_tag_python3.12-nodejs22-amd64
ghcr.io/openhands/agent-server:102f1dc-python-arm64
ghcr.io/openhands/agent-server:102f1dc-nikolaik_s_python-nodejs_tag_python3.12-nodejs22-arm64
ghcr.io/openhands/agent-server:102f1dc-golang
ghcr.io/openhands/agent-server:102f1dc-java
ghcr.io/openhands/agent-server:102f1dc-python

About Multi-Architecture Support

  • Each variant tag (e.g., 102f1dc-python) is a multi-arch manifest supporting both amd64 and arm64
  • Docker automatically pulls the correct architecture for your platform
  • Individual architecture tags (e.g., 102f1dc-python-amd64) are also available if needed

… step

search_events blocks for the entire duration of agent.step() because
the read path and the run loop contend on the same FIFOLock inside
ConversationState.

Co-authored-by: openhands <openhands@all-hands.dev>
@github-actions

github-actions Bot commented Mar 4, 2026 •

Copy link
Copy Markdown
Contributor

API breakage checks (Griffe)

Result: Failed

Log excerpt (first 1000 characters)

============================================================
Checking openhands-sdk (openhands.sdk)
============================================================
Comparing openhands-sdk 1.11.5 against 1.11.4
::notice title=openhands-sdk API::Ignoring Field metadata-only change (non-breaking): load_public_skills
::notice title=openhands-sdk API::Ignoring Field metadata-only change (non-breaking): temperature
::warning file=openhands-sdk/openhands/sdk/llm/llm.py,line=196,title=LLM.top_p::Attribute value was changed: `Field(default=1.0, ge=0, le=1)` -> `Field(default=None, ge=0, le=1, description='Nucleus sampling parameter. Defaults to None (uses provider default). Set to a value between 0 and 1 to control diversity of outputs.')`
::error title=SemVer::Breaking changes detected (1); require at least minor version bump from 1.11.x, but new is 1.11.5

============================================================
Checking openhands-workspace (openhands.workspace)
============================

Action log

@github-actions

github-actions Bot commented Mar 4, 2026 •

Copy link
Copy Markdown
Contributor

Agent server REST API breakage checks (OpenAPI)

Result: Passed

Action log

@all-hands-bot all-hands-bot left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Taste Rating: 🟡 Acceptable - Excellent problem documentation, but CI impact needs addressing

[CRITICAL ISSUES]
The test will fail and break CI. Either mark it @pytest.mark.xfail(reason="reproduces lock contention - fix pending") or include the fix in this PR.

[IMPROVEMENT OPPORTUNITIES]
The mock setup works but is fragile. Consider if there's a cleaner way to test lock contention.

VERDICT: ⚠️ Needs xfail marker before merge - otherwise this blocks everyone's CI

KEY INSIGHT: Failing tests that document bugs are valuable, but must be marked as expected-to-fail to avoid breaking the build for other developers.

@github-actions

github-actions Bot commented Mar 4, 2026

Copy link
Copy Markdown
Contributor

Coverage

Coverage Report •
FileStmtsMissCoverMissing
openhands-agent-server/openhands/agent_server
   event_service.py3198174%55–56, 74–76, 85–89, 92–95, 115, 219, 236, 290–291, 295, 303, 306, 352–353, 369, 371, 375–377, 381, 390–391, 393, 397, 403, 405, 413–418, 554, 556–557, 561, 575–577, 579, 583–586, 590–593, 601–604, 624, 628–633, 645–646, 648–649, 656–657, 659–660, 664, 670, 687–688
TOTAL19158872854% 

@tofarr tofarr left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Append only nature means locking should not be required

@neubig

neubig commented Mar 5, 2026

Copy link
Copy Markdown
Member

I'm going to merge to prevent this from going stale.

@neubig
neubig merged commit 0ca523a into main Mar 5, 2026
31 of 32 checks passed
@neubig
neubig deleted the fix/search-events-lock-contention branch March 5, 2026 15:01
zparnold pushed a commit to zparnold/software-agent-sdk that referenced this pull request Mar 5, 2026
StressTestor pushed a commit to StressTestor/software-agent-sdk that referenced this pull request Jun 1, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants