Skip to content

fix: stop pipe and console readers at end of file (CPU spin after builder VM) - #24

Merged
tieuanhquoc merged 1 commit into
masterfrom
fix/eof-spin
Oct 7, 2026
Merged

tieuanhquoc merged 1 commit into
masterfrom
fix/eof-spin

Conversation

@tieuanhquoc

Copy link
Copy Markdown
Collaborator

What & why

DockZ kept a CPU core busy (~75% for 5 hours, Activity Monitor showed 92%) after the disk shrink. A 3 s sample showed the NSFileHandle.fd_monitoring thread looping availableData → read → empty: a handle at end of file stays readable, and SerialExpect (builder VM console reader) never removed its handler, so it spun after the builder VM powered off. Same class in compose output and machine provisioning (stopped only when their process exited).

FileHandle.readChunks(_:onEnd:) is now the single async reader; it removes its handler at EOF. All four call sites use it.

Checklist

  • swift run -c release DockzApp test passes locally (441 checks)
  • No new external Swift dependencies
  • New pure logic has checks in test-runner.swift
  • Verified the affected flow in the running app

How I verified it

  • Tests: the reader delivers every chunk, reports EOF and removes its handler; a source scan fails if any file installs readabilityHandler by hand again.
  • Diagnosis on the affected process: ps 222 CPU-minutes in 5 h; sample stack as above; process started 18:55, builder VM (disk shrink) ran inside it at 19:10. Binary is stripped, so the frames are not symbolised — attribution to SerialExpect is by mechanism and timing.
  • New build installed: idle at ~3% CPU after launch. Not yet re-observed after a builder VM run in the GUI.

🤖 Generated with Claude Code

…lder VM)

A handle at end of file stays readable and availableData returns empty
data each time, so a readabilityHandler that only skips empty chunks is
called in a tight loop. SerialExpect never removed its handler, so after
every builder VM run (disk shrink, image build, machine base build) DockZ
spun on the closed console: seen at ~75% of a core for 5 hours, sampled
in NSFileHandle fd_monitoring -> availableData -> read.

FileHandle.readChunks(_:onEnd:) is now the one way to read asynchronously:
it removes its handler at end of file. SerialExpect, the SSH stderr
collector, compose output and machine provisioning use it (the last two
only stopped when their process exited). Tests: the reader delivers all
data and removes itself at EOF; no source file installs a handler by hand.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@tieuanhquoc
tieuanhquoc merged commit 309f909 into master Oct 7, 2026
3 checks passed
@tieuanhquoc
tieuanhquoc deleted the fix/eof-spin branch October 7, 2026 17:07
@tieuanhquoc tieuanhquoc mentioned this pull request Oct 7, 2026
4 tasks done
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.

1 participant