Skip to content

Fix auto-title race condition: extract message before background task - #2599

Merged
enyst merged 3 commits into
mainfrom
fix/autotitle-race-condition
Apr 4, 2026
Merged

enyst merged 3 commits into
mainfrom
fix/autotitle-race-condition

Conversation

@rbren

@rbren rbren commented Mar 29, 2026 •

Copy link
Copy Markdown
Member

Problem

Auto-title generation fails intermittently with ValueError: No user messages found in conversation events.

All 33 failures in the logs are this same error — a race condition in AutoTitleSubscriber.

Root Cause

The subscriber fires on a user MessageEvent, then spawns a background asyncio.create_task that calls generate_title(). That method re-reads self._state.events to find the first user message — but the event that triggered the subscriber hasn't been persisted to the events list yet when the background task runs.

Fix

Extract the message text directly from the event in the subscriber callback (which already has it), then pass it to the title generation utils (generate_title_with_llm / generate_fallback_title). This completely avoids the race since we no longer need to re-read from the events list.

Changes

  • conversation_service.py: Added _extract_message_text() helper. Refactored AutoTitleSubscriber to extract text upfront and added _generate_title_from_text() that calls title utils directly.
  • test_conversation_service.py: Updated TestAutoTitle tests to match new behavior. Added tests for empty messages and LLM fallback.

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.13-nodejs22-slim 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:d9cc307-python

Run

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

All tags pushed for this build

ghcr.io/openhands/agent-server:d9cc307-golang-amd64
ghcr.io/openhands/agent-server:d9cc307-golang_tag_1.21-bookworm-amd64
ghcr.io/openhands/agent-server:d9cc307-golang-arm64
ghcr.io/openhands/agent-server:d9cc307-golang_tag_1.21-bookworm-arm64
ghcr.io/openhands/agent-server:d9cc307-java-amd64
ghcr.io/openhands/agent-server:d9cc307-eclipse-temurin_tag_17-jdk-amd64
ghcr.io/openhands/agent-server:d9cc307-java-arm64
ghcr.io/openhands/agent-server:d9cc307-eclipse-temurin_tag_17-jdk-arm64
ghcr.io/openhands/agent-server:d9cc307-python-amd64
ghcr.io/openhands/agent-server:d9cc307-nikolaik_s_python-nodejs_tag_python3.13-nodejs22-slim-amd64
ghcr.io/openhands/agent-server:d9cc307-python-arm64
ghcr.io/openhands/agent-server:d9cc307-nikolaik_s_python-nodejs_tag_python3.13-nodejs22-slim-arm64
ghcr.io/openhands/agent-server:d9cc307-golang
ghcr.io/openhands/agent-server:d9cc307-java
ghcr.io/openhands/agent-server:d9cc307-python

About Multi-Architecture Support

  • Each variant tag (e.g., d9cc307-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., d9cc307-python-amd64) are also available if needed

The AutoTitleSubscriber was spawning a background task that re-read
events from the conversation state to find the first user message.
Due to a race condition, the event that triggered the subscriber
hadn't been persisted to the events list yet, causing 'No user
messages found in conversation events' errors (33/33 failures).

Fix: extract the message text directly from the event in the
subscriber callback (which already has the event), then pass it
to the title generation utils. This completely avoids the race.

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

github-actions Bot commented Mar 29, 2026 •

Copy link
Copy Markdown
Contributor

Python API breakage checks — ✅ PASSED

Result: ✅ PASSED

Action log

@github-actions

github-actions Bot commented Mar 29, 2026 •

Copy link
Copy Markdown
Contributor

REST API breakage checks (OpenAPI) — ✅ PASSED

Result: ✅ PASSED

Action log

@github-actions

github-actions Bot commented Mar 29, 2026 •

Copy link
Copy Markdown
Contributor

Coverage

Coverage Report •
FileStmtsMissCoverMissing
openhands-agent-server/openhands/agent_server
   conversation_service.py4549579%141–142, 169, 172, 174, 181–187, 215, 222, 243, 342, 348, 353, 359, 367–368, 377–380, 389, 403–405, 412, 445–446, 485, 488, 505–509, 511–512, 515–516, 519–524, 621, 628–632, 635–636, 640–644, 647–648, 652–656, 659–660, 666–671, 678–679, 683, 685–686, 691–692, 698–699, 706–707, 711–713, 731, 755, 1004, 1007
openhands-sdk/openhands/sdk/conversation
   title_utils.py56394%35, 75, 156
TOTAL22528559075% 

@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.

🟡 Acceptable - Solves the race condition but introduces code duplication. Consider refactoring to reuse existing utilities.

@rbren

rbren commented Apr 2, 2026

Copy link
Copy Markdown
Member Author

@OpenHands fix all the code duplication mentioned in code review

@openhands-ai

openhands-ai Bot commented Apr 2, 2026

Copy link
Copy Markdown

I'm on it! rbren can track my progress at all-hands.dev

@enyst enyst left a comment •

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I’m OpenHands-GPT-5.4, an AI assistant responding on behalf of the user.

Thanks for the targeted fix — this looks like it addresses a real race in the autotitle path.

My read is that the core issue is AutoTitleSubscriber firing before the first user event is guaranteed to be persisted, so extracting the text from the incoming MessageEvent before spawning the background task is a sound way to break that dependency on event-store timing.

I agree there is a bit of duplication with the existing title utilities, but I would treat that as follow-up cleanup rather than a blocker here. I would not contort the current event-based generate_conversation_title() API into also accepting raw message text just to deduplicate this patch; if we want cleanup, a dedicated helper like generate_title_from_message_text(...) or extract_text_from_message(...) would be a cleaner refactor.

From a maintainer perspective, I’d favor landing the race fix first and iterating on cleanup separately.

This review was created by OpenHands-GPT-5.4, an AI assistant, on behalf of the user.

@enyst

enyst commented Apr 4, 2026

Copy link
Copy Markdown
Member

@OpenHands Understand this PR, merge main into it, babysit it and review it, and if you think it's ready merge it.

@openhands-ai

openhands-ai Bot commented Apr 4, 2026

Copy link
Copy Markdown

I'm on it! enyst can track my progress at all-hands.dev

Refactor auto-title generation to reuse shared message/title helpers.

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

@enyst enyst left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Reviewed latest commit fd7d126. Merging main resolved the conflict cleanly, and the follow-up refactor now centralizes message extraction/title generation in title_utils without widening the existing event-based API. I also verified locally with make build, uv run pytest tests/sdk/conversation/test_generate_title.py tests/agent_server/test_conversation_service.py, and uv run pre-commit run --files AGENTS.md openhands-sdk/openhands/sdk/conversation/title_utils.py openhands-agent-server/openhands/agent_server/conversation_service.py tests/agent_server/test_conversation_service.py. Looks ready pending green CI.

This review was created by an AI assistant (OpenHands) on behalf of the user.

@enyst
enyst merged commit 00f05d7 into main Apr 4, 2026
38 of 40 checks passed
@enyst
enyst deleted the fix/autotitle-race-condition branch April 4, 2026 23:22
@openhands-ai

openhands-ai Bot commented Apr 4, 2026

Copy link
Copy Markdown

Here’s the final summary of my work since the last summary:

  • I babysat the PR through CI to completion.
  • I investigated the failing sdk-tests check, confirmed it was flaky/non-deterministic rather than caused by this PR, and reproduced the relevant SDK test suites locally successfully.
  • I reran the failed CI job until it passed.
  • I submitted an approval review on the PR with validation notes.
  • I merged PR Fix auto-title race condition: extract message before background task #2599 successfully once all required checks were green.
  • I confirmed the PR is now in the MERGED state.

Conciseness check:

  • No additional code changes were made beyond what was needed for the merge workflow and prior review-addressing work.
  • I did not introduce extra scope after the implementation/validation work; the remaining actions were only PR review, CI babysitting, rerun, and merge.
  • The feature branch was intentionally left undeleted on the remote.

Final state:

StressTestor pushed a commit to StressTestor/software-agent-sdk that referenced this pull request Jun 1, 2026
…OpenHands#2599)

Co-authored-by: openhands <openhands@all-hands.dev>
Co-authored-by: Engel Nyst <engel.nyst@gmail.com>
pengzhang-create pushed a commit to Pyromind-Dynamics/software-agent-sdk that referenced this pull request Aug 13, 2026
…OpenHands#2599)

Co-authored-by: openhands <openhands@all-hands.dev>
Co-authored-by: Engel Nyst <engel.nyst@gmail.com>
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.

3 participants