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

Temporary fix for deadlock on pagehide event evaluation when page.close is called #979

Merged
merged 3 commits into from
Jul 26, 2023
Merged
Show file tree
Hide file tree
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
8 changes: 7 additions & 1 deletion api/frame.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
package api

import "github.com/dop251/goja"
import (
"context"

"github.com/dop251/goja"
)

// Frame is the interface of a CDP target frame.
type Frame interface {
Expand All @@ -12,6 +16,8 @@ type Frame interface {
Content() string
Dblclick(selector string, opts goja.Value)
DispatchEvent(selector string, typ string, eventInit goja.Value, opts goja.Value)
// EvaluateWithContext for internal use only
EvaluateWithContext(ctx context.Context, pageFunc goja.Value, args ...goja.Value) (any, error)
Evaluate(pageFunc goja.Value, args ...goja.Value) any
EvaluateHandle(pageFunc goja.Value, args ...goja.Value) (JSHandle, error)
Fill(selector string, value string, opts goja.Value)
Expand Down
11 changes: 6 additions & 5 deletions browser/mapping_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,12 @@ func customMappings() map[string]string {
"Page.getMouse": "mouse",
"Page.getTouchscreen": "touchscreen",
// internal methods
"ElementHandle.objectID": "",
"Frame.id": "",
"Frame.loaderID": "",
"JSHandle.objectID": "",
"Browser.close": "",
"ElementHandle.objectID": "",
"Frame.id": "",
"Frame.loaderID": "",
"JSHandle.objectID": "",
"Browser.close": "",
"Frame.evaluateWithContext": "",
// TODO: browser.on method is unexposed until more event
// types other than 'disconnect' are supported.
// See: https://github.com/grafana/xk6-browser/issues/913
Expand Down
26 changes: 20 additions & 6 deletions common/frame.go
Original file line number Diff line number Diff line change
Expand Up @@ -709,22 +709,36 @@ func (f *Frame) dispatchEvent(selector, typ string, eventInit goja.Value, opts *
return nil
}

// Evaluate will evaluate provided page function within an execution context.
func (f *Frame) Evaluate(pageFunc goja.Value, args ...goja.Value) any {
f.log.Debugf("Frame:Evaluate", "fid:%s furl:%q", f.ID(), f.URL())
// EvaluateWithContext will evaluate provided page function within an execution context.
// The passed in context will be used instead of the frame's context. The context must
// be a derivative of one that contains the goja runtime.
func (f *Frame) EvaluateWithContext(ctx context.Context, pageFunc goja.Value, args ...goja.Value) (any, error) {
f.log.Debugf("Frame:EvaluateWithContext", "fid:%s furl:%q", f.ID(), f.URL())

f.waitForExecutionContext(mainWorld)

opts := evalOptions{
forceCallable: true,
returnByValue: true,
}
result, err := f.evaluate(f.ctx, mainWorld, opts, pageFunc, args...)
result, err := f.evaluate(ctx, mainWorld, opts, pageFunc, args...)
if err != nil {
k6ext.Panic(f.ctx, "evaluating JS: %v", err)
return nil, fmt.Errorf("evaluating JS: %w", err)
}

applySlowMo(f.ctx)
applySlowMo(ctx)

return result, nil
}

// Evaluate will evaluate provided page function within an execution context.
func (f *Frame) Evaluate(pageFunc goja.Value, args ...goja.Value) any {
f.log.Debugf("Frame:Evaluate", "fid:%s furl:%q", f.ID(), f.URL())

result, err := f.EvaluateWithContext(f.ctx, pageFunc, args...)
if err != nil {
k6ext.Panic(f.ctx, "%v", err)
}

return result
}
Expand Down
9 changes: 7 additions & 2 deletions common/page.go
Original file line number Diff line number Diff line change
Expand Up @@ -427,15 +427,20 @@ func (p *Page) Close(opts goja.Value) error {

// forcing the pagehide event to trigger web vitals metrics.
v := p.vu.Runtime().ToValue(`() => window.dispatchEvent(new Event('pagehide'))`)
_ = p.Evaluate(v)
ctx, cancel := context.WithTimeout(p.ctx, p.defaultTimeout())
defer cancel()
_, err := p.MainFrame().EvaluateWithContext(ctx, v)
if err != nil {
p.logger.Warnf("Page:Close", "failed to hide page: %v", err)
}

add := runtime.RemoveBinding(webVitalBinding)
if err := add.Do(cdp.WithExecutor(p.ctx, p.session)); err != nil {
return fmt.Errorf("internal error while removing binding from page: %w", err)
}

action := target.CloseTarget(p.targetID)
err := action.Do(cdp.WithExecutor(p.ctx, p.session))
err = action.Do(cdp.WithExecutor(p.ctx, p.session))
if err != nil {
// When a close target command is sent to the browser via CDP,
// the browser will start to cleanup and the first thing it
Expand Down