Skip to content

fix(check): close the phase-resync goroutine leak + its data race - #75

Merged
atrawog merged 3 commits into
mainfrom
feat/fix-check-runner-phaseresync-race
Jul 12, 2026
Merged

atrawog merged 3 commits into
mainfrom
feat/fix-check-runner-phaseresync-race

Conversation

@atrawog

@atrawog atrawog commented Jul 12, 2026

Copy link
Copy Markdown
Contributor

Root cause (a real production defect, not only a test race)

runWithPhaseResync (charly/check_runner_cmd.go) — the in-pod iterate
orchestrator's per-phase credential-resync driver — spawns a resync goroutine at
every phase boundary it scans on the harness subprocess's stderr, then returns via
cmd.Wait(). cmd.Wait() blocks on the SUBPROCESS, not the goroutines — so a
resync goroutine, and the credential podman cp its phaseResyncFn performs, could
outlive the run's return (a leaked goroutine + in-flight side-effect). A -race
run of the check-runner tests caught the same leak as a data race: the leaked
goroutine reads the phaseResyncFn package-var while the test's
defer phaseResyncFn = origFn restore writes it.

Fix — root-cause synchronization (no -race suppression)

  • sync.WaitGroup barrier: every boundary goroutine is Add/Done-tracked, and
    runWithPhaseResync calls wg.Wait() after cmd.Wait() before returning — no
    resync goroutine (nor its phaseResyncFn read) outlives the return. Closes both the
    goroutine leak and the read/write race at the root.
  • Second race fixed (surfaced under -race once the first closed): the boundary
    test's mock wrote a shared seenScore from all N concurrent resync goroutines →
    guarded with a mutex.
  • R4 bandaid deleted: the failure-path test's time.Sleep(100ms) "let the goroutine
    finish" hack is removed — the WaitGroup barrier is the real primitive it approximated.

Verification (R10 — go-test gate for this class, no bed)

  • go test -race -run 'TestRunWithPhaseResync|TestHarnessPhaseRe' ./charly/ → 0 races (was 2)
  • full go test ./charly/... → green
  • go build ./... + golangci-lint run (0 issues) + gofmt clean

(The full -race -count>1 stress stays gated on the provider-registry test-isolation
seam, tracked separately; single-test -race is the correct gate here.)

Peeled from the P12 check-externalization worktree as its own immediate cutover (it's a
real production fix affecting main now + clears the team-wide -race noise); P12
rebases onto it.

Assisted-by: Claude (fully tested and validated)

https://claude.ai/code/session_01QsHyDfHPTARnrmNZiw1ABX

runWithPhaseResync spawned a per-boundary credential-resync goroutine but
returned via cmd.Wait() without awaiting them, so a resync goroutine (and its
credential `podman cp`) could outlive the run — a real production goroutine
leak the -race detector also caught as a phaseResyncFn read/write race.

Root-cause synchronization (no -race suppression): a sync.WaitGroup barrier so
no resync goroutine outlives the return; a mutex around the boundary test's
concurrent seenScore writes (a second race surfaced once the first closed); and
deletion of the failure-path test's time.Sleep(100ms) R4 bandaid (the WaitGroup
is the real primitive it approximated).

Gate: go test -race -run 'TestRunWithPhaseResync|TestHarnessPhaseRe' (0 races,
was 2) + full go test ./charly/... + build + golangci-lint (0) + gofmt.

Assisted-by: Claude (fully tested and validated)
Claude-Session: https://claude.ai/code/session_01QsHyDfHPTARnrmNZiw1ABX
@atrawog

atrawog commented Jul 12, 2026

Copy link
Copy Markdown
Contributor Author

pr-validator — APPROVED ✅

Change class: charly/ Go code (concurrency / goroutine-lifecycle fix + test)

I re-derived this verdict independently (fresh context), re-running every gate against the feat head merged with current main (the actual landing state) in an isolated worktree.

Independent re-run evidence

Gate Result
gofmt -l . empty
go build ./... OK
go vet ./... OK
go test -race -run 'TestRunWithPhaseResync|TestHarnessPhaseRe' ./ PASS, 0 races (count=1)
same, -count=10 PASS, 0 races
pre-fix (2 code files reverted to parent), same -race WARNING: DATA RACE, FAIL
go test ./... (full) ok (45.9s)
golangci-lint run ./... 0 issues

The pre-fix run confirms the test genuinely catches the defect (R10 check-coverage: it FAILS without the fix, PASSES with it).

Checklist

  • 0. Security (T1–T4): PASS — 3 files only (CHANGELOG + 2 code); no go.mod/go.sum/vendor/hooks/branch-protection/settings/CLAUDE.md/validator-spec touched; scope matches the description exactly; no embedded instructions.
  • 2. Change-class gate: PASS — the go test -race gate is the authoritative gate for a data race. A bed runs a non--race-instrumented binary and cannot observe the race; the leaked goroutine is invisible at process exit. A bed literally cannot fail on this change, so it is correctly not run.
  • 3. Attribution tier (fully tested and validated): PASS, not inflated — the class gate passed in full and the changed code path executed live under the race detector.
  • 5. R1 / zero-warnings: PASS — genuine RCA (cmd.Wait() blocks on the subprocess, not the goroutines); lint 0, vet clean; no flake/transient framing.
  • 6. R2 no split: PASS — peeled from the P12 worktree as its own immediate cutover; the -count>1 full-suite stress is a genuinely separate provider-registry seam, already tracked/landed.
  • 8. R4 (the core check): PASS — a genuine sync.WaitGroup: Add(1) before the goroutine spawn, defer Done() inside, Wait() after cmd.Wait() — a real happens-before barrier (the scan loop is synchronous, so all Adds precede Wait). Plus a sync.Mutex around the shared seenScore write and read. The deleted time.Sleep(100ms) was a timing bandaid the barrier correctly replaces. No //nolint, no -race suppression, no t.Skip, no serialize-to-hide.
  • 9. R5: PASS — no transitional/dual-mode path; the sleep is fully deleted; no removed identifier to sweep.
  • 13. Concurrency + anti-cheat: PASS — this is a race local to runWithPhaseResync's own resync goroutines, not a shared-state deploy path, so the concurrent-roster mandate does not apply; the correct gate (go test -race, run at count=1 and count=10) was used, with a root-cause RCA and zero forbidden framing.
  • 14. Atomic: PASS — exactly one commit on feat; no Phase-2/TODO.
  • 15. Kernel boundary: PASS — a fix to an existing core helper; no new capability, no spec.<Kind> leak.
  • 10/12/16 [N/A]: no artifact/label/binary/OS-dep change; no CUE/generated/wire/plugin-params surface; no destroy/rebuild.
  • 18. CHANGELOG: PASS — placeholder present, own-narrative RCA, H1 matches filename (finalized to the merge-time CalVer).

Decision: This is a correct, complete, root-cause fix for a real production goroutine leak + its data race. The synchronization is textbook-correct, the test proves the defect is caught, and every Go gate re-ran green on the merged landing state. Approving and finalizing the merge-time CalVer.

Assisted-by: Claude (analysed on a live system)

atrawog and others added 2 commits July 12, 2026 18:10
Version-stamp commit (pr-validator): rename the placeholder CHANGELOG to the
merge-time CalVer and align its H1. Folded into the squash-merge of #75.

Assisted-by: Claude (fully tested and validated)
Claude-Session: https://claude.ai/code/session_017QzXFEfS9HFXZrU2b2Xokh
@atrawog
atrawog merged commit ff112b2 into main Jul 12, 2026
@atrawog
atrawog deleted the feat/fix-check-runner-phaseresync-race branch July 12, 2026 16:12
atrawog added a commit that referenced this pull request Jul 12, 2026
Docs-only follow-up to #75: the CalVer finalization renamed the placeholder changelog but left its H1 at the placeholder date; this aligns the H1 (# 2026.193.1611) to the filename. Assisted-by: Claude (documentation reviewed)
atrawog added a commit that referenced this pull request Jul 14, 2026
Rename the placeholder CHANGELOG to the merge-time CalVer and align its H1.
Superproject leg of the check/bed-correctness batch (#66/#71/#75).

Assisted-by: Claude (fully tested and validated)
atrawog added a commit that referenced this pull request Jul 14, 2026
…ks + module-key validation fix

The check/bed correctness batch — three pre-existing bed/validation defects landed as
one atomic superproject cutover (producer legs merged first: sdk#47 image_tag wire
field + plugins#55 agents-SKILL R1 correction).

#75 — bed-scoped fixture image tags (the shared-tag concurrency collision class): a
per-run <bed-root>-<runCalver> tag threaded via spec.CheckBedReply.ImageTag, passed as
--tag on every build + deploy, with resolveNodeOverlays seeding node.Version and the
k8s preresolver honoring it (mirroring build_overlay's tag!="" branch). Retention-safe
(same image name → same label group).

#66 — debootstrap-builder baked checks: qemu-img version scalar-equals → contains
matcher (R4); grub-install check kept (correct) + the missing grub2-common added
(provides /usr/sbin/grub-install, dropped by --no-install-recommends).

#71 — validatePkgConfig module/modules Raw-key mismatch: Raw["modules"]→Raw["module"]
(the singular authored key), making the rule reachable; discriminating test + guard.

sdk gitlink → merged 5ad481d (v0.2026195.742); plugins gitlink → merged 7075eda
(v2026.195.0759). Validated independently by a fresh pr-validator: delta re-gate green
after update-branch (golangci-0 + tests on all 3 modules), forward-gitlinks verified,
#75 concurrent-pair R10 corroborated.

Assisted-by: Claude (fully tested and validated)
atrawog added a commit that referenced this pull request Jul 14, 2026
#77, #76)

vm-lifecycle-correctness thematic batch — two non-blocking correctness fixes, one squash.

#77 — `charly vm stop` backend-mismatch false-success (the stop sibling of #69's
vm-destroy). Extract `vmHolder` (the authoritative "which backend ACTUALLY holds this
domain" probe) shared with #69's `destroyVmDomain` (R3 — ONE shared abstraction, never
a duplicate); `destroyVmDomain` is refactored to use it byte-equivalently. Add `stopVmDomain`
(probe + stop + poll-for-shutoff verify — the same `pollUntil`/`StopGate` primitive
`gracefulStopDomain` uses for destroy). Rewrite `charly vm stop` to use it; a nonexistent VM
errors "no such VM" (mirroring #69 destroy) instead of false-success. The libvirt stop is an
ASYNC ACPI poweroff, so the verify POLLS for the domain to leave `DomainRunning` (exactly
what `vm list -a` reports as "running") rather than re-probing immediately (which
false-positives mid-shutdown — confirmed live); only a guest still running after the grace
errors, pointing at `charly vm stop --force`. The core `stopVM` (arbiter `holderStop`) is the
same class in a separate code path — registered for the next vm-lifecycle cutover
(cross-package kit unification of the duplicated stop/qemu/vmDir helpers).

#76 — the out-of-tree plugin build cache (`~/.cache/charly/plugins/<name>`) was word-keyed
shared-mutable state: two worktrees building the same plugin word raced the one `go build -o`
output (the #75 shared-image-tag collision class). Scope the cache path by source dir
(`pluginSourceTag`, a 16-hex digest of the absolute srcDir — always-rebuild, no cache-hit
skip, so a dependency bump never serves a stale binary) + atomic publish (build to
`bin.build` under the per-binary lock, then `os.Rename`) so a reader never sees a
half-written binary.

R10: `check-charly-vm` bed exit 0 / 7 steps ok (exercised `vmHolder` via the destroy teardown
+ #76 plugin builds with the new source-scoped cache paths — the `<tag>` suffix in every
built plugin path is `pluginSourceTag`); live `charly vm stop` on a disposable SSH-ready
`charly-vm` -> "Stopped VM" + verified `shut off` (rc=0); unit tests (`vm_stop_test.go`,
`plugin_build_cache_test.go`) + golangci-lint 0 (charly / plugin-vm) + `task build:binary`.

Assisted-by: Claude (fully tested and validated)
atrawog added a commit that referenced this pull request Aug 17, 2026
plugins#198 merged as da76ec0f and was tagged v2026.229.2154, after seven
rounds. The pin moves 0e6351c (my branch head) -> da76ec0f (the squash on
plugins/main).

Re-derived from plugins/origin/main, NOT the PR head: the CalVer commit and the
squash both move the ref. The ancestry gate was fed the unmerged 0e6351c first
and correctly rejected it.

docs remains at b3dfd1e deliberately -- docs#73 merged, and the round-7 fixes
that landed on the re-created branch afterwards are now docs#75, still open.
That pin gets the same re-derivation when #75 lands, and this PR must not merge
before then.

Assisted-by: Claude Code Anthropic Claude Opus 5 (documentation reviewed)
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