Skip to content

fix(check): serialize sessionHandle lifecycle transitions (session_service -race) - #38

Merged
github-actions[bot] merged 3 commits into
mainfrom
fix/session-handle-mutex
Sep 7, 2026
Merged

github-actions[bot] merged 3 commits into
mainfrom
fix/session-handle-mutex

Conversation

@atrawog

@atrawog atrawog commented Sep 7, 2026 •

Copy link
Copy Markdown
Contributor

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: the
sessionHandle struct is shared mutable state between the reaper goroutine
(spawnSetsidProcess's background cmd.Wait() + stop transition), the stop
path (stopSession) and the persisted write (writeSessionHandle marshal) —
the -race report pair is session_service.go:243 (reaper writes h.Status /
h.StoppedAt) vs :310 (stopSession reads the same fields) with
writeSessionHandle's whole-struct marshal (:298) in between. A 30s
recorder dies the moment stopSession SIGTERMs it, so the reaper fires DURING
the 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.mu guards Status/StoppedAt/Transport/PID/Unit + the JSON
    write (writeSessionHandle now locks; persistLocked writes under the
    held lock — one consistent snapshot, never a torn marshal).
  • markStopped() = locked transition + persist (idempotent; the reaper and
    stopSession may fire concurrently).
  • isStopped() / snapshot() = locked reads (SessionLiveness and the
    lifecycle test no longer read raw fields off a handle a goroutine may be
    transitioning).
  • Call sites: the reaper goroutine and stopSession now both go through
    markStopped; SessionLiveness reads through snapshot().

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:64 errcheck — os.WriteFile return unchecked
    in a fixture; assign to _.
  • bed_debug_hint_test.go:28 errcheck — w.Close() unchecked; assign to
    _.
  • instrument.go:543 errcheck — defer f.Close() unchecked; wrap in a
    defer func(){ _ = f.Close() }().
  • bed_autoprovision_retry_test.go:10 S1028 — errors.New(fmt.Sprintf(...))
    → fmt.Errorf(...).
  • steps_file_test.go:33 QF1008 — embedded-field selector Plan[0].Op.ID →
    Plan[0].ID.
  • vm_port_forward_env_test.go:33 gofmt — gofmt -w.

How tested

Before (main @ 68d4bf9, session_service.go unmodified):

$ go test ./... -count=1 -race
--- FAIL: TestSpawnSessionSetsidLifecycle (0.05s)   [race detected]
--- FAIL: TestOrphanSweepReapsStaleSession (0.05s)  [race detected]
--- FAIL: TestSweepToleratesHandlelessStateDir (0.05s) [race detected]
--- FAIL: TestFinalizeSessionDir (0.05s)            [race detected]
--- FAIL: TestSpawnSessionStopEscalates (0.05s)     [race detected]
FAIL

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):

$ go test ./... -count=1 -race
ok  	github.com/opencharly/plugin-check/candy/plugin-check	1.758s
?   	github.com/opencharly/plugin-check/candy/plugin-check/cmd/serve	[no test files]
$ go build ./...    # PASS
$ go vet ./...      # PASS (zero findings)
$ gofmt -l .        # empty (zero files unformatted)
$ golangci-lint run ./...   # 0 issues (the 6 surfaced findings are fixed in-tree)

Rulebook compliance

  • R1: full RCA recorded for the race — shared-struct race on sessionHandle
    lifecycle fields between the reaper goroutine, stopSession and the
    persisted 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).
  • R3: one canonical transition (markStopped) and one canonical write
    (persistLocked) serve every caller — no duplicated locking patterns.
  • R4: no workarounds, no sleeps, no retries.
  • R6: PR-only, no force-push, no direct main push.
  • R7: full -race suite + build + vet + gofmt + golangci-lint pasted above.

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)

@github-actions

github-actions Bot commented Sep 7, 2026

Copy link
Copy Markdown

Review — BLOCK

Head SHA: 8a9b967476e9 (branch: plugin-check)

Change class: code change — Go concurrency bug fix in candy/plugin-check/session_service.go + test.

Security screen: PASS — no scope mismatch (diff matches description exactly), no secrets/exfiltration, no weakened guardrails, no supply-chain changes.

Blocks

  1. B14(b)/R2 — forbidden "pre-existing" framing. The body surfaces findings and dismisses them with the catalogued parking phrase without naming an exit or providing RCA:

    • "The vm_port_forward_env_test.go gofmt finding is pre-existing (present with my files stashed) and untouched."
    • "the 6 reported issues are all pre-existing in other files" (golangci-lint).

    Under B14(b), a parking phrase ("pre-existing") is ITSELF a finding unless it names an exit THIS PR advances; under R1 every warning needs an RCA. Neither is provided — no named thematic batch, no root cause. The author must either fix these in-tree or route each to a NAMED thematic batch cutover, and supply an RCA for each surfaced warning. (The race-condition fix itself is correctly handled in-tree — that part is fine — but the two "pre-existing" dismissals are the forbidden R2 split.)

Notes (non-blocking, for completeness)

  • The mutex fix is technically sound: markStopped/persistLocked/isStopped/snapshot all lock correctly, no double-lock/deadlock, persistLocked is the non-locking variant called under held lock. Placement is CORRECT (candy-layer capability fix). One atomic commit. Attribution tier "fully tested and validated" is justified by the live -race run of the changed path.

Verdict: BLOCK


ollama/deepseek-v4-flash — pi-review-action.

View action run

…x3, S1028, QF1008, gofmt) — RCA: all pre-existing mechanical issues in the same package, fixed here rather than parked
@github-actions

github-actions Bot commented Sep 7, 2026

Copy link
Copy Markdown

Review — BLOCK

Head SHA: ac20ee976d7f (branch: plugin-check)

Change class: code change — Go concurrency bug fix in candy/plugin-check/session_service.go + tests + in-tree lint fixes.

Security screen: PASS — no scope mismatch, no secrets/exfiltration, no weakened guardrails, no supply-chain changes.

Blocks

  1. Body-truthfulness (rule 4 / A1) — stale "pre-existing" claims contradict the CURRENT diff. The current body asserts two things that the current diff directly contradicts:

    • "The vm_port_forward_env_test.go gofmt finding is pre-existing (present with my files stashed) and untouched." — FALSE. The current diff contains the gofmt alignment changes to vm_port_forward_env_test.go (commit ac20ee976d7f).
    • "golangci-lint run ./... # ZERO findings in the changed files (the 6 reported issues are all pre-existing in other files)" — FALSE. The current diff fixes all six surfaced findings in the changed files: errcheck ×3 (bed_autoprovision_test.go, bed_debug_hint_test.go, instrument.go), S1028 (bed_autoprovision_retry_test.go), QF1008 (steps_file_test.go), and gofmt (vm_port_forward_env_test.go).

    The second commit resolved the prior review's B14(b) finding in-tree — good — but the body was never updated to reflect that, so it still carries the forbidden "pre-existing / untouched" parking framing that the diff itself refutes. The author must update the body's Summary and How-tested sections to state that all surfaced lint findings were fixed in-tree (naming each), removing the stale parking phrases.

Notes (non-blocking)

  • The mutex fix is technically sound: markStopped/persistLocked/isStopped/snapshot lock correctly with no double-lock/deadlock; persistLocked is the non-locking variant called under held lock. Placement is CORRECT (candy-layer capability fix). One atomic change. Attribution tier "fully tested and validated" is justified by the live -race run of the changed path; Assisted-by: line is present in the correct format.

Verdict: BLOCK


ollama/deepseek-v4-flash — pi-review-action.

View action run

@github-actions

github-actions Bot commented Sep 7, 2026

Copy link
Copy Markdown

Review — PASS

Head SHA: 99c3f717e6a5 (branch: plugin-check)

Security screen: PASS
Checklist: All applicable rules PASS or NA.

Comment intake: Both prior github-actions[bot] BLOCKs (B14(b) "pre-existing" framing at 8a9b967; body-truthfulness stale claims at ac20ee) are stale-superseded — the current body (99c3f71) explicitly states all six lint findings are fixed in-tree with per-finding RCA and no parking framing, and the current diff confirms all six fixes (errcheck ×3, S1028, QF1008, gofmt) are present in the changed files.

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.

View action run

@github-actions
github-actions Bot enabled auto-merge (squash) September 7, 2026 20:53
@github-actions
github-actions Bot merged commit 836e682 into main Sep 7, 2026
2 checks passed
charly-auto-merge Bot added a commit that referenced this pull request Sep 7, 2026
@aitrawog
aitrawog deleted the fix/session-handle-mutex branch September 25, 2026 19:22
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