fix: guard pageImpl.frames to prevent data race#625
Closed
alvarorichard wants to merge 1 commit into
Closed
Conversation
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.
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 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()andPage.Frame()at the same time.This PR:
pageImpl.frameswith the existingchannelOwnerRWMutex;Page.Frames();Page.Frame()iterate over a protected snapshot;Testing
tests/page_frames_race_test.gogo build ./...gofumpt -w page.go tests/page_frames_race_test.gogolangci-lint runBROWSER=chromium HEADLESS=1 go test -v --race ./...All checks passed locally, including the full test suite with the race detector.