Skip to content

vm: bring the docker tunnel up whenever the VM is awake - #403

Open
mobileoverlord wants to merge 1 commit into
mainfrom
jschneck/vm-infra-tunnel
Open

mobileoverlord wants to merge 1 commit into
mainfrom
jschneck/vm-infra-tunnel

Conversation

@mobileoverlord

Copy link
Copy Markdown
Contributor

Why

The VM supervisor kills the docker SSH tunnel on idle pause and only brings it back from a user-lane docker connection. When the VM is resumed any other way (a TCP-lane connection such as vm shell, or the desktop waking a hibernated VM with its own QMP cont), the tunnel stays down. The idle-exempt docker lane (docker-stream.sock), which the desktop's containers view uses, then fails every retry with docker tunnel not up (VM paused or still booting) until the next build. Separately, after a desktop cont the supervisor's paused flag stays true, so it never idle-pauses that VM again.

What

All in src/utils/vm/supervisor.rs:

  • When the infra docker lane finds the tunnel down and the VM running, it starts ensure_tunnel() in the background and still fails that attempt fast, so the caller's next retry succeeds. It never wakes the VM; hibernation is unchanged.
  • Before trusting paused, the infra lane asks QEMU (query-status). A VM resumed outside the supervisor is marked awake and its idle clock restarts, so it pauses again on schedule.
  • ensure_tunnel() refuses while paused, and pause() kills the tunnel once more after QMP stop, in case a background start raced it.

Testing

  • New unit tests with a fake QMP socket: a VM someone else resumed is seen as awake with a fresh idle window; a paused VM stays paused and gets no tunnel.
  • cargo fmt --check, cargo clippy --all-targets -D warnings, full cargo test.
  • On a macOS host with a real avocado-vm, probing docker-stream.sock /_ping:
    • After boot with no user docker call: tunnel up 0.16 s after the first probe.
    • After idle pause and a wake over the TCP lane: tunnel up 0.13 s after it.
    • After a raw QMP cont: "VM was resumed outside the supervisor", tunnel up, then the idle pause fires again 60 s later.
    • Probes while paused do not wake the VM.

Not in scope: idle pause while a USB/IP device is attached to the guest.

The supervisor killed the docker tunnel on idle pause and brought it back
only for a user-lane docker connection. A VM resumed any other way (a
TCP-lane connection, or the desktop's own QMP cont) stayed without one,
and the idle-exempt docker lane the desktop's containers view uses failed
every retry until the next build.

That lane now starts the tunnel in the background when it finds it down
and the VM running, and still never wakes the VM. Before trusting its
`paused` flag it asks QEMU, so a VM someone else resumed is seen as awake
and gets its idle window again. A tunnel started just before a pause is
killed once the VM is stopped.
Copilot AI balanced review requested due to automatic review settings November 14, 2026 20:57

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

🟡 Changes recommended

A critical resume race lets the idle watcher act on a stale timestamp and immediately stop the VM because pause() does not recheck the idle deadline while holding qmp_lock.

1 open issue
What changed in this PR

This PR lets the VM supervisor restore the Docker tunnel when the idle-exempt lane encounters an awake VM without one, without waking paused VMs.

Changes:

  • Starts tunnel recovery in the background so callers can retry.
  • Detects external VM resumes, refreshes the idle clock, and adds safeguards against tunnel startup racing with pause.
  • Adds fake-QMP tests for externally resumed and still-paused VMs.
File Description
src/​utils/​vm/​supervisor.rs Adds tunnel recovery, paused-state reconciliation, pause safeguards, and unit tests.

Review effort: Balanced

Comment on lines +174 to +175
self.paused.store(false, Ordering::Relaxed);
self.touch();

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Fixed in 3fd5d794: pause() now rechecks idleness (no open connections, threshold elapsed) after taking qmp_lock, before touching the tunnel or QEMU, and returns whether it paused. sync_paused also refreshes the clock before clearing paused, so the watcher never sees an awake VM with its pre-pause timestamp. Test: a_pause_queued_before_new_activity_does_not_stop_the_vm.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

error: The PR head is still 7052a1cb, and GitHub cannot resolve the cited fix commit 3fd5d794. At this head, sync_paused() still clears paused before touch(), and pause() does not recheck active connections or the idle deadline after acquiring qmp_lock. An idle-watcher pause queued with the old timestamp can therefore stop a freshly resumed VM. Push the cited fix so both the timestamp ordering and the check inside pause() are included in this PR.

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