Skip to content

fix(sandbox): keep the session key hash in sync when a resume fails on the app server but completes on the runtime API (OHE-3850) - #1151

Merged
hieptl merged 2 commits into
mainfrom
hiep/ohe-3850-resume-that-fails-on-the-app-server-but-completes-on-the
Nov 5, 2026
Merged

hieptl merged 2 commits into
mainfrom
hiep/ohe-3850-resume-that-fails-on-the-app-server-but-completes-on-the

Conversation

@hieptl

@hieptl hieptl commented Nov 3, 2026 •

Copy link
Copy Markdown
Contributor

HUMAN:

  • A human has tested these changes.

AGENT:


Why

The runtime API rotates a sandbox's session key on resume and returns the new key only in the /resume response. RemoteSandboxService.resume_sandbox stored the new session_api_key_hash only after a 2xx. When the response was lost (a transport error or the client timeout) or failed late with a 5xx, it raised a 502 without looking at the runtime again, even though the runtime API had already moved the runtime to STARTING and went on to start the pod with the new key.

From then on the stored hash belonged to the old key, or was empty because the pause clears it. valid_sandbox authenticates webhook posts with a database-only hash lookup, so it rejected every event the resumed pod posted with 401 until a later resume succeeded. The agent server gives up after its retries, so those events were lost: the reported conversation has a 155-minute hole in its history.

Summary

  • Reading a RUNNING runtime now refreshes the stored hash from the key on the runtime record when it differs. This covers get_sandbox, the batch and search reads, and the session key lookup. Paused and starting runtimes are left alone: a paused record still carries the key the pause invalidated, and a starting one may not carry the rotated key yet.
  • A /resume that raises or returns a 5xx now re-checks the runtime once, like the existing 409 handling. If it is already STARTING or RUNNING, the resume is reported as a success instead of a 502, and the next read of the running sandbox refreshes the hash. Otherwise the 502 is raised as before.
  • New tests in tests/unit/app_server/test_remote_sandbox_service.py cover both failure branches, the immediate refresh when the re-check already sees RUNNING, the RUNNING-only refresh on read, and the reported scenario on a real table.

Issue Number

OHE-3850. Fixes #1030.

How to Test

uv run pytest tests/unit/app_server/test_remote_sandbox_service.py

143 tests pass (one of them starts Postgres through Docker), and 5 of the 7 new tests fail on main. The other 2 check that paused and starting runtimes leave the hash alone, so they pass on both. ruff 0.12.5, the version pre-commit pins, is clean, and mypy reports nothing new on the touched file.

The lost-response path is hard to trigger by hand, because the runtime API has to take longer than the app server's HTTP client timeout to answer /resume. To check the recovery on a test environment with the remote runtime:

  1. Start a conversation and wait for its sandbox to run.
  2. Set that sandbox's session_api_key_hash to NULL in v1_remote_sandbox. The agent server's webhook posts now get 401.
  3. Open the conversation in Canvas. The column is set again and the webhook posts are accepted.

I have not run this on a deployed environment yet.

Video/Screenshots

Backend only.

Type

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

Notes

  • No migration or config change, and only the remote backend changes. Sandboxes that already have a stale hash are fixed the next time the running sandbox is read, so there is no backfill.
  • A read writes the sandbox row only when the hash differs from the running runtime's key, so steady-state reads add no writes.
  • The ticket's last acceptance criterion, the previous pod's final batch after a rotation, needs no change here. The agent server already logs that loss at error level, and the URL in that log carries the conversation id. Rotating the key on resume is intended.
  • Known edge: a read that sees RUNNING just before an app-server pause commits can set the hash again after the pause cleared it. Runtime-API idle pauses already leave the hash in place today, and the sandbox secret endpoints still check that the sandbox is running.
  • The Canvas side of the same rotation, where the socket keeps retrying the old key, is [Bug]: After a sandbox resume Canvas keeps reconnecting with the rotated-out session key and shows "Unable to connect to server" OpenHands#18992.

Enterprise server image for this PR:

ghcr.io/openhands/enterprise-server:sha-671ff30

…n the app server but completes on the runtime API (OHE-3850)

The runtime API rotates a sandbox's session key on resume and returns the
new key only in the /resume response. resume_sandbox stored the new hash
only after a 2xx. When the response was lost (a transport error or the
client timeout) or failed late with a 5xx, it raised a 502 without
looking at the runtime again, even though the runtime API had already
moved the runtime to STARTING and went on to start the pod with the new
key. The stored hash stayed on the old key, or empty after the pause, so
valid_sandbox rejected every webhook post from the resumed pod with 401
until a later resume succeeded, and the agent server dropped those
events.

- Reading a RUNNING runtime now refreshes the stored hash from the key on
  the runtime record when it differs. This covers get_sandbox, the batch
  and search reads, and the session key lookup. Paused and starting
  runtimes are left alone: a paused record still carries the key the
  pause invalidated, and a starting one may not carry the rotated key
  yet.
- A /resume that raises or returns a 5xx re-checks the runtime once. If
  it is already STARTING or RUNNING, the resume is reported as a success
  instead of a 502, and the next read of the running sandbox, such as the
  wait for it after the resume, refreshes the hash. Otherwise the 502 is
  raised as before.

Tests cover both resume failure branches, the immediate refresh when the
re-check already sees RUNNING, the RUNNING-only refresh on read, and the
reported scenario on a real table: after a lost resume response and one
read of the running runtime, the webhook key lookup accepts the rotated
key and rejects the old one.
@github-actions github-actions Bot added the type: fix A bug fix label Nov 3, 2026
@github-actions

github-actions Bot commented Nov 3, 2026 •

Copy link
Copy Markdown
Contributor

Coverage report

Click to see where and how coverage changed

FileStatementsMissingCoverageCoverage
(new stmts)
Lines missing
  openhands/app_server/sandbox
  remote_sandbox_service.py 247, 765-766, 768
Project Total  

This report was generated by python-coverage-comment-action

@tofarr tofarr left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

🍰

@hieptl
hieptl merged commit 16523db into main Nov 5, 2026
24 checks passed
@hieptl
hieptl deleted the hiep/ohe-3850-resume-that-fails-on-the-app-server-but-completes-on-the branch November 5, 2026 14:31
@openhands-release-bot openhands-release-bot Bot added the released: 1.87.0 Shipped in 1.87.0 label Nov 5, 2026
@openhands-release-bot

Copy link
Copy Markdown
Contributor

🚀 Released in 1.87.0.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

released: 1.87.0 Shipped in 1.87.0 type: fix A bug fix

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Resume that fails on the app server but completes on the runtime API leaves a stale session key hash, so the runtime's events get 401 for hours

2 participants