From 50a3777773e536d8b25b9ac3110d8979bc4271b2 Mon Sep 17 00:00:00 2001 From: ankur22 Date: Mon, 29 Apr 2024 16:32:00 +0100 Subject: [PATCH 1/2] Refactor to allow for more error handling --- common/js_handle.go | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/common/js_handle.go b/common/js_handle.go index d021d7eb4..1eef5f1b0 100644 --- a/common/js_handle.go +++ b/common/js_handle.go @@ -84,9 +84,12 @@ func (h *BaseJSHandle) Dispose() { // context. The reason the context would be closed is due to the // iteration ending and therefore the associated browser and its assets // will be automatically deleted. - if !errors.Is(err, context.Canceled) { - k6ext.Panic(h.ctx, "dispose: %w", err) + if errors.Is(err, context.Canceled) { + h.logger.Debugf("BaseJSHandle:Dispose", "%v", err) + return } + + k6ext.Panic(h.ctx, "dispose: %w", err) } } From 7e42ae944429ed77604190e695b2212af470712e Mon Sep 17 00:00:00 2001 From: ankur22 Date: Mon, 29 Apr 2024 16:32:57 +0100 Subject: [PATCH 2/2] Add error handling for Dispose Dispose can panic when a navigation occurs on a page with an iframe. The specific error that is returned is "Cannot find context with specified id". The reason for this error is due to the fact that Chrome has already cleaned up the associated resource before it receives the CDP command to release it. Therefore it is safe to handle this error, log a debug log and carry on with the test. --- common/js_handle.go | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/common/js_handle.go b/common/js_handle.go index 1eef5f1b0..f52469e24 100644 --- a/common/js_handle.go +++ b/common/js_handle.go @@ -4,6 +4,7 @@ import ( "context" "errors" "fmt" + "strings" "github.com/grafana/xk6-browser/k6ext" "github.com/grafana/xk6-browser/log" @@ -88,6 +89,14 @@ func (h *BaseJSHandle) Dispose() { h.logger.Debugf("BaseJSHandle:Dispose", "%v", err) return } + // The following error indicates that the object we're trying to release + // cannot be found, which would mean that the object has already been + // removed/deleted. This can occur when a navigation occurs, usually when + // a page contains an iframe. + if strings.Contains(err.Error(), "Cannot find context with specified id") { + h.logger.Debugf("BaseJSHandle:Dispose", "%v", err) + return + } k6ext.Panic(h.ctx, "dispose: %w", err) }