Skip to content

fix(sess): guard ctx.session with atomic.Pointer (closes #31)#51

Merged
joaomdsg merged 1 commit into
mainfrom
fix/session-data-race-31
May 29, 2026
Merged

fix(sess): guard ctx.session with atomic.Pointer (closes #31)#51
joaomdsg merged 1 commit into
mainfrom
fix/session-data-race-31

Conversation

@joaomdsg

Copy link
Copy Markdown
Member

What

Fixes the critical data race in #31: Session.Rotate wrote s.ctx.session = fresh outside any lock (sess.go) while broadcastRender (broadcast.go:54), the action/SSE session-mismatch checks (action.go, sse.go), and Ctx.Session (ctx.go) read c.session from other goroutines. Per the Go memory model this is UB; go test -race flags it, and an unlucky reorder could expose a stale session pointer to a fan-out path whose target was just removed from app.sessions by Rotate.

How

Convert Ctx.session from *session to atomic.Pointer[session]:

  • Rotate and the render-time assignment use .Store().
  • All readers (broadcastRender, action/SSE mismatch checks, Ctx.Session, StateSess.Read/Update) use .Load().
  • StateSess.Update now loads the session pointer once into a local. Besides being race-free, this closes a pre-existing window: the old code read ctx.session twice (data mutation, then broadcastRender arg), so a Rotate landing 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 drives StateSess.Update, whose broadcastRender reads 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.

  • Without the fix: -race reports broadcast.go:54 read vs sess.go:106 write.
  • With the fix: green, including -count=5.

Verification

  • go vet ./... clean
  • gofmt -l . clean
  • go test -race ./... — all packages pass

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.
@joaomdsg

Copy link
Copy Markdown
Member Author

Verification

  • Race test (definitive): TestRotateSession_doesNotRaceWithSiblingSessionBroadcast fails under -race without the fix (read broadcast.go:54 vs write sess.go:106) and passes with it (-count=5).
  • Full suite: go test -race ./... green across all packages; go vet and gofmt clean.
  • Live browser (Brave) under the -race build: drove the internal/examples/auth demo end-to-end — register → login (sess.Rotate) → requireAuth-protected /profile. All 200; /profile rendered the user's name + email, proving rotation stored the fresh session, carried data forward, and the new cookie authenticated the protected page. Zero data-race reports in the server log.

Non-blocking note (out of scope)

The session-mismatch guard if sess := ctx.session.Load(); sess != nil && a.sessionFromRequest(r) != sess now appears in 3 spots (action.go, sse.go:37, sse.go:232). This duplication pre-dates this PR — the diff only swapped the bare field read for .Load(). Worth extracting to a ctx.sessionMatches(r) helper in a follow-up, but kept out of this fix to keep the diff surgical.

@joaomdsg
joaomdsg merged commit a98e9d9 into main May 29, 2026
4 checks passed
@joaomdsg
joaomdsg deleted the fix/session-data-race-31 branch May 29, 2026 00:25
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