Skip to content

Commit

Permalink
Add error handling for Dispose
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
ankur22 committed Apr 29, 2024
1 parent 53c0ce7 commit 72239a3
Showing 1 changed file with 8 additions and 0 deletions.
8 changes: 8 additions & 0 deletions common/js_handle.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"errors"
"fmt"
"strings"

"github.com/grafana/xk6-browser/k6ext"
"github.com/grafana/xk6-browser/log"
Expand Down Expand Up @@ -87,6 +88,13 @@ func (h *BaseJSHandle) Dispose() {
if errors.Is(err, context.Canceled) {
h.logger.Debugf("BaseJSHandle:Dispose", "%v", err)
}
// 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)
}

k6ext.Panic(h.ctx, "dispose: %w", err)
}
Expand Down

0 comments on commit 72239a3

Please sign in to comment.