Skip to content

fix(driver-podman): keep restarted sandboxes from failing before supervisor start - #6025

Open
pimlock wants to merge 1 commit into
mainfrom
fix/podman-restart-supervisor-race/pimlock
Open

pimlock wants to merge 1 commit into
mainfrom
fix/podman-restart-supervisor-race/pimlock

Conversation

@pimlock

@pimlock pimlock commented Nov 26, 2026

Copy link
Copy Markdown
Contributor

Summary

Fix an intermittent race in the Podman driver where a sandbox restarted with StopSandbox then StartSandbox moves from Starting to Error with reason ContainerExited. The watcher killed the freshly started workload because, during the restart, the supervisor still showed the exit from the previous run. A second, related path let a late supervisor event publish the previous run's exit.

Related Issue

No issue required: localized bug fix for an intermittent CI failure; the race and evidence are described below.

How the race happens

Each Podman sandbox is a pair of containers: the workload and its supervisor. start_sandbox starts them in two separate API calls, workload first:

start_container(workload)    // workload: running
start_container(supervisor)  // supervisor: exited -> running

The watcher handles Podman events on its own task. For every workload or supervisor event, it calls inspect_workload, which inspects the workload and then the supervisor. A running workload whose supervisor is not running is treated as a lost supervisor and contained with stop_container(workload, 0). On first create that check never fires, because the new supervisor is in created state, which is reported as starting. On a restart the supervisor is still exited from the StopSandbox.

Race 1: the watcher stops the new workload (the CI failure)

  1. StopSandbox stops the supervisor, then the workload. Both are now exited.
  2. StartSandbox calls start_container(workload). Podman emits a workload start event.
  3. The watcher handles that event, or a queued supervisor stop/die from step 1, before start_container(supervisor) takes effect. inspect_workload sees a running workload and an exited supervisor.
  4. It takes this as a lost supervisor and calls stop_container(workload, 0). The new workload is killed, and the re-inspect reports it exited.
  5. start_container(supervisor) succeeds and StartSandbox returns OK. The gateway receives the snapshot from step 4 and moves the sandbox Starting → Error with ContainerExited.
  6. The supervisor is running, so it pushes logs, but its workload is gone, so it never opens a gateway session.

This matches the gateway log of the failing run (lane rust-podman-additional-ca, test attach::attach_reconnect_replays_history_for_the_same_main_process) for sandbox 1a1665c3:

00:32:12.260710  StopSandbox request completed successfully
00:32:12.271112  podman.start_sandbox: Starting sandbox container
00:32:12.532797  StartSandbox request completed successfully
00:32:12.537742  Sandbox phase changed old_phase=Starting new_phase=Error
00:32:12.537765  Sandbox failed to become ready reason=ContainerExited
00:32:12.567     PushSandboxLogs from the new supervisor
                 (no "supervisor session: accepted" before DeleteSandbox at 00:32:27.9)

Whether it happens depends on whether the watcher's inspect lands inside the gap between the two start calls. That's why it is intermittent and why earlier runs of the same lane passed.

Race 2: a late supervisor event publishes the previous exit

Podman can deliver a container's stop/die event after the stop API returned. The existing LifecycleEventFences already drops a workload die/stop whose snapshot shows the exit recorded at the last stop or start. Supervisor events went through a separate early branch in map_podman_event that re-inspected the workload without that check. If a supervisor event from StopSandbox is handled after the gateway set Starting but before the workload start, the snapshot still shows the previous run's exited workload, and the gateway moves the sandbox to Error the same way.

How this fix resolves it

  • Race 1: start_sandbox now starts the pair through start_stopped_pair. It holds a per-sandbox pair-start guard (LifecycleEventFences::begin_pair_start) from before start_container(workload) until start_container(supervisor) returns, or until the workload is stopped after a failed supervisor start. While that guard is held, inspect_workload reports a running workload with a not-yet-running supervisor as starting rather than stopping it. Any inspect inside the gap now publishes Starting, and the next event after the supervisor starts reports the real state.
    • Containment is unchanged everywhere else. The guard lives in driver memory and is dropped on success, error, or cancellation. A missing supervisor is still contained, and so is an exited one outside this driver's own start window, including after a gateway or driver restart. The workload boundary does not run the agent until the supervisor confirms it, so the window gives the workload nothing it did not already have.
  • Race 2: the supervisor branch of map_podman_event now applies the same previous-exit check (is_previous_exit) to its workload snapshot. A workload exit published after a containment has a new FinishedAt, so it still goes through.

Changes

  • watcher.rs: add the pair-start guard to LifecycleEventFences. inspect_workload takes the fences and treats an in-progress pair start as starting. Split matches_previous_exit into an action check plus is_previous_exit, and apply the latter to supervisor-triggered snapshots.
  • driver.rs: both restart paths in start_sandbox (recovery and fresh credentials) use start_stopped_pair. get_sandbox and list_sandboxes pass the shared fences to inspect_workload.

Testing

  • cargo test -p openshell-driver-podman passes
  • cargo clippy -p openshell-driver-podman --all-targets -- -D warnings is clean; the pre-commit rust:lint hook passed
  • Unit tests added in watcher.rs:
    • restarted_workload_is_not_contained_before_supervisor_start: running workload plus an exited supervisor during a pair start reports starting and issues no stop.
    • exited_supervisor_outside_a_start_stops_workload: the same state outside a start, or during another sandbox's start, is still contained.
    • delayed_supervisor_event_does_not_publish_previous_workload_exit: a supervisor die whose workload snapshot matches the recorded previous exit is dropped.
    • The first and third tests fail when their fix is disabled.
  • Not reproduced under real Podman. The failing conformance test runs in the rust-podman* E2E lanes.

Checklist

  • Follows Conventional Commits
  • Commits are signed off (DCO)

…rvisor start

A sandbox restart starts the workload before its supervisor. Until the
supervisor start returns, the supervisor still reports the exit from the
previous run. A watcher inspect in that window treated it as a lost
supervisor and stopped the new workload with timeout 0, which moved the
sandbox from Starting to Error with ContainerExited.

Mark the workload and supervisor start as in progress, and report the
workload as starting during that window. A missing supervisor, or an
exited one outside a start, is still contained.

Supervisor events also re-inspected the workload without the
previous-exit fence, so a delayed supervisor stop or die from the last
StopSandbox could publish the earlier run's exit. Apply the fence to
those snapshots too.

Signed-off-by: Piotr Mlocek <pmlocek@nvidia.com>
@github-actions github-actions Bot added the state:accepted A maintainer decided OpenShell should pursue this issue label Nov 26, 2026
@pimlock pimlock added the test:e2e Requires end-to-end coverage label Nov 26, 2026

This branch has not been deployed

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

Labels

state:accepted A maintainer decided OpenShell should pursue this issue test:e2e Requires end-to-end coverage

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant