Repository navigation
fix(check): serialize sessionHandle lifecycle transitions (session_service -race) - #38
Conversation
Review — BLOCKHead SHA: Change class: code change — Go concurrency bug fix in Security screen: PASS — no scope mismatch (diff matches description exactly), no secrets/exfiltration, no weakened guardrails, no supply-chain changes. Blocks
Notes (non-blocking, for completeness)
Verdict: BLOCK ollama/deepseek-v4-flash — pi-review-action. |
…x3, S1028, QF1008, gofmt) — RCA: all pre-existing mechanical issues in the same package, fixed here rather than parked
Review — BLOCKHead SHA: Change class: code change — Go concurrency bug fix in Security screen: PASS — no scope mismatch, no secrets/exfiltration, no weakened guardrails, no supply-chain changes. Blocks
Notes (non-blocking)
Verdict: BLOCK ollama/deepseek-v4-flash — pi-review-action. |
…fixed in-tree, no parking framing)
Review — PASSHead SHA: Security screen: PASS Comment intake: Both prior Placement: CORRECT — candy-layer concurrency fix; no core/sdk movement, no alias files, no new sdk-kit imports. Verdict: PASS ollama/deepseek-v4-flash — pi-review-action. |
Summary
The session_service -race run is RED: five session tests fail under -race on main:
TestSpawnSessionSetsidLifecycle,TestOrphanSweepReapsStaleSession,TestSweepToleratesHandlelessStateDir,TestFinalizeSessionDir,TestSpawnSessionStopEscalates. All five share ONE root cause: thesessionHandlestruct is shared mutable state between the reaper goroutine(
spawnSetsidProcess's backgroundcmd.Wait()+ stop transition), the stoppath (
stopSession) and the persisted write (writeSessionHandlemarshal) —the -race report pair is
session_service.go:243(reaper writesh.Status/h.StoppedAt) vs:310(stopSessionreads the same fields) withwriteSessionHandle's whole-struct marshal (:298) in between. A 30srecorder dies the moment
stopSessionSIGTERMs it, so the reaper fires DURINGthe stop — the race is not theoretical.
Fix (RCA → root fix): serialize every lifecycle-field access and every
persisted write on the handle's own
sync.Mutex:sessionHandle.muguards Status/StoppedAt/Transport/PID/Unit + the JSONwrite (
writeSessionHandlenow locks;persistLockedwrites under theheld lock — one consistent snapshot, never a torn marshal).
markStopped()= locked transition + persist (idempotent; the reaper andstopSessionmay fire concurrently).isStopped()/snapshot()= locked reads (SessionLiveness and thelifecycle test no longer read raw fields off a handle a goroutine may be
transitioning).
stopSessionnow both go throughmarkStopped;SessionLivenessreads throughsnapshot().No behavior change: same transitions, same persisted handle.json shape (mu is
unexported, excluded from the JSON).
Lint findings surfaced by the validator are FIXED IN-TREE in the same PR
(RCA: all six are mechanical issues in this package, each with a one-line root
fix — none need a thematic batch when the fix is landing here anyway):
bed_autoprovision_test.go:64errcheck —os.WriteFilereturn uncheckedin a fixture; assign to
_.bed_debug_hint_test.go:28errcheck —w.Close()unchecked; assign to_.instrument.go:543errcheck —defer f.Close()unchecked; wrap in adefer func(){ _ = f.Close() }().bed_autoprovision_retry_test.go:10S1028 —errors.New(fmt.Sprintf(...))→
fmt.Errorf(...).steps_file_test.go:33QF1008 — embedded-field selectorPlan[0].Op.ID→Plan[0].ID.vm_port_forward_env_test.go:33gofmt —gofmt -w.How tested
Before (main @ 68d4bf9, session_service.go unmodified):
Race pair (representative, from -race output):
Write at session_service.go:243 (reaper Status write) ... Previous read at session_service.go:310 (stopSession Status read).After (this branch, from the final committed tree):
Rulebook compliance
sessionHandlelifecycle fields between the reaper goroutine,
stopSessionand thepersisted marshal; root fix = handle-owned mutex over every transition +
write. The six lint findings surfaced by the validator each carry their own
one-line RCA and are fixed in-tree in this PR — not parked, no "pre-existing
baseline" framing (strict-policy B14).
markStopped) and one canonical write(
persistLocked) serve every caller — no duplicated locking patterns.Change classification
Class: bug fix (concurrency). Gate: go test -race green (the exact gate the
validator's B5/B14 finding demands). Attribution: fully tested and validated —
the changed path (recorder spawn → concurrent stop/reaper) ran live under -race
in this PR's own test run.
Assisted-by: pi openrouter/deepseek/deepseek-v4-flash-0731 (fully tested and validated)