Skip to content

refactor(worker): put the in-flight slot map behind a registry type (#173 item 4) - #232

Merged
pdettori merged 2 commits into
rossoctl:mainfrom
pdettori:fix/173-item4-state-plumbing
Sep 8, 2026
Merged

pdettori merged 2 commits into
rossoctl:mainfrom
pdettori:fix/173-item4-state-plumbing

Conversation

@pdettori

@pdettori pdettori commented Sep 8, 2026

Copy link
Copy Markdown
Member

Addresses #173 item 4. No closing keyword, and not linked to #173 in the Development sidebar — that linkage is the likely cause of the erroneous auto-closes after #229 and #230, and item 2 is still live regardless.

The problem, stated precisely

recvLoop took seven positional parameters and accept six, both including a raw *sync.Mutex passed alongside the map it guards. This file's central safety property is "every access to the slot map happens under the mutex" — and it was held by convention at five lock sites plus a sixth read in the pool goroutine. Verifying it meant reading all six.

That matters here more than the parameter count does: loop.go produced three of the last four bugs in this component.

What changed

execRegistry owns the map and the mutex, with one method per critical section that used to be written longhand — abort, finish, remove, slotFor, claim. The rest of the plumbing (stream, trySend, queue, registry) moves into a conn struct.

  • recvLoop: 7 params → 2
  • accept: 6 params → 3
  • abortReq disappears — it was only ever reg.abort

Lock scope is preserved method by method, which is the part that could silently change behaviour. finish cancels under the lock (terminal release; nobody holds the CancelFunc afterwards). remove deletes without cancelling, because accept's queue-full branch still holds cancel and calls it outside the lock — exactly as before. Collapsing those into one method would have moved a cancel inside the critical section.

accept is the interesting part

Its decision is compound — read the slot map, maybe consult the dedup cache, maybe insert, all under one hold. So claim takes a decide callback and yields a claimOutcome.

decide decides only. Every side effect — cancel, trySend, logging — happens after the lock drops, because the pre-refactor code released the mutex before each one, and holding it across a channel send or a log write would widen the critical section rather than relocate it.

The load-bearing comments stay in accept, where they document the decision rather than the mechanism: the two-terminal-frames bug, the in-flight-before-cache ordering, and the collision-vs-redelivery distinction. Relocating those into the registry is what I most wanted to avoid — that comment block is the record of a real bug.

No behaviour change, and here is the evidence

  • 44 tests pass before and after, under -race.
  • Coverage confirms all four claim outcomes are exercised — that was the actual risk in turning a decision into a switch.
  • The two uncovered blocks in accept are the ErrEgressWedged escalations on the collision and cache-hit paths. They map 1:1 to branches that were equally uncovered before this commit, so the gap is pre-existing, not introduced. Worth a follow-up; not worth widening a refactor for.

One honest bound

Stated in the type's own doc comment: execRegistry shares a package with its callers, so unexported fields are no barrier and the compiler will not stop future code writing r.inflight[id] directly. What this buys is that every access is co-located in ~60 lines with no map-and-mutex pair being passed around — auditability, not enforcement. Real enforcement needs a separate package and an exported slot, which is more surface than one file's invariant is worth. I'd rather the comment say that than imply a guarantee the compiler does not give.

Where #173 stands after this

Item 4 done. Items 9 and 10 were reviewed and declined on value (item 9's own remedy is permanent production API surface to remove a 50 ms sleep at three call sites; item 10 trims one unused package from an image that ships no rg and calls itself a demo image). Item 2 (MaxCallRecvMsgSize) is the only live one left and needs a decision for both ends of the wire before any code.

Verification

go test -race ./... · go vet · gofmt · make test (whole repo) · make typecheck · make lint (9/9 hooks). No TypeScript touched; pi-fork is not involved and contains no Go.

Assisted-By: Claude (Anthropic AI) [email protected]

…ossoctl#173 item 4)

recvLoop took seven positional parameters and accept six, both including a raw
*sync.Mutex passed alongside the map it guards. This file's central safety
property is "every access to the slot map happens under the mutex", and it was
held by convention at five separate lock sites plus a sixth read in the pool
goroutine — so verifying it meant reading all six.

execRegistry now owns the map and the mutex, with one method per critical section
that used to be written longhand: abort, finish, remove, slotFor, claim. The
remaining plumbing (stream, trySend, queue, registry) moves into a conn struct.
recvLoop goes 7 params -> 2, accept 6 -> 3, and abortReq disappears entirely since
it was only ever reg.abort.

Each method preserves its original lock SCOPE, which is the part that could
silently change behaviour. finish cancels under the lock (it is the terminal
release, nobody holds the CancelFunc afterwards); remove deletes without
cancelling, because accept's queue-full branch still holds cancel and calls it
outside the lock, exactly as before. Collapsing those two into one method would
have moved a cancel inside the critical section.

accept's decision is compound — read the slot map, maybe consult the dedup cache,
maybe insert, all under one hold — so claim takes a decide callback and returns a
claimOutcome. decide DECIDES ONLY: every side effect (cancel, trySend, logging)
happens after the lock drops, because the pre-refactor code released the mutex
before each one and holding it across a channel send would widen the critical
section rather than relocate it. The load-bearing comments — the two-terminal-
frames bug, the in-flight-before-cache ordering, the collision-vs-redelivery
distinction — stay in accept where they document the decision, not in the
registry.

No behaviour change: 44 tests pass before and after, under -race, and coverage
confirms all four claim outcomes are exercised. The two uncovered blocks in accept
are the ErrEgressWedged escalations on the collision and cache-hit paths; they map
1:1 to branches that were equally uncovered before this commit, so the gap is
pre-existing rather than introduced. Worth a follow-up, not worth widening a
refactor for.

One honest bound, stated in the type's own doc comment: execRegistry shares a
package with its callers, so unexported fields are no barrier and the compiler
will not stop future code writing r.inflight[id] directly. What this buys is that
every access is co-located in ~60 lines with no map-and-mutex pair being passed
around. That is auditability, not enforcement. Enforcement would need a separate
package and an exported slot, which is more surface than one file's invariant is
worth.

Assisted-By: Claude (Anthropic AI) <[email protected]>
Signed-off-by: Paolo Dettori <[email protected]>

@pdettori pdettori left a comment

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reviewed the lock scope path by path, comparing base 33e6919 against head ca2ae73 side by side rather than reading the diff linearly — for this change that is the only reading that proves anything. The claim in the description holds: every critical section keeps its original scope.

Path Base Head
abortReq → reg.abort lock, cancel, no delete same
finish → reg.finish cancel under lock, then delete same
pool read lock, read, unlock slotFor
in-flight hit fp compared under lock, cancel after unlock same
cache hit Lookup under lock, cancel/trySend after same
admitted insert under lock, collision log after same
queue-full delete under lock, cancel outside remove + cancel

The remove/finish split is the load-bearing detail and the doc comment on remove says why it exists, which is what stops someone collapsing the two later and quietly moving a cancel inside the lock. Keeping enqueue out of conn while trySend goes in is the right asymmetry too: accept cannot reach the blocking sender by accident.

The coverage claim checks out. Tests drive the loop through Serve rather than calling accept/recvLoop directly, which is why a 7→2 parameter change needed no test edits, and all four claimOutcome values are reached by name: TestDuplicateInFlightIsCoalesced, TestCollidingDuplicateWhileInFlightIsRefused, TestRedeliveredReqIDReEmitsWithoutRerunning / TestCacheSurvivesReconnect, plus the admitted path throughout. Agreed that the two uncovered ErrEgressWedged escalations are pre-existing and not worth widening this refactor for.

One suggestion inline about claim's return convention. Nothing blocking.

One nit that cannot be inline, since the file is not in this diff: remote-worker/internal/session/loop_test.go:388 reads // let recvLoop's abortReq land before the pool frees up. This PR deletes abortReq, so that comment becomes a dangling reference to an identifier that no longer exists. Worth folding into this commit as reg.abort — in a file where the comments are this load-bearing, a name that greps to nothing is the kind of thing that costs the next reader real time.

CI is 12/12.

Assisted-By: Claude Code

return &slot{ctx: slotCtx, cancel: cancel, fp: fp}
})

switch outcome {

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Consider having decide return (claimOutcome, *slot) and claim return the outcome, rather than writing back through the captured outcome/cached/collision vars.

Two things combine here. claimAdmitted is iota's zero value, and the admitted path is the implicit fall-through after this switch — there is no case claimAdmitted and no default. So "decide ran and admitted" and "outcome was never assigned" are the same state.

It is not reachable today: claim calls decide unconditionally, so outcome is always set. But if a future edit ever gives claim an early return — a fast-path miss, say — the bug is exactly the silent one this file works hardest to prevent. The exec falls through, gets enqueued with no slot inserted, the pool hits sl == nil and continues, and no terminal frame is ever sent. The caller then waits out DEFAULT_EXEC_TIMEOUT_S, 30 minutes since #182.

Returning the outcome makes "an outcome was decided" a property of the signature rather than of closure discipline, and lets the switch name all four cases. That is the same move var _ [TerminalReserve - 1]struct{} makes above: turn a plausible future mistake into something the compiler catches instead of something a comment warns about.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed in ad992da. The hazard is real as stated — I checked each link rather than taking the chain on trust: claimAdmitted is iota's zero value, var outcome claimOutcome starts there, and the switch had no case claimAdmitted and no default, so "decided to admit" and "never assigned" were genuinely the same observable state.

decide now returns (claimOutcome, *slot) and claim returns the outcome.

Went one step past the suggestion. Returning the outcome removes the ambiguity, but admitting was still the fall-through — so a fifth outcome added later without a case would inherit "admit it", which is the same failure wearing a different hat. The switch now names claimAdmitted explicitly (enqueue path split into enqueueAdmitted), and anything unhandled returns a loud error. Same spirit as the var _ [TerminalReserve - 1]struct{} you cite: make the plausible future mistake fail rather than proceed.

cached and collision stay captured. They are payload for a verdict rather than the verdict — each is assigned in the same statement as the outcome that makes it meaningful — so folding them into the return would widen the signature without closing a hole.

One honest number: accept's coverage drops 94.6% → 90.0%, entirely because of the new unreachable guard. I checked rather than assumed — the only three uncovered blocks are that guard plus the two pre-existing ErrEgressWedged escalations. claim and enqueueAdmitted are both 100%, and the suite is still 44 passing under -race. A deliberately-unreachable guard costing coverage percentage seemed the right trade to flag rather than hide.

The abortReq nit is fixed too — good catch on something outside the diff. It now reads reg.abort.

…utcome, fix a dangling name

Verified the reviewer's hazard before changing anything, and it is real as stated:
claimAdmitted is iota's zero value, `var outcome claimOutcome` starts there, and
admitting was accept's implicit fall-through with no case and no default. So
"decide decided to admit" and "decide never ran" were the same observable state.
Not reachable today — claim calls decide unconditionally — but an early return
added to claim later would enqueue an exec with no slot inserted, the pool would
skip it at sl == nil, no terminal frame would ever be sent, and the caller would
wait out DEFAULT_EXEC_TIMEOUT_S: 30 minutes since rossoctl#182. Precisely the silent class
of failure this file is organised to prevent.

decide now returns (claimOutcome, *slot) and claim returns the outcome, so an
undecided verdict cannot be observed as a decided one. Went one step further than
the suggestion: the switch now names claimAdmitted explicitly, with the enqueue
path split into enqueueAdmitted, and a fifth outcome added without a case here
gets a loud error instead of inheriting "admit it". cached and collision stay
captured — they are payload for a verdict, each assigned in the same statement as
the outcome that makes it meaningful, not the control decision itself.

accept's coverage moves 94.6% -> 90.0%, entirely because of the new
deliberately-unreachable guard. Checked rather than assumed: the only three
uncovered blocks are that guard plus the two pre-existing ErrEgressWedged
escalations on the collision and cache-hit paths. enqueueAdmitted is 100%, claim
is 100%, and the suite is still 44 passing under -race.

Also fixes the nit the reviewer could not file inline because the file was not in
the diff: loop_test.go's comment said "let recvLoop's abortReq land", and this
branch deletes abortReq. Now reads reg.abort. A name that greps to nothing is
worse here than elsewhere, because this file's comments are load-bearing.

Assisted-By: Claude (Anthropic AI) <[email protected]>
Signed-off-by: Paolo Dettori <[email protected]>
@pdettori
pdettori merged commit d7f9630 into rossoctl:main Sep 8, 2026
12 checks passed
@pdettori
pdettori deleted the fix/173-item4-state-plumbing branch September 8, 2026 14:16
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