Pre-connect serial backlog replay stalls after ~one socket fill (~280 B) on an idle guest; delivery only resumes when the guest produces new output

オープン
#8,997 コメント 2 件 リアクション 0 件 担当者 0 名 GitHub で見る

メンテナーはふだん 1 日以内に返信

まだ誰も着手していません。

評価

難易度
4/5
見積もり時間
3〜5日
初心者へのやさしさ
52/100
issue の種類
バグ
明瞭さ
明確に書かれている
活発さ
活発
技術スタック
rust
領域
backend, testing

調査の方向性

Start with serial_buffer/src/lib.rs (SerialBuffer::flush() and SerialBuffer::write()) and vmm/src/serial_manager.rs (SocketConsole::attach_client()); the issue says the equivalent socket code may now be in serial_buffer/src/socket.rs. Trace how a non-blocking write returning EAGAIN is retried, and inspect existing serial-buffer and socket-console tests. Done means a late client can receive the full backlog and then live output even when the guest is idle, with a regression test covering the stall.

索引モデルが issue の本文から書いたものです。

説明

Environment

  • Cloud Hypervisor: v53.0, official static release binary (cloud-hypervisor-static, --version → cloud-hypervisor v53.0; sha256 448af3d4e59b22c2987f7df94c213ad40fb53a10d437e42b5ee6c4fce7c29ecc, matching tag v53.0 = commit 9ed824d6d08df3e96f7d5f50795d9449ac99f431)
  • Host: 16 vCPU AMD EPYC 9554P, 31 GiB RAM, /dev/kvm present, Ubuntu 24.04 (noble), kernel 6.8.0-142-generic, x86_64
  • Guest: Ubuntu noble server cloud image (amd64), 2 vCPU, 1 GiB, single virtio-blk disk, no NIC, rust-hypervisor-firmware 0.5.0
  • The pre-connect buffering feature (#8322) first shipped in v53.0; v52.0 and earlier drop pre-connect output entirely, so this report applies to v53.0 and, by source inspection (see Mechanism), also to current main.

Summary

Since #8322, --serial socket=... buffers output produced before a client connects (1 MiB ring) and replays it on connect. In our testing the buffer itself works — it is lossless, in-order, and holds the full boot stream — but delivery of the backlog to a late-connecting client stalls after roughly one socket-buffer fill (~278–330 B on this host), and only creeps forward when the guest emits new output. The flush loop writes the backlog one byte per write() syscall to the non-blocking client socket, breaks on the first EAGAIN, and reports success; nothing retries the flush until the next guest output byte triggers SerialBuffer::write(). For the canonical use case the feature was built for — reconnecting to an idle, already-booted VM — the client receives ~278 B of stale firmware output and then a silent console: no kernel banner, no live output. Live output is also stuck behind the stalled backlog (in-order, stale-first). A pty-transport control on the same guest replays the full 62 KB backlog healthily, and a --serial file= control captures the complete ~74 KB stream, isolating the defect to the socket flush path.

Reproduction steps (standalone)

Any guest that (a) emits a few tens of KB of serial output while booting and (b) goes quiet once booted will reproduce this. We used an Ubuntu noble cloud image with firmware boot; a direct-kernel-boot guest with console=ttyS0 works equally well.

  1. Start the VM with socket serial and no client connected:
./cloud-hypervisor-static \
  --api-socket /tmp/ch-api.sock \
  --serial socket=/tmp/serial.sock \
  --cpus boot=2 --memory size=1G \
  --disk path=/path/to/guest.img
  1. Wait for the guest to boot fully and go quiet — e.g. 4 minutes after start, with no serial output for the last 60 s (verify the guest actually booted via ch-remote --api-socket /tmp/ch-api.sock vm.info → Running, or by the disk activity settling). On our image the full boot stream is ≈74 KB (firmware ~1.2 KB → GRUB/EFI ~9.5 KB → kernel banner at byte ~11,051 → dmesg replay ~43 KB → systemd to ~62 KB, then a 117–200 B/s trickle that stops once idle).

  2. Connect a poll-style reader (polls at 20 ms — this is the shape of a web-console client that reads on a timer) and count bytes for 60 s:

import socket, time
s = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
s.connect("/tmp/serial.sock")
s.settimeout(0.02)          # 20 ms poll
total, t0 = 0, time.monotonic()
while time.monotonic() - t0 < 60:
    try:
        total += len(s.recv(65536))
    except socket.timeout:
        pass
    print(f"t+{time.monotonic()-t0:5.1f}s  total={total}")
  1. Contrast arms (each on a fresh boot, same settle):
    • Tight reader: replace the reader with a continuously-draining loop (while True: s.recv(1 << 20)) while the guest is still trickling output (connect at ~90 s after boot on our image).
    • pty control: same guest/timing, started with --serial pty instead, opening the pty slave at +90 s.
    • file control: same guest, --serial file=/tmp/serial.log.

Expected / Actual

  • Expected (per #7907/#8322): the late client receives the full buffered boot log (~74 KB on our image) promptly, then live output.
  • Actual (idle guest + 20 ms-poll reader): exactly 278 B delivered, then nothing for the full 60 s. No kernel banner (it sits at byte ~11,051 of the stream). No live output either — the guest was healthy and would have answered input, but nothing further arrives on the socket.
  • Actual (trickling guest + 20 ms-poll reader): a byte-exact prefix of the stream, crawling at ~0.6–1.3 KB/s (e.g. 7,265 B in the first 10 s, cut mid-GRUB), and zero live bytes while attached — the client watches stale backlog.
  • Actual (trickling guest + tight continuously-draining reader): the whole backlog is pushed in the first guest-output-triggered flush session — ~72.7 KB in 0.4 s (75,281 B cumulative at ≈1.8 s; full 86,379 B of backlog + live trickle over the 68 s observation window, ending at the ubuntu login: prompt). The feature works only in this combination: guest producing output and client draining fast.
  • pty control: 62,464 B of backlog replayed, first byte +0.902 s, then live cascade — healthy.
  • file control: 74,228 B by +90 s, banner/systemd/login all present — the guest and the serial device produce the complete stream; nothing is lost before the sink.

Measurements

Late-attach delivery (all on the same image; "settle" = time from VM start before the client connects):

Run Guest output window Client Delivered
E3 boot + 240 s settle, fully quiet guest 20 ms-poll reader, 60 s 278 B, no banner, no live output
E3b-B boot + 90 s settle (trickling) 20 ms-poll reader, 10 s 7,265 B byte-exact prefix, cut mid-GRUB
E3d late1 same 20 ms-poll, 10 s 8,119 B; 0 live bytes while attached; late2 (after detach): 9,288 B
E3e late1/2 same, --seccomp=false 20 ms-poll, 12 s + 8 s 10,314 B then 6,265 B — byte-exact continuation; a keypress adds 4,958 B ending mid-kernel-log

Backlog-delivery regimes (from the strace characterization below):

Guest state / reader Backlog delivery
Quiet (idle VM), any reader one flush session, hard stop at ~278 B
Trickling guest + 20 ms-poll reader ~0.6–1.3 KB/s crawl
Trickling guest + tight continuously-draining reader whole backlog in the first guest-triggered session: ~72.7 KB in 0.4 s

Losslessness proof (E3e): late1 + late2 + post-keypress bytes end at reference stream offset exactly 21,537 = 10,314 + 6,265 + 4,958 — a perfect in-order continuation. The guest was fully booted (it honored an ACPI power-button press in 1.2 s while the delivered stream was still showing kernel-log content from [0.284 s], which a booted guest cannot be emitting live). So: no data loss, no guest stall — delivery of the backlog stalls.

strace on the serial-manager TID (late attach at +90 s, --seccomp=false for ptrace friendliness; defect identical with seccomp on):

accept4(25, ...) = 128                       # client accepted
fcntl(128, F_DUPFD_CLOEXEC) = 129            # writer clone
epoll_ctl(…, EPOLL_CTL_ADD, 128, {EPOLLIN})  # input only — never EPOLLOUT
write(129, "[", 1) = 1                       # flush begins: ONE byte per
write(129, "I", 1) = 1                       #   write() syscall
…  279 one-byte writes in ~21 ms …
write(129, "N", 1) = -1 EAGAIN               # flush loop breaks HERE
( zero syscalls on this thread for the next 68 s — blocked in epoll_wait;
  all subsequent backlog bytes were written by the vCPU thread )

Host-level socket characterization (a plain AF_UNIX stream pair shaped like the accept path, non-blocking, 1-byte sends, SO_SNDBUF = wmem_default = 212,992): a 1-byte-per-send writer gets EAGAIN after exactly 278 bytes — AF_UNIX sender accounting charges each 1-byte skb ~766 B of truesize (212,992 / 278 ≈ 766). Large writes fit 233,152 B. The ~278 B hard stop is therefore an artifact of byte-at-a-time writes against skb truesize accounting, not of the nominal 212 KB SO_SNDBUF.

Mechanism analysis

All file/function references are to the v53.0 tag; the same code is present in current main (the #8687 rework moved SocketConsole from vmm/src/serial_manager.rs into serial_buffer/src/socket.rs but did not change the flush or retry semantics described below).

Proven from source:

  1. vmm/src/serial_manager.rs, SocketConsole::attach_client() — on accept it calls buffer.set_out(writer), sets write_out = true, and calls buffer.flush() once. The accepted socket is registered with epoll for EPOLLIN only (epoll_ctl(EPOLL_CTL_ADD, …, EPOLLIN)); there is no EPOLLOUT (writable) watch anywhere, and no timer that re-attempts the flush.
  2. serial_buffer/src/lib.rs, SerialBuffer::flush() — the replay loop pops one byte at a time (while let Some(byte) = self.buffer.pop_front() { self.out.write_all(&[byte]) … }), i.e. one 1-byte write() syscall per backlog byte. On any error — including EAGAIN from the O_NONBLOCK client socket — it pushes the byte back and breaks. It then returns self.out.flush(), which is Ok: the EAGAIN is never surfaced anywhere; the flush reports success despite an almost-fully-undelivered backlog.
  3. SerialBuffer::write() — the device's output sink (shared with the serial device via SharedSerialBuffer): every new guest byte first retries self.flush(). This is the only retry path. Backlog delivery rate is therefore governed by guest output rate × client drain rate:
    • quiet guest → one flush session at attach, hard stop at ~one socket fill (~278–330 B of 1-byte skbs on this host);
    • trickling guest + slow reader → crawl;
    • trickling guest + tight reader → the first guest-triggered session drains everything.
  4. Live output is queued behind the stalled backlog (the ring is strictly in-order): during a late-attached ACPI shutdown, our client received 3-minute-old kernel-log lines while the actual shutdown cascade was being produced.

Characterization (host-level, measured): the exact stall byte count (~278) is the AF_UNIX truesize arithmetic above and will vary with wmem_default; the stall itself is host-independent — it needs only a non-blocking sink, a break-on-first-error flush, and no retry path.

Test gap (why #8322's verification passed): the PR's end-to-end verification used a guest that panics (VFS: Unable to mount root fs) — a panicking kernel printk's continuously, which continuously retriggers the flush — together with a draining reader: exactly the combination that masks the stall. The detached-through-real-boot + idle-guest scenario was not covered.

Suggested fix direction (suggestion only)

  1. Retry-driven flush: register the accepted client socket for EPOLLOUT (or otherwise re-arm the flush on writability / a short timer with backoff) so the backlog drains at socket speed, independent of guest output rate. This is the structural fix.
  2. Batch the flush: pop and write contiguous runs instead of one byte per write_all() — besides the syscall-count reduction, per the characterization above a large write fits ~233 KB where 1-byte writes fit ~278 B, so batching alone raises the effective one-shot capacity by ~3 orders of magnitude. Batching without a retry path is still incomplete (a slow client will eventually re-fill the socket).
  3. At minimum: surface the partial flush (flush() currently returns Ok with an undelivered backlog) and document the drain-rate contract for consumers of the replayed history.

Related but distinct: we have separately observed that a serial client that dies with unread data in flight silently kills the serial-manager thread at v53.0 (the console then stays dead for the lifetime of the VMM process). That is a different defect in the same code path and is reported separately.

References

  • #8322 — "vmm: buffer socket serial output for late-connecting clients" (merged 2026-06-08, first released in v53.0) — the feature whose replay path stalls
  • #7907 — "Add ring buffer support for Socket serial/console mode" (closed by #8322) — the original request, which describes the late-attach use case this regression defeats
  • #7502 — serial-manager fixes at v53 (pty initial flush, socket fd leak); confirms the surrounding paths were recently reworked
  • #8687 — post-v53.0 rework moving SocketConsole into serial_buffer; the flush semantics analyzed above are unchanged in it
  • Historical lineage of the flush-on-write design: #3058 ("Serial buffer not flushed until write", 2021), #3004 ("Serial port buffering stalls guest boot", 2021) — pty-era manifestations of the same write-triggered-flush behavior
  • #7974 — open follow-up "Serial/console socket connection should persist across VM reboot", which builds on this buffering machinery and would inherit the stall

Found during a downstream re-qualification of the v53.0 release (static binary, digests above). All measurements above are from that campaign.

主要言語
Rust
スター
6.3k
フォーク
786
平均マージ
1日 3時間
マージ済み PR(30日)
195

環境構築

はじめの一歩

  1. issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
  2. 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
  3. リポジトリをフォークし、ブランチを切って変更します。
  4. issue 番号を参照したプルリクエストを送ります。

cloud-hypervisor/cloud-hypervisor のほかの issue

cloud-hypervisor/cloud-hypervisor の issue をすべて見る

似ている issue

Rust の issue をもっと見る

新しい issue をメールで受け取る

初心者向けの GitHub issue を短くまとめたダイジェスト。