Skip to content
This repository was archived by the owner on Jan 30, 2025. It is now read-only.

Commit ba775c9

Browse files
committed
Fix do not unnecessarily set mainframe
Also: Add some sanity checks and protect the mainframe field behind a mutex in places that forgot to use it.
1 parent 176800f commit ba775c9

File tree

6 files changed

+50
-22
lines changed

6 files changed

+50
-22
lines changed

chromium/browser_type.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -174,6 +174,7 @@ func makeLogger(ctx context.Context, launchOpts *common.LaunchOptions) (*common.
174174
reCategoryFilter, _ = regexp.Compile(launchOpts.LogCategoryFilter)
175175
logger = common.NewLogger(ctx, k6Logger, launchOpts.Debug, reCategoryFilter)
176176
)
177+
177178
// set the log level from the launch options (usually from a script's options).
178179
if launchOpts.Debug {
179180
_ = logger.SetLevel("debug")

common/frame.go

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -166,7 +166,7 @@ func (f *Frame) clearLifecycle() {
166166
}
167167
f.lifecycleEventsMu.Unlock()
168168

169-
f.page.frameManager.mainFrame.recalculateLifecycle()
169+
f.page.frameManager.MainFrame().recalculateLifecycle()
170170

171171
// keep the request related to the document if present
172172
// in f.inflightRequests
@@ -419,7 +419,7 @@ func (f *Frame) position() *Position {
419419
if frame == nil {
420420
return nil
421421
}
422-
if frame == f.page.frameManager.mainFrame {
422+
if frame == f.page.frameManager.MainFrame() {
423423
return &Position{X: 0, Y: 0}
424424
}
425425
element := frame.FrameElement()
@@ -1107,6 +1107,10 @@ func (f *Frame) IsVisible(selector string, opts goja.Value) bool {
11071107

11081108
// ID returns the frame id
11091109
func (f *Frame) ID() string {
1110+
if f == nil {
1111+
return ""
1112+
}
1113+
11101114
f.propertiesMu.RLock()
11111115
defer f.propertiesMu.RUnlock()
11121116

@@ -1115,6 +1119,10 @@ func (f *Frame) ID() string {
11151119

11161120
// LoaderID returns the ID of the frame that loaded this frame
11171121
func (f *Frame) LoaderID() string {
1122+
if f == nil {
1123+
return ""
1124+
}
1125+
11181126
f.propertiesMu.RLock()
11191127
defer f.propertiesMu.RUnlock()
11201128

@@ -1123,6 +1131,10 @@ func (f *Frame) LoaderID() string {
11231131

11241132
// Name returns the frame name
11251133
func (f *Frame) Name() string {
1134+
if f == nil {
1135+
return ""
1136+
}
1137+
11261138
f.propertiesMu.RLock()
11271139
defer f.propertiesMu.RUnlock()
11281140

@@ -1358,6 +1370,10 @@ func (f *Frame) Uncheck(selector string, opts goja.Value) {
13581370

13591371
// URL returns the frame URL.
13601372
func (f *Frame) URL() string {
1373+
if f == nil {
1374+
return ""
1375+
}
1376+
13611377
f.propertiesMu.RLock()
13621378
defer f.propertiesMu.RUnlock()
13631379

common/frame_manager.go

Lines changed: 24 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -203,7 +203,7 @@ func (m *FrameManager) frameLifecycleEvent(frameID cdp.FrameID, event LifecycleE
203203
frame := m.getFrameByID(frameID)
204204
if frame != nil {
205205
frame.onLifecycleEvent(event)
206-
m.mainFrame.recalculateLifecycle() // Recalculate life cycle state from the top
206+
m.MainFrame().recalculateLifecycle() // Recalculate life cycle state from the top
207207
}
208208
}
209209

@@ -253,23 +253,26 @@ func (m *FrameManager) frameNavigated(frameID cdp.FrameID, parentFrameID cdp.Fra
253253
}
254254
}
255255

256-
if isMainFrame {
257-
if frame != nil {
258-
m.logger.Debugf("FrameManager:frameNavigated:MainFrame:delete",
259-
"fmid:%d fid:%v pfid:%v docid:%s fname:%s furl:%s initial:%t oldfid:%v",
260-
m.ID(), frameID, parentFrameID, documentID, name, url, initial, frame.ID())
256+
var mainFrame *Frame
257+
if isMainFrame && frame == nil {
258+
m.logger.Debugf("FrameManager:frameNavigated:MainFrame:initialMainFrameNavigation",
259+
"fmid:%d fid:%v pfid:%v docid:%s fname:%s furl:%s initial:%t",
260+
m.ID(), frameID, parentFrameID, documentID, name, url, initial)
261261

262-
// Update frame ID to retain frame identity on cross-process navigation.
263-
delete(m.frames, cdp.FrameID(frame.ID()))
264-
frame.setID(frameID)
265-
} else {
266-
m.logger.Debugf("FrameManager:frameNavigated:MainFrame:initialMainFrameNavigation",
267-
"fmid:%d fid:%v pfid:%v docid:%s fname:%s furl:%s initial:%t",
268-
m.ID(), frameID, parentFrameID, documentID, name, url, initial)
262+
// Initial main frame navigation.
263+
frame = NewFrame(m.ctx, m, nil, frameID, m.logger)
264+
mainFrame = frame
265+
} else if isMainFrame && frame.ID() != string(frameID) {
266+
m.logger.Debugf("FrameManager:frameNavigated:MainFrame:delete",
267+
"fmid:%d fid:%v pfid:%v docid:%s fname:%s furl:%s initial:%t oldfid:%v",
268+
m.ID(), frameID, parentFrameID, documentID, name, url, initial, frame.ID())
269269

270-
// Initial main frame navigation.
271-
frame = NewFrame(m.ctx, m, nil, frameID, m.logger)
272-
}
270+
// Update frame ID to retain frame identity on cross-process navigation.
271+
delete(m.frames, cdp.FrameID(frame.ID()))
272+
frame.setID(frameID)
273+
mainFrame = frame
274+
}
275+
if mainFrame != nil {
273276
m.frames[frameID] = frame
274277
m.setMainFrame(frame)
275278
}
@@ -548,6 +551,11 @@ func (m *FrameManager) MainFrame() *Frame {
548551
func (m *FrameManager) setMainFrame(f *Frame) {
549552
m.mainFrameMu.Lock()
550553
defer m.mainFrameMu.Unlock()
554+
555+
m.logger.Debugf("FrameManager:setMainFrame",
556+
"fmid:%d fid:%v furl:%s",
557+
m.ID(), f.ID(), f.URL())
558+
551559
m.mainFrame = f
552560
}
553561

common/page.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -693,7 +693,7 @@ func (p *Page) Reload(opts goja.Value) api.Response {
693693
k6common.Throw(rt, fmt.Errorf("failed parsing options: %w", err))
694694
}
695695

696-
ch, evCancelFn := createWaitForEventHandler(p.ctx, p.frameManager.mainFrame, []string{EventFrameNavigation}, func(data interface{}) bool {
696+
ch, evCancelFn := createWaitForEventHandler(p.ctx, p.frameManager.MainFrame(), []string{EventFrameNavigation}, func(data interface{}) bool {
697697
return true // Both successful and failed navigations are considered
698698
})
699699
defer evCancelFn() // Remove event handler
@@ -713,7 +713,7 @@ func (p *Page) Reload(opts goja.Value) api.Response {
713713
}
714714

715715
if p.frameManager.mainFrame.hasSubtreeLifecycleEventFired(parsedOpts.WaitUntil) {
716-
waitForEvent(p.ctx, p.frameManager.mainFrame, []string{EventFrameAddLifecycle}, func(data interface{}) bool {
716+
_, _ = waitForEvent(p.ctx, p.frameManager.MainFrame(), []string{EventFrameAddLifecycle}, func(data interface{}) bool {
717717
return data.(LifecycleEvent) == parsedOpts.WaitUntil
718718
}, parsedOpts.Timeout)
719719
}

common/request.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -264,5 +264,8 @@ func (r *Request) Timing() goja.Value {
264264

265265
// URL returns the request URL
266266
func (r *Request) URL() string {
267+
if r == nil {
268+
return ""
269+
}
267270
return r.url.String()
268271
}

common/screenshotter.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ func (s *screenshotter) fullPageSize(p *Page) (*Size, error) {
5151
forceCallable: true,
5252
returnByValue: true,
5353
}
54-
result, err := p.frameManager.mainFrame.evaluate(s.ctx, mainWorld, opts, rt.ToValue(`
54+
result, err := p.frameManager.MainFrame().evaluate(s.ctx, mainWorld, opts, rt.ToValue(`
5555
() => {
5656
if (!document.body || !document.documentElement) {
5757
return null;
@@ -95,7 +95,7 @@ func (s *screenshotter) originalViewportSize(p *Page) (*Size, *Size, error) {
9595
forceCallable: true,
9696
returnByValue: true,
9797
}
98-
result, err := p.frameManager.mainFrame.evaluate(s.ctx, mainWorld, opts, rt.ToValue(`
98+
result, err := p.frameManager.MainFrame().evaluate(s.ctx, mainWorld, opts, rt.ToValue(`
9999
() => (
100100
{ width: window.innerWidth, height: window.innerHeight }
101101
)`))

0 commit comments

Comments
 (0)