Skip to content
Merged
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
12 changes: 12 additions & 0 deletions pkg/protocols/headless/engine/page.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,11 +62,13 @@ func (i *Instance) Run(ctx *contextargs.Context, actions []*Action, payloads map
page = page.Timeout(options.Timeout)

if err = i.browser.applyDefaultHeaders(page); err != nil {
_ = page.Close()
return nil, nil, err
}

if i.browser.customAgent != "" {
if userAgentErr := page.SetUserAgent(&proto.NetworkSetUserAgentOverride{UserAgent: i.browser.customAgent}); userAgentErr != nil {
_ = page.Close()
return nil, nil, userAgentErr
}
}
Expand All @@ -78,6 +80,7 @@ func (i *Instance) Run(ctx *contextargs.Context, actions []*Action, payloads map
target := ctx.MetaInput.Input
input, err := urlutil.Parse(target)
if err != nil {
_ = page.Close()
return nil, nil, errkit.Wrapf(err, "could not parse URL %s", target)
}

Expand All @@ -100,6 +103,14 @@ func (i *Instance) Run(ctx *contextargs.Context, actions []*Action, payloads map
inputURL: input,
}

successfulPageCreation := false
defer func() {
if !successfulPageCreation {
// to avoid leaking pages in case of errors
createdPage.Close()
}
}()

httpclient, err := i.browser.getHTTPClient()
if err != nil {
return nil, nil, err
Expand Down Expand Up @@ -207,6 +218,7 @@ func (i *Instance) Run(ctx *contextargs.Context, actions []*Action, payloads map
}
}

successfulPageCreation = true // avoid closing the page in case of success in deferred function
return data, createdPage, nil
}

Expand Down
Loading