From 6f5d04a8694da155016cae8247f3c95bdf5accaa Mon Sep 17 00:00:00 2001 From: ankur22 Date: Wed, 25 Sep 2024 17:31:16 +0100 Subject: [PATCH] Update to browser v.1.8.5 Fixes a situation where the browser might hang if the browserProc exits before the connection closes. --- go.mod | 2 +- go.sum | 4 ++-- .../grafana/xk6-browser/chromium/browser_type.go | 10 ++++++---- .../github.com/grafana/xk6-browser/common/browser.go | 11 ++++++++--- .../grafana/xk6-browser/common/connection.go | 3 ++- vendor/modules.txt | 2 +- 6 files changed, 20 insertions(+), 12 deletions(-) diff --git a/go.mod b/go.mod index 28ac4ea59bf..8f3f5ce5610 100644 --- a/go.mod +++ b/go.mod @@ -15,7 +15,7 @@ require ( github.com/go-sourcemap/sourcemap v2.1.4+incompatible github.com/golang/protobuf v1.5.4 github.com/gorilla/websocket v1.5.1 - github.com/grafana/xk6-browser v1.8.4 + github.com/grafana/xk6-browser v1.8.5 github.com/grafana/xk6-dashboard v0.7.5 github.com/grafana/xk6-output-opentelemetry v0.2.0 github.com/grafana/xk6-output-prometheus-remote v0.4.0 diff --git a/go.sum b/go.sum index 2c9b884cfe8..06ae270c405 100644 --- a/go.sum +++ b/go.sum @@ -91,8 +91,8 @@ github.com/gorilla/websocket v1.5.1 h1:gmztn0JnHVt9JZquRuzLw3g4wouNVzKL15iLr/zn/ github.com/gorilla/websocket v1.5.1/go.mod h1:x3kM2JMyaluk02fnUJpQuwD2dCS5NDG2ZHL0uE0tcaY= github.com/grafana/sobek v0.0.0-20240829081756-447e8c611945 h1:rxyoc2Ee/iKcm5EYNU/dEO0GcMZDN5vaDGyIo7pt804= github.com/grafana/sobek v0.0.0-20240829081756-447e8c611945/go.mod h1:FmcutBFPLiGgroH42I4/HBahv7GxVjODcVWFTw1ISes= -github.com/grafana/xk6-browser v1.8.4 h1:k7ivI6fVIaS8ZTdNXSI+4eVayUNQa6I7We93PjLkW9s= -github.com/grafana/xk6-browser v1.8.4/go.mod h1:we1lHAYSVZD5s/4yre2KWtaqY4G0ikNVpn0QE2EO8DA= +github.com/grafana/xk6-browser v1.8.5 h1:dNAG8dhcaEx/HOELEnGzAw8ShCvkpukfyTGUhebZsj0= +github.com/grafana/xk6-browser v1.8.5/go.mod h1:yCtZ4G8U/imVBikBO4HJoMyNoejmECcJk4CK5XGSxis= github.com/grafana/xk6-dashboard v0.7.5 h1:TcILyffT/Ea/XD7xG1jMA5lwtusOPRbEQsQDHmO30Mk= github.com/grafana/xk6-dashboard v0.7.5/go.mod h1:Y75F8xmgCraKT+pBzFH6me9AyH5PkXD+Bxo1dm6Il/M= github.com/grafana/xk6-output-opentelemetry v0.2.0 h1:u/ctsfUQsyLh6paBjOmKshqeYr6HUsCJr29eO2QTb/s= diff --git a/vendor/github.com/grafana/xk6-browser/chromium/browser_type.go b/vendor/github.com/grafana/xk6-browser/chromium/browser_type.go index 43a670fa6bd..e2bdc1bd7b0 100644 --- a/vendor/github.com/grafana/xk6-browser/chromium/browser_type.go +++ b/vendor/github.com/grafana/xk6-browser/chromium/browser_type.go @@ -124,7 +124,7 @@ func (b *BrowserType) Connect(ctx, vuCtx context.Context, wsEndpoint string) (*c func (b *BrowserType) connect( ctx, vuCtx context.Context, wsURL string, opts *common.BrowserOptions, logger *log.Logger, ) (*common.Browser, error) { - browserProc, err := b.link(wsURL, logger) + browserProc, err := b.link(ctx, wsURL, logger) if browserProc == nil { return nil, fmt.Errorf("connecting to browser: %w", err) } @@ -144,9 +144,10 @@ func (b *BrowserType) connect( } func (b *BrowserType) link( + ctx context.Context, wsURL string, logger *log.Logger, ) (*common.BrowserProcess, error) { - bProcCtx, bProcCtxCancel := context.WithCancel(context.Background()) + bProcCtx, bProcCtxCancel := context.WithCancel(ctx) p, err := common.NewRemoteBrowserProcess(bProcCtx, wsURL, bProcCtxCancel, logger) if err != nil { bProcCtxCancel() @@ -205,7 +206,7 @@ func (b *BrowserType) launch( return nil, 0, fmt.Errorf("finding browser executable: %w", err) } - browserProc, err := b.allocate(path, flags, dataDir, logger) + browserProc, err := b.allocate(ctx, path, flags, dataDir, logger) if browserProc == nil { return nil, 0, fmt.Errorf("launching browser: %w", err) } @@ -238,11 +239,12 @@ func (b *BrowserType) Name() string { // allocate starts a new Chromium browser process and returns it. func (b *BrowserType) allocate( + ctx context.Context, path string, flags map[string]any, dataDir *storage.Dir, logger *log.Logger, ) (_ *common.BrowserProcess, rerr error) { - bProcCtx, bProcCtxCancel := context.WithCancel(context.Background()) + bProcCtx, bProcCtxCancel := context.WithCancel(ctx) defer func() { if rerr != nil { bProcCtxCancel() diff --git a/vendor/github.com/grafana/xk6-browser/common/browser.go b/vendor/github.com/grafana/xk6-browser/common/browser.go index 051ace0c38f..8a1638cafe1 100644 --- a/vendor/github.com/grafana/xk6-browser/common/browser.go +++ b/vendor/github.com/grafana/xk6-browser/common/browser.go @@ -538,9 +538,14 @@ func (b *Browser) Close() { // command, which triggers the browser process to exit. if !b.browserOpts.isRemoteBrowser { var closeErr *websocket.CloseError - // Using a internal context here since vu context will very likely be - // closed. - err := cdpbrowser.Close().Do(cdp.WithExecutor(b.browserCtx, b.conn)) + // Using the internal context with a timeout of 10 seconds here since + // 1. vu context will very likely be closed; + // 2. there's a chance that the process has died but the connection still + // thinks it's open. + toCtx, toCancelCtx := context.WithTimeout(b.browserCtx, time.Second*10) + defer toCancelCtx() + + err := cdpbrowser.Close().Do(cdp.WithExecutor(toCtx, b.conn)) if err != nil && !errors.As(err, &closeErr) { b.logger.Errorf("Browser:Close", "closing the browser: %v", err) } diff --git a/vendor/github.com/grafana/xk6-browser/common/connection.go b/vendor/github.com/grafana/xk6-browser/common/connection.go index e951089340b..171705a1839 100644 --- a/vendor/github.com/grafana/xk6-browser/common/connection.go +++ b/vendor/github.com/grafana/xk6-browser/common/connection.go @@ -623,7 +623,8 @@ func (c *Connection) Execute(ctx context.Context, method string, params easyjson Method: cdproto.MethodType(method), Params: buf, } - return c.send(c.ctx, msg, ch, res) + + return c.send(evCancelCtx, msg, ch, res) } // IgnoreIOErrors signals that the connection will soon be closed, so that any diff --git a/vendor/modules.txt b/vendor/modules.txt index 2009c4774a8..099dead6252 100644 --- a/vendor/modules.txt +++ b/vendor/modules.txt @@ -173,7 +173,7 @@ github.com/grafana/sobek/ftoa/internal/fast github.com/grafana/sobek/parser github.com/grafana/sobek/token github.com/grafana/sobek/unistring -# github.com/grafana/xk6-browser v1.8.4 +# github.com/grafana/xk6-browser v1.8.5 ## explicit; go 1.21 github.com/grafana/xk6-browser/browser github.com/grafana/xk6-browser/chromium