Skip to content

fix(agent-server): take over conversation leases left by crashed owners - #3184

Merged
rbren merged 2 commits into
mainfrom
fix/lease-crash-recovery
May 9, 2026
Merged

rbren merged 2 commits into
mainfrom
fix/lease-crash-recovery

Conversation

@rbren

@rbren rbren commented May 9, 2026 •

Copy link
Copy Markdown
Member

When the agent-server is killed or otherwise exits non-gracefully, the per-conversation owner_lease.json is left on disk with an expires_at timestamp up to 45 seconds in the future. A freshly started server generates a new owner_instance_id, sees the lease as held, and skips the conversation in ConversationService.__aenter__ - so existing conversations effectively disappear from the API for up to the full TTL window after a crash-restart.

Augment the lease payload with the owner's hostname and PID, and on claim() treat the lease as stale (eligible for takeover) when the recorded host matches this machine but the recorded PID is no longer alive. Cross-host leases continue to use the TTL-only path so the existing split-brain protection between distinct service instances is unchanged.

Backwards compatible: legacy lease files without owner_host / owner_pid keep their previous TTL-only semantics.

  • A human has tested these changes.

Why

Summary

Issue Number

How to Test

Video/Screenshots

Type

  • Bug fix
  • Feature
  • Refactor
  • Breaking change
  • Docs / chore

Notes


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:529efa7-python

Run

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

All tags pushed for this build

ghcr.io/openhands/agent-server:529efa7-golang-amd64
ghcr.io/openhands/agent-server:529efa737d7aaf228f15c2b5923d6ce05217fdaf-golang-amd64
ghcr.io/openhands/agent-server:fix-lease-crash-recovery-golang-amd64
ghcr.io/openhands/agent-server:529efa7-golang_tag_1.21-bookworm-amd64
ghcr.io/openhands/agent-server:529efa7-golang-arm64
ghcr.io/openhands/agent-server:529efa737d7aaf228f15c2b5923d6ce05217fdaf-golang-arm64
ghcr.io/openhands/agent-server:fix-lease-crash-recovery-golang-arm64
ghcr.io/openhands/agent-server:529efa7-golang_tag_1.21-bookworm-arm64
ghcr.io/openhands/agent-server:529efa7-java-amd64
ghcr.io/openhands/agent-server:529efa737d7aaf228f15c2b5923d6ce05217fdaf-java-amd64
ghcr.io/openhands/agent-server:fix-lease-crash-recovery-java-amd64
ghcr.io/openhands/agent-server:529efa7-eclipse-temurin_tag_17-jdk-amd64
ghcr.io/openhands/agent-server:529efa7-java-arm64
ghcr.io/openhands/agent-server:529efa737d7aaf228f15c2b5923d6ce05217fdaf-java-arm64
ghcr.io/openhands/agent-server:fix-lease-crash-recovery-java-arm64
ghcr.io/openhands/agent-server:529efa7-eclipse-temurin_tag_17-jdk-arm64
ghcr.io/openhands/agent-server:529efa7-python-amd64
ghcr.io/openhands/agent-server:529efa737d7aaf228f15c2b5923d6ce05217fdaf-python-amd64
ghcr.io/openhands/agent-server:fix-lease-crash-recovery-python-amd64
ghcr.io/openhands/agent-server:529efa7-nikolaik_s_python-nodejs_tag_python3.13-nodejs22-slim-amd64
ghcr.io/openhands/agent-server:529efa7-python-arm64
ghcr.io/openhands/agent-server:529efa737d7aaf228f15c2b5923d6ce05217fdaf-python-arm64
ghcr.io/openhands/agent-server:fix-lease-crash-recovery-python-arm64
ghcr.io/openhands/agent-server:529efa7-nikolaik_s_python-nodejs_tag_python3.13-nodejs22-slim-arm64
ghcr.io/openhands/agent-server:529efa7-golang
ghcr.io/openhands/agent-server:529efa737d7aaf228f15c2b5923d6ce05217fdaf-golang
ghcr.io/openhands/agent-server:fix-lease-crash-recovery-golang
ghcr.io/openhands/agent-server:529efa7-golang_tag_1.21-bookworm
ghcr.io/openhands/agent-server:529efa7-java
ghcr.io/openhands/agent-server:529efa737d7aaf228f15c2b5923d6ce05217fdaf-java
ghcr.io/openhands/agent-server:fix-lease-crash-recovery-java
ghcr.io/openhands/agent-server:529efa7-eclipse-temurin_tag_17-jdk
ghcr.io/openhands/agent-server:529efa7-python
ghcr.io/openhands/agent-server:529efa737d7aaf228f15c2b5923d6ce05217fdaf-python
ghcr.io/openhands/agent-server:fix-lease-crash-recovery-python
ghcr.io/openhands/agent-server:529efa7-nikolaik_s_python-nodejs_tag_python3.13-nodejs22-slim

About Multi-Architecture Support

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

When the agent-server is killed or otherwise exits non-gracefully, the
per-conversation `owner_lease.json` is left on disk with an
`expires_at` timestamp up to 45 seconds in the future. A freshly
started server generates a new `owner_instance_id`, sees the lease as
held, and skips the conversation in `ConversationService.__aenter__` -
so existing conversations effectively disappear from the API for up to
the full TTL window after a crash-restart.

Augment the lease payload with the owner's hostname and PID, and on
`claim()` treat the lease as stale (eligible for takeover) when the
recorded host matches this machine but the recorded PID is no longer
alive. Cross-host leases continue to use the TTL-only path so the
existing split-brain protection between distinct service instances is
unchanged.

Backwards compatible: legacy lease files without `owner_host` /
`owner_pid` keep their previous TTL-only semantics.

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

github-actions Bot commented May 9, 2026 •

Copy link
Copy Markdown
Contributor

Python API breakage checks — ✅ PASSED

Result: ✅ PASSED

Action log

@github-actions

github-actions Bot commented May 9, 2026 •

Copy link
Copy Markdown
Contributor

REST API breakage checks (OpenAPI) — ✅ PASSED

Result: ✅ PASSED

Action log

@github-actions

github-actions Bot commented May 9, 2026 •

Copy link
Copy Markdown
Contributor

Coverage

Coverage Report •
FileStmtsMissCoverMissing
openhands-agent-server/openhands/agent_server
   conversation_lease.py1461291%43–44, 57, 64, 68, 240, 246, 248, 250, 264–265, 269
TOTAL262871151556% 

@rbren

rbren commented May 9, 2026

Copy link
Copy Markdown
Member Author

@OpenHands fix failing gh action

@openhands-ai

openhands-ai Bot commented May 9, 2026

Copy link
Copy Markdown

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

Co-authored-by: openhands <openhands@all-hands.dev>
@rbren
rbren marked this pull request as ready for review May 9, 2026 21:33
@openhands-ai

openhands-ai Bot commented May 9, 2026

Copy link
Copy Markdown

Final summary of the new work since my last summary:

  • Fully addressed the request to fix the failing GitHub Action on PR fix(agent-server): take over conversation leases left by crashed owners #3184.
  • Kept the fix minimal and directly scoped to the pyright/pre-commit failure.
  • Updated only the two relevant files:
    • openhands-agent-server/openhands/agent_server/conversation_lease.py
    • tests/agent_server/test_conversation_lease.py
  • Committed and pushed the fix to the existing PR branch.
  • Verified that the previously failing Pre-commit checks/pre-commit workflow is now passing.

Conciseness check:

  • Changes are concise.
  • No unrelated files were modified.
  • No extraneous changes appear to need reverting.

Net new outcome:

  • The PR branch now includes commit 529efa73 with the typing fix.
  • The failing GitHub Action has been cleared; only unrelated longer-running checks were still pending when last checked.

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

🟢 Good taste - Elegant crash-recovery solution that solves a real production problem.

KEY INSIGHT: The PID-based liveness check elegantly eliminates the 45-second conversation blackout window after crashes while preserving cross-host split-brain protection through selective same-host takeover.

[RISK ASSESSMENT]

  • [Overall PR] ⚠️ Risk Assessment: 🟢 LOW

Backward compatible infrastructure fix with comprehensive test coverage. Only affects crash-recovery scenarios. Conservative error handling treats unknown conditions as alive, preventing inappropriate lease takeovers. No agent behavior or benchmark impact.

@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 end-to-end that orphaned conversation leases from crashed servers are immediately reclaimed on restart, eliminating the up-to-45-second API unavailability window.

Does this PR achieve its stated goal?

Yes. The PR fixes the crash-recovery bug where conversations become inaccessible after a non-graceful server shutdown. I reproduced the bug on main (conversations disappeared for the full TTL duration) and confirmed the fix on this PR branch (conversations immediately recovered). The PID-based liveness check works correctly, and all backwards-compatibility edge cases pass.

Phase Result
Environment Setup ✅ Dependencies installed via uv sync
CI Status ✅ Core tests passing (agent-server-tests, sdk-tests, pre-commit), Docker builds pending
Functional Verification ✅ Bug reproduced on main, fix verified on PR branch, all edge cases pass
Functional Verification

Test 1: Reproduce the Bug (without the fix)

Step 1 — Establish baseline on main branch:

Checked out main (commit 7569088) and ran a crash simulation:

cd /home/runner/work/software-agent-sdk/software-agent-sdk/pr-repo
git checkout main
uv run python /tmp/qa_crash_recovery.py

Output:

[Step 1] Creating conversation with initial service...
✓ Created conversation f79179490e8644c48bc6ad0b9305703e

[Step 2] Simulating non-graceful shutdown...
   (Forging a lease with a dead PID and far-future expires_at)
   Lease path: /tmp/tmp_a4pq09q/conversations/f79179490e8644c48bc6ad0b9305703e/owner_lease.json
✓ Forged lease with dead PID 2147483647
   Lease expires at: 1778366171.2784748
   Current time: 1778362571.278547
   TTL remaining: 3600.0 seconds

[Step 3] Restarting service to test crash recovery...
❌ FAILURE: Conversation f79179490e8644c48bc6ad0b9305703e was NOT recovered!
   The restarted service failed to pick up the conversation.
   This would cause the conversation to disappear from the API
   for up to 3600.0 seconds.

This confirms the bug exists: a conversation with an orphaned lease (dead PID, far-future TTL) is not recovered on restart, making it inaccessible for up to 1 hour.


Test 2: Verify the Fix (with the PR changes)

Step 2 — Apply the PR's changes:

Checked out fix/lease-crash-recovery (commit 529efa7):

git checkout fix/lease-crash-recovery
uv run python /tmp/qa_crash_recovery.py

Output:

[Step 1] Creating conversation with initial service...
✓ Created conversation da923aca8b70485abaafd6ea864782e5

[Step 2] Simulating non-graceful shutdown...
   (Forging a lease with a dead PID and far-future expires_at)
   Lease path: /tmp/tmp22s_04cw/conversations/da923aca8b70485abaafd6ea864782e5/owner_lease.json
✓ Forged lease with dead PID 2147483647
   Lease expires at: 1778366153.481207
   Current time: 1778362553.4812765
   TTL remaining: 3600.0 seconds

[Step 3] Restarting service to test crash recovery...
✅ SUCCESS: Conversation da923aca8b70485abaafd6ea864782e5 was recovered!
   The restarted service immediately reclaimed the orphaned lease
   despite the TTL being far in the future.

This shows the fix works: the conversation is recovered immediately, even though the lease TTL has 3600 seconds remaining.


Test 3: Edge Cases

Ran comprehensive edge case tests to verify backwards compatibility and safety:

uv run python /tmp/qa_edge_cases.py

Results:

Test 1: Cross-host lease uses TTL-only (no PID check)
✅ PASS: Cross-host lease respects TTL (conversation not recovered)

Test 2: Legacy lease files use TTL-only
✅ PASS: Legacy lease respects TTL (conversation not recovered)

Test 3: Same-process PID is not treated as dead
✅ PASS: Same-process lease is protected from takeover

Passed: 3/3

All edge cases pass:

  • Cross-host leases: PID checks are skipped when owner_host differs from the current host, preserving split-brain protection
  • Legacy leases: Leases without owner_host/owner_pid fields continue to use TTL-only expiry (backwards compatible)
  • Same-process protection: Leases pointing to this process's PID are never treated as dead, preventing incorrect takeover

Test 4: Lease Payload Structure

Verified that new lease files include the required fields:

uv run python /tmp/qa_lease_payload.py

Output:

Lease file contents:
  owner_instance_id: 9fec5c9d1cde4133a7fba563cd213f0a
  generation: 1
  expires_at: 1778362685.5665522
  owner_host: runnervmeorf1
  owner_pid: 3968

Verification:
  ✅ owner_instance_id
  ✅ generation
  ✅ expires_at
  ✅ owner_host
  ✅ owner_pid

✅ All fields present and correct!

New leases correctly include owner_host and owner_pid for crash recovery while maintaining backwards compatibility with legacy lease files.

Issues Found

None.

@rbren
rbren merged commit 573aa25 into main May 9, 2026
39 checks passed
@rbren
rbren deleted the fix/lease-crash-recovery branch May 9, 2026 21:59
StressTestor pushed a commit to StressTestor/software-agent-sdk that referenced this pull request Jun 1, 2026
…rs (OpenHands#3184)

Co-authored-by: openhands <openhands@all-hands.dev>
pengzhang-create pushed a commit to Pyromind-Dynamics/software-agent-sdk that referenced this pull request Aug 13, 2026
…rs (OpenHands#3184)

Co-authored-by: openhands <openhands@all-hands.dev>
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