From ab431b543ee286ad4dc960a6c06cfbbbba0aea50 Mon Sep 17 00:00:00 2001 From: ankur22 Date: Fri, 5 Apr 2024 17:09:48 +0100 Subject: [PATCH] Add fix to prevent panic when ctx closed If the context is closed, and that is the error that is received by the dispose method, then that would elude to the fact that the iteration has ended. Therefore, we shouldn't need to panic or log an error at this point, and instead just continue on as normal. The page has closed well before this panic occurs. --- common/js_handle.go | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/common/js_handle.go b/common/js_handle.go index d9e20e691..d021d7eb4 100644 --- a/common/js_handle.go +++ b/common/js_handle.go @@ -2,6 +2,7 @@ package common import ( "context" + "errors" "fmt" "github.com/grafana/xk6-browser/k6ext" @@ -79,7 +80,13 @@ func (h *BaseJSHandle) AsElement() *ElementHandle { // Dispose releases the remote object. func (h *BaseJSHandle) Dispose() { if err := h.dispose(); err != nil { - k6ext.Panic(h.ctx, "dispose: %w", err) + // We do not want to panic on an error when the error is a closed + // 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) + } } }