Repository navigation
fix(agent-server): take over conversation leases left by crashed owners - #3184
Conversation
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>
Python API breakage checks — ✅ PASSEDResult: ✅ PASSED |
REST API breakage checks (OpenAPI) — ✅ PASSEDResult: ✅ PASSED |
|
@OpenHands fix failing gh action |
|
I'm on it! rbren can track my progress at all-hands.dev |
Co-authored-by: openhands <openhands@all-hands.dev>
|
Final summary of the new work since my last summary:
Conciseness check:
Net new outcome:
|
all-hands-bot
left a comment
There was a problem hiding this comment.
🟢 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
left a comment
There was a problem hiding this comment.
✅ 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.pyOutput:
[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.pyOutput:
[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.pyResults:
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_hostdiffers from the current host, preserving split-brain protection - Legacy leases: Leases without
owner_host/owner_pidfields 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.pyOutput:
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.
…rs (OpenHands#3184) Co-authored-by: openhands <openhands@all-hands.dev>
…rs (OpenHands#3184) Co-authored-by: openhands <openhands@all-hands.dev>
When the agent-server is killed or otherwise exits non-gracefully, the per-conversation
owner_lease.jsonis left on disk with anexpires_attimestamp up to 45 seconds in the future. A freshly started server generates a newowner_instance_id, sees the lease as held, and skips the conversation inConversationService.__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_pidkeep their previous TTL-only semantics.Why
Summary
Issue Number
How to Test
Video/Screenshots
Type
Notes
Agent Server images for this PR
• GHCR package: https://github.com/OpenHands/agent-sdk/pkgs/container/agent-server
Variants & Base Images
eclipse-temurin:17-jdknikolaik/python-nodejs:python3.13-nodejs22-slimgolang:1.21-bookwormPull (multi-arch manifest)
# Each variant is a multi-arch manifest supporting both amd64 and arm64 docker pull ghcr.io/openhands/agent-server:529efa7-pythonRun
All tags pushed for this build
About Multi-Architecture Support
529efa7-python) is a multi-arch manifest supporting both amd64 and arm64529efa7-python-amd64) are also available if needed