Skip to content

docs(sdk): document FIFOLock held across arun()'s await (Q4 of #3341) - #3400

Merged
VascoSch92 merged 5 commits into
mainfrom
docs/fifolock-await-invariant
May 29, 2026
Merged

VascoSch92 merged 5 commits into
mainfrom
docs/fifolock-await-invariant

Conversation

@VascoSch92

@VascoSch92 VascoSch92 commented May 27, 2026 •

Copy link
Copy Markdown
Member

What

Q4 of #3341, documentation only. arun() holds the state lock (FIFOLock) across await self.agent.astep(...). Two comments make that implicit, load-bearing invariant explicit:

  • at the await site in arun() — why holding the lock across the await is safe, and the rule it depends on;
  • on FIFOLock — that reentrancy is keyed by OS thread id, not by asyncio task.

Why

The lock-across-await is safe today only because every other state mutator is dispatched via run_in_executor onto a worker thread. Since FIFOLock is thread-reentrant, a state-mutating coroutine awaited on the event-loop thread would silently re-enter the lock — no block, no error — and corrupt history. Nothing in the code recorded that rule; this turns it into a greppable warning so a future async mutator (or the Q5 refactor) doesn't trip over it.

No behavior change.

Verification

  • ruff format --check and ruff check clean

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:47922a7-python

Run

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

All tags pushed for this build

ghcr.io/openhands/agent-server:47922a7-golang-amd64
ghcr.io/openhands/agent-server:47922a7ee2db83346f2ecfd73c5be43e40fadf77-golang-amd64
ghcr.io/openhands/agent-server:docs-fifolock-await-invariant-golang-amd64
ghcr.io/openhands/agent-server:47922a7-golang_tag_1.21-bookworm-amd64
ghcr.io/openhands/agent-server:47922a7-golang-arm64
ghcr.io/openhands/agent-server:47922a7ee2db83346f2ecfd73c5be43e40fadf77-golang-arm64
ghcr.io/openhands/agent-server:docs-fifolock-await-invariant-golang-arm64
ghcr.io/openhands/agent-server:47922a7-golang_tag_1.21-bookworm-arm64
ghcr.io/openhands/agent-server:47922a7-java-amd64
ghcr.io/openhands/agent-server:47922a7ee2db83346f2ecfd73c5be43e40fadf77-java-amd64
ghcr.io/openhands/agent-server:docs-fifolock-await-invariant-java-amd64
ghcr.io/openhands/agent-server:47922a7-eclipse-temurin_tag_17-jdk-amd64
ghcr.io/openhands/agent-server:47922a7-java-arm64
ghcr.io/openhands/agent-server:47922a7ee2db83346f2ecfd73c5be43e40fadf77-java-arm64
ghcr.io/openhands/agent-server:docs-fifolock-await-invariant-java-arm64
ghcr.io/openhands/agent-server:47922a7-eclipse-temurin_tag_17-jdk-arm64
ghcr.io/openhands/agent-server:47922a7-python-amd64
ghcr.io/openhands/agent-server:47922a7ee2db83346f2ecfd73c5be43e40fadf77-python-amd64
ghcr.io/openhands/agent-server:docs-fifolock-await-invariant-python-amd64
ghcr.io/openhands/agent-server:47922a7-nikolaik_s_python-nodejs_tag_python3.13-nodejs22-slim-amd64
ghcr.io/openhands/agent-server:47922a7-python-arm64
ghcr.io/openhands/agent-server:47922a7ee2db83346f2ecfd73c5be43e40fadf77-python-arm64
ghcr.io/openhands/agent-server:docs-fifolock-await-invariant-python-arm64
ghcr.io/openhands/agent-server:47922a7-nikolaik_s_python-nodejs_tag_python3.13-nodejs22-slim-arm64
ghcr.io/openhands/agent-server:47922a7-golang
ghcr.io/openhands/agent-server:47922a7ee2db83346f2ecfd73c5be43e40fadf77-golang
ghcr.io/openhands/agent-server:docs-fifolock-await-invariant-golang
ghcr.io/openhands/agent-server:47922a7-golang_tag_1.21-bookworm
ghcr.io/openhands/agent-server:47922a7-java
ghcr.io/openhands/agent-server:47922a7ee2db83346f2ecfd73c5be43e40fadf77-java
ghcr.io/openhands/agent-server:docs-fifolock-await-invariant-java
ghcr.io/openhands/agent-server:47922a7-eclipse-temurin_tag_17-jdk
ghcr.io/openhands/agent-server:47922a7-python
ghcr.io/openhands/agent-server:47922a7ee2db83346f2ecfd73c5be43e40fadf77-python
ghcr.io/openhands/agent-server:docs-fifolock-await-invariant-python
ghcr.io/openhands/agent-server:47922a7-nikolaik_s_python-nodejs_tag_python3.13-nodejs22-slim

About Multi-Architecture Support

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

arun() holds the state lock (FIFOLock) across 'await astep'. That is safe only because every other state mutator is dispatched via run_in_executor onto a worker thread; FIFOLock is reentrant by OS thread id, so a state-mutating coroutine awaited on the event-loop thread would silently re-enter the lock and corrupt history. Make this load-bearing invariant explicit with a comment at the await site and a note on FIFOLock. Comment-only; no behavior change.
@github-actions

github-actions Bot commented May 27, 2026 •

Copy link
Copy Markdown
Contributor

Python API breakage checks — ✅ PASSED

Result: ✅ PASSED

Action log

@github-actions

github-actions Bot commented May 27, 2026 •

Copy link
Copy Markdown
Contributor

REST API breakage checks (OpenAPI) — ✅ PASSED

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. The warning is useful, but one sentence overstates the executor invariant; I left one inline comment.

[RISK ASSESSMENT] 🟢 LOW — documentation-only, no runtime behavior change.

VERDICT: Comment until the wording is clarified.

This review was generated by an AI agent (OpenHands) on behalf of the requester.


Was this automated review useful? React with 👍 or 👎 to this review to help us measure review quality.
Workflow run: https://github.com/OpenHands/software-agent-sdk/actions/runs/26503939638

@VascoSch92
VascoSch92 requested a review from malhotra5 May 27, 2026 09:52

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

✅ QA Report: PASS

Runtime SDK verification confirms the PR documents the FIFO lock/arun() async invariant without changing observed lock behavior.

Does this PR achieve its stated goal?

Yes. The stated goal was documentation-only: make the lock-across-await invariant and FIFOLock's thread-based reentrancy explicit. Running the SDK on main showed the underlying same-thread coroutine reentry behavior existed but both warnings were absent; running the same verification on 830ed290 showed both warnings present while imports and runtime lock behavior remained unchanged.

Phase Result
Environment Setup ✅ make build completed successfully; SDK imports worked via uv run python.
CI Status 🟡 At time checked: 15 successful, 12 in progress, 3 skipped; no failures observed.
Functional Verification ✅ Before/after runtime introspection showed the new warnings are present and behavior is unchanged.
Functional Verification

Test 1: Documentation warning appears for the actual SDK objects while lock behavior remains unchanged

Step 1 — Establish baseline without the PR:
Checked out origin/main and ran a Python script that imported FIFOLock and LocalConversation, demonstrated same-event-loop-thread reentry while a lock holder was suspended across await, and inspected the runtime doc/source text exposed by Python introspection:

git checkout origin/main >/tmp/qa_checkout_base.log 2>&1 && uv run python - <<'PY'
import asyncio, inspect, subprocess
from openhands.sdk.conversation.fifo_lock import FIFOLock
from openhands.sdk.conversation.impl.local_conversation import LocalConversation
# creates one holder coroutine and one contender coroutine sharing one FIFOLock
# then prints import status, observed lock events, and whether the new warnings exist
PY

Output excerpt:

commit=5c11fece
import_ok=FIFOLock,LocalConversation
runtime_reentry_events=holder acquired | contender acquired while holder awaited | holder releasing
fifolock_doc_asyncio_warning_present=False
arun_lock_across_await_warning_present=False

This confirms the invariant the PR describes is real: another coroutine on the same event-loop thread can acquire the thread-reentrant lock while the holder is suspended. It also establishes the baseline documentation gap: neither warning was present on main.

Step 2 — Apply the PR's changes:
Checked out docs/fifolock-await-invariant and reset to the PR commit 830ed2909e2cad7f4fa5e63efb040bf0e8f8b12f.

Step 3 — Re-run with the PR in place:
Ran the same SDK import/introspection/lock-behavior script:

git checkout docs/fifolock-await-invariant >/tmp/qa_checkout_pr.log 2>&1 && git reset --hard 830ed2909e2cad7f4fa5e63efb040bf0e8f8b12f >/tmp/qa_reset_pr.log 2>&1 && uv run python - <<'PY'
import asyncio, inspect, subprocess
from openhands.sdk.conversation.fifo_lock import FIFOLock
from openhands.sdk.conversation.impl.local_conversation import LocalConversation
# same script as baseline
PY

Output excerpt:

commit=830ed290
import_ok=FIFOLock,LocalConversation
runtime_reentry_events=holder acquired | contender acquired while holder awaited | holder releasing
fifolock_doc_asyncio_warning_present=True
arun_lock_across_await_warning_present=True

This shows the PR achieves its documentation goal: the FIFOLock asyncio/task warning and the arun() lock-across-await warning are now visible through the actual SDK source/doc objects. The identical runtime_reentry_events output also supports the PR's claim of no behavior change.

Issues Found

None.

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

Reword the comment above astep() to reflect that astep()'s own state
mutations are intentional and part of the locked critical section.
The run_in_executor requirement applies only to unrelated concurrent
mutators, not to native async Agent.astep / ACPAgent.astep that mutate
on the event-loop thread.
@VascoSch92
VascoSch92 requested a review from all-hands-bot May 28, 2026 15:55

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

✅ QA Report: PASS

Verified the documentation-only change by importing the SDK from base and PR branches and exercising FIFOLock behavior; the PR documents the observed invariant without changing runtime behavior.

Does this PR achieve its stated goal?

Yes. The PR set out to make the FIFOLock/arun lock-across-await invariant explicit without changing behavior. On main, the imported SDK did not expose the new FIFOLock docstring/source warnings; on the PR branch, the same probes returned True for the new warnings. The runtime lock probe behaved identically before and after: same event-loop coroutines re-enter the lock while held, while a worker thread waits until release.

Phase Result
Environment Setup ✅ make build completed and installed the repo packages with uv sync --dev.
CI Status ⚠️ 31 successful, 1 skipped, 1 pending (QA Changes by OpenHands); no failing checks observed.
Functional Verification ✅ Imported and exercised SDK lock behavior on both base and PR branches.
Functional Verification

Test 1: Documentation visibility and FIFOLock runtime invariant

Step 1 — Reproduce / establish baseline (without the fix):
Ran:

git checkout main >/tmp/qa_checkout_main.out 2>&1 && \
  echo '--- BASE main ---' && \
  uv run python /tmp/qa_fifolock_runtime.py

Observed relevant output:

--- BASE main ---
FIFOLock doc mentions OS thread id: False
FIFOLock doc mentions asyncio task: False
LocalConversation.arun source mentions state lock held across await: False
LocalConversation.arun source mentions run_in_executor: False
Same event-loop coroutine re-entered while held: True
Worker thread attempted acquire while held: True
Worker thread acquired before release: False
Worker thread acquired after release: True
Worker acquisition observed main release started: True

This establishes the baseline: the runtime behavior already matches the invariant, but the newly claimed documentation warnings are absent on main.

Step 2 — Apply the PR's changes:
Checked out docs/fifolock-await-invariant at c58f62ecac1d8876f6a087a9f59cada81df2451e.

Step 3 — Re-run with the fix in place:
Ran:

git checkout docs/fifolock-await-invariant >/tmp/qa_checkout_pr.out 2>&1 && \
  echo '--- PR docs/fifolock-await-invariant ---' && \
  uv run python /tmp/qa_fifolock_runtime.py

Observed relevant output:

--- PR docs/fifolock-await-invariant ---
FIFOLock doc mentions OS thread id: True
FIFOLock doc mentions asyncio task: True
LocalConversation.arun source mentions state lock held across await: True
LocalConversation.arun source mentions run_in_executor: True
Same event-loop coroutine re-entered while held: True
Worker thread attempted acquire while held: True
Worker thread acquired before release: False
Worker thread acquired after release: True
Worker acquisition observed main release started: True

This confirms the PR adds the intended documentation markers while preserving the same lock behavior observed on main.

Issues Found

None.

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

@VascoSch92

Copy link
Copy Markdown
Member Author

@OpenHands fix the pre-commit checks and update the branch

@openhands-ai

openhands-ai Bot commented May 29, 2026

Copy link
Copy Markdown

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

@openhands-ai

openhands-ai Bot commented May 29, 2026

Copy link
Copy Markdown

Summary

Addressed @VascoSch92's comment "fix the pre-commit checks and update the branch" on PR #3400.

Checklist

  • ✅ Fix pre-commit checks: The CI failure was ruff-format flagging a single trailing space on line 1220 of openhands-sdk/openhands/sdk/conversation/impl/local_conversation.py (inside the new FIFOLock invariant comment block added by this PR). Ran uv run ruff format to strip it, and verified locally with uv run pre-commit run --files <path> — all hooks pass.
  • ✅ Update branch: Merged origin/main into docs/fifolock-await-invariant cleanly (ort strategy, no conflicts; pulled in 3 unrelated files from main).
  • ✅ Pushed commit 47922a7e to the PR branch so CI re-runs.

Conciseness

Changes are minimal and on-scope:

  • 1-line whitespace fix in the file the PR already touches.
  • 1 merge commit for the requested branch update.

No extraneous edits.

@VascoSch92
VascoSch92 enabled auto-merge (squash) May 29, 2026 09:35
@VascoSch92
VascoSch92 requested a review from all-hands-bot May 29, 2026 09:35

all-hands-bot commented May 29, 2026 •

Copy link
Copy Markdown
Collaborator

✅ Review complete.

This review was performed through OpenHands Cloud Automation. You can log in and view the conversation here.

@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: 🟢 Good taste — Accurate, targeted documentation of a genuinely non-obvious concurrency invariant.


fifo_lock.py — FIFOLock docstring addition

The new Note is technically accurate: FIFOLock calls threading.get_ident() for reentrancy, so any two asyncio tasks on the same OS thread share the same owner identity and silently re-enter. The general rule ("Only hold it across an await when no other task on that thread can acquire it") is a correct, actionable contract for callers.

local_conversation.py — inline comment before await self.agent.astep()

The 12-line block does exactly what was needed:

  • It explicitly exempts astep()'s own mutations — including native async Agent.astep / ACPAgent.astep that call on_event on the event-loop thread — as intentional members of the critical section, which removes the ambiguity that a prior review raised about over-broad wording.
  • It narrows the invariant to unrelated state mutators, and correctly prescribes run_in_executor as the dispatch mechanism for those.
  • The failure mode is clearly stated: a concurrent same-thread coroutine would silently re-enter and corrupt history.

The two places (docstring and call site) are coherent and complementary — reading one doesn't require the other.

Minor observations (non-blocking)

Convention with no enforcement: The prescription "Such unrelated mutators must be dispatched via run_in_executor onto a worker thread" is a design invariant enforced only by convention. A future contributor adding a new async state mutator may not find this comment. A follow-up could add a brief note in a CONTRIBUTING.md or an architectural doc that lists this as a known constraint on arun()'s execution model — but this is out of scope for a documentation-only PR and is not required to merge.

Documentation scope: The comment sits inside the else: branch that covers the non-ACP path only. The ACP path releases the state lock before its await astep() and re-acquires it after, so the invariant doesn't apply there. The placement is correct and appropriately scoped.


[RISK ASSESSMENT]

[Overall PR] ⚠️ Risk Assessment: 🟢 LOW — documentation-only, no runtime behavior change, no new dependencies, no public API change.


VERDICT:
✅ Worth merging — The change accurately documents a load-bearing invariant that was previously implicit, makes the failure mode greppable, and leaves no behavior difference.

KEY INSIGHT: Thread-id-keyed reentrancy and asyncio cooperative scheduling are an invisible trap; making the contract explicit at both the lock definition and the await site is the right way to guard it.


This review was generated by an AI agent (OpenHands) on behalf of the user through OpenHands Automation. View conversation

@VascoSch92
VascoSch92 merged commit a178dde into main May 29, 2026
34 checks passed
@VascoSch92
VascoSch92 deleted the docs/fifolock-await-invariant branch May 29, 2026 09:40

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

✅ QA Report: PASS

Verified the docs-only PR by comparing baseline vs PR documentation visibility and exercising the affected SDK lock/conversation behavior; the PR achieves its stated goal with no functional regression observed.

Does this PR achieve its stated goal?

Yes. On main, the FIFOLock pydoc output and greppable source warning text were absent; on the PR branch, pydoc exposes the new FIFOLock note and git grep finds both intended warnings. I also exercised FIFOLock and a minimal Conversation.arun() path on both branches; observed behavior was identical, matching the PR's “No behavior change” claim.

Phase Result
Environment Setup ✅ make build completed and installed the uv workspace dependencies
CI Status ✅ Existing checks are passing; only this qa-changes check was still in progress, and cleanup-on-approval was skipped
Functional Verification ✅ Baseline/PR documentation delta verified; runtime smoke behavior unchanged
Functional Verification

Test 1: Developer-facing documentation warning is newly visible

Step 1 — Reproduce / establish baseline (without the fix):
Checked out origin/main (58cfdcef) and ran:

OPENHANDS_SUPPRESS_BANNER=1 uv run python -m pydoc openhands.sdk.conversation.fifo_lock.FIFOLock | grep -F 'reentrancy is keyed by OS thread id' || echo 'pydoc note not found'
git grep -n -E 'state lock is held across this await|reentrancy is keyed by OS thread id' -- openhands-sdk/openhands/sdk/conversation/fifo_lock.py openhands-sdk/openhands/sdk/conversation/impl/local_conversation.py || echo 'warning text not found'

Observed:

pydoc note not found
warning text not found

This establishes the baseline: the lock/task reentrancy warning and the arun() await-site warning were not available to a developer via pydoc/search before the PR.

Step 2 — Apply the PR's changes:
Checked out docs/fifolock-await-invariant at 47922a7e.

Step 3 — Re-run with the fix in place:
Ran the same commands and observed:

 |  Note: reentrancy is keyed by OS thread id, not by asyncio task, so the
openhands-sdk/openhands/sdk/conversation/fifo_lock.py:28:    Note: reentrancy is keyed by OS thread id, not by asyncio task, so the
openhands-sdk/openhands/sdk/conversation/impl/local_conversation.py:1212:                        # The state lock is held across this await. Mutations

This confirms the stated documentation goal: the FIFOLock warning is visible through runtime documentation, and both intended comments are greppable at their target locations.

Test 2: FIFOLock behavior still matches the documented invariant

Step 1 — Reproduce / establish baseline (without the fix):
On origin/main, ran a short script using the real FIFOLock that: (1) acquires the lock in one coroutine, awaits another coroutine on the same event-loop thread, and has the inner coroutine acquire the lock; and (2) confirms a worker thread blocks while the main thread holds the lock.

OPENHANDS_SUPPRESS_BANNER=1 uv run python /tmp/fifolock_qa.py

Observed:

{
  "same_event_loop_reentry_markers": [
    "outer-acquired",
    "inner-reentered-same-thread",
    "outer-after-inner"
  ],
  "worker_thread_serialization": {
    "worker_blocked_until_main_thread_released": true,
    "worker_acquired_after_release": true,
    "worker_wait_seconds": 0.15
  }
}

This confirms the underlying behavior described by the new docs: same-thread coroutines can re-enter, while a different OS thread is serialized.

Step 2 — Apply the PR's changes:
Checked out docs/fifolock-await-invariant at 47922a7e.

Step 3 — Re-run with the fix in place:
Ran the same script and observed the same result:

{
  "same_event_loop_reentry_markers": [
    "outer-acquired",
    "inner-reentered-same-thread",
    "outer-after-inner"
  ],
  "worker_thread_serialization": {
    "worker_blocked_until_main_thread_released": true,
    "worker_acquired_after_release": true,
    "worker_wait_seconds": 0.15
  }
}

This supports the PR's “No behavior change” claim while confirming the documentation accurately describes the lock behavior.

Test 3: Minimal Conversation.arun() still completes

Step 1 — Reproduce / establish baseline (without the fix):
On origin/main, ran a minimal SDK conversation with a custom async agent whose astep() awaits briefly and marks the conversation finished:

OPENHANDS_SUPPRESS_BANNER=1 uv run python /tmp/conversation_arun_qa.py

Observed:

execution_status=finished
events=1

This establishes that the async conversation path completed successfully before the docs-only change.

Step 2 — Apply the PR's changes:
Checked out docs/fifolock-await-invariant at 47922a7e.

Step 3 — Re-run with the fix in place:
Ran the same smoke script and observed:

execution_status=finished
events=1

This confirms the changed arun() area still behaves the same for a real SDK conversation using an async astep().

Issues Found

None.

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

StressTestor pushed a commit to StressTestor/software-agent-sdk that referenced this pull request Jun 1, 2026
pengzhang-create pushed a commit to Pyromind-Dynamics/software-agent-sdk that referenced this pull request Aug 13, 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.

3 participants