fix(sess): guard ctx.session with atomic.Pointer (closes #31)#51
Merged
Conversation
Session.Rotate wrote s.ctx.session outside any lock while broadcastRender, the action/SSE session-mismatch checks, and Ctx.Session read it from other goroutines — a data race the detector flags and that could expose a stale session pointer to a fan-out path. Convert Ctx.session to atomic.Pointer[session]: Rotate and the render-time assignment Store; all readers Load. statesess.Update now loads the pointer once, which also closes a pre-existing window where a rotate landing between the two reads broadcast against the new session while mutating the old one.
Member
Author
Verification
Non-blocking note (out of scope)The session-mismatch guard |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What
Fixes the critical data race in #31:
Session.Rotatewrotes.ctx.session = freshoutside any lock (sess.go) whilebroadcastRender(broadcast.go:54), the action/SSE session-mismatch checks (action.go, sse.go), andCtx.Session(ctx.go) readc.sessionfrom other goroutines. Per the Go memory model this is UB;go test -raceflags it, and an unlucky reorder could expose a stale session pointer to a fan-out path whose target was just removed fromapp.sessionsbyRotate.How
Convert
Ctx.sessionfrom*sessiontoatomic.Pointer[session]:Rotateand the render-time assignment use.Store().broadcastRender, action/SSE mismatch checks,Ctx.Session,StateSess.Read/Update) use.Load().StateSess.Updatenow loads the session pointer once into a local. Besides being race-free, this closes a pre-existing window: the old code readctx.sessiontwice (data mutation, thenbroadcastRenderarg), so aRotatelanding between them mutated the old session but broadcast against the new pointer — waking the wrong tab set. Loading once keeps mutation and fan-out on the same session.Surgical, no API change — exactly the fix suggested in the issue.
Test (TDD)
TestRotateSession_doesNotRaceWithSiblingSessionBroadcast(sess_test.go): two tabs on distinct sessions; one rotates in a loop (writing its ctx's session pointer) while the other drivesStateSess.Update, whosebroadcastRenderreads every live ctx's session pointer before the session-equality filter (broadcast.go:54) — including the rotating one. Distinct sessions isolate the pointer race without either tab invalidating the other.-racereportsbroadcast.go:54read vssess.go:106write.-count=5.Verification
go vet ./...cleangofmt -l .cleango test -race ./...— all packages pass