Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixes an NPD by not disposing of the original handle #4280

Merged
merged 2 commits into from
Jan 27, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 13 additions & 9 deletions internal/js/modules/k6/browser/common/frame.go
Original file line number Diff line number Diff line change
Expand Up @@ -478,7 +478,7 @@ func (f *Frame) waitForSelectorRetry(
// It will auto retry on certain errors until the retryCount is below 0. The
// retry workaround is needed since the underlying DOM can change when the
// wait action is performed during a navigation.
func (f *Frame) waitForSelector(selector string, opts *FrameWaitForSelectorOptions) (_ *ElementHandle, rerr error) {
func (f *Frame) waitForSelector(selector string, opts *FrameWaitForSelectorOptions) (*ElementHandle, error) {
f.log.Debugf("Frame:waitForSelector", "fid:%s furl:%q sel:%q", f.ID(), f.URL(), selector)

handle, err := f.waitFor(selector, opts, 20)
Expand All @@ -497,21 +497,25 @@ func (f *Frame) waitForSelector(selector string, opts *FrameWaitForSelectorOptio
if ec == nil {
return nil, fmt.Errorf("waiting for selector %q: execution context %q not found", selector, mainWorld)
}

// an element should belong to the current execution context.
// otherwise, we should adopt it to this execution context.
adopted := handle
if ec != handle.execCtx {
defer func() {
if err := handle.Dispose(); err != nil {
err = fmt.Errorf("disposing element handle: %w", err)
rerr = errors.Join(err, rerr)
}
}()
if handle, err = ec.adoptElementHandle(handle); err != nil {
if adopted, err = ec.adoptElementHandle(handle); err != nil {
return nil, fmt.Errorf("waiting for selector %q: adopting element handle: %w", selector, err)
}

if err = handle.Dispose(); err != nil {
f.log.Warnf(
"Frame:waitForSelector",
"fid:%s furl:%q sel:%q disposing element handle: %v",
f.ID(), f.URL(), selector, err,
)
}
}

return handle, nil
return adopted, nil
}

func (f *Frame) waitFor(
Expand Down
Loading