Skip to content

fix: guard pageImpl.frames to prevent data race#625

Closed
alvarorichard wants to merge 1 commit into
mxschmitt:mainfrom
alvarorichard:fix/page-frames-data-race
Closed

fix: guard pageImpl.frames to prevent data race#625
alvarorichard wants to merge 1 commit into
mxschmitt:mainfrom
alvarorichard:fix/page-frames-data-race

Conversation

@alvarorichard

Copy link
Copy Markdown

What changed

This fixes a data race involving pageImpl.frames.

The frames slice is updated by the connection event handler when iframes are attached or detached, while user code can read it through Page.Frames() and Page.Frame() at the same time.

This PR:

  • protects pageImpl.frames with the existing channelOwner RWMutex;
  • returns a copy of the frames slice from Page.Frames();
  • makes Page.Frame() iterate over a protected snapshot;
  • keeps event emission outside the lock to avoid deadlocks when event handlers call Page methods.

Testing

  • Added tests/page_frames_race_test.go
  • go build ./...
  • gofumpt -w page.go tests/page_frames_race_test.go
  • golangci-lint run
  • BROWSER=chromium HEADLESS=1 go test -v --race ./...

All checks passed locally, including the full test suite with the race detector.

pageImpl.frames was written by the connection dispatch goroutine
(onFrameAttached/onFrameDetached) and read by user goroutines via
Page.Frames()/Page.Frame() with no synchronization. Frames() also
returned the internal slice without copying, so callers aliased
memory mutated by the event goroutine.

Guard p.frames with the sync.RWMutex already embedded via
channelOwner, mirroring the existing pattern in browser.go and
browser_context.go. Frames() now returns a copy under RLock, Frame()
iterates a locked snapshot, and onFrameAttached/onFrameDetached
mutate under Lock with Emit kept outside the critical section since
handlers run synchronously and may re-enter Page methods.

Adds a regression test that fails with multiple DATA RACE reports
under -race before this change and passes after.
@alvarorichard
alvarorichard deleted the fix/page-frames-data-race branch July 17, 2026 03:42
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