fix: address Playwright API audit findings
- Replace deprecated Locator.Type() with PressSequentially() (node.go) - Close page on Goto failure to prevent resource leak (playwright.go) - Fix teardown order: close context before browser (playwright.go) - Clean up resources on NewPage failure (interactive.go) - Spawn cleanup goroutine on context cancellation in both constructors Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
@@ -195,6 +195,12 @@ func NewBrowser(ctx context.Context, opts ...BrowserOptions) (Browser, error) {
|
||||
|
||||
select {
|
||||
case <-ctx.Done():
|
||||
go func() {
|
||||
r := <-resultCh
|
||||
if r.err == nil && r.browser != nil {
|
||||
_ = r.browser.Close()
|
||||
}
|
||||
}()
|
||||
return nil, ctx.Err()
|
||||
case result := <-resultCh:
|
||||
return result.browser, result.err
|
||||
@@ -242,6 +248,7 @@ func (b playWrightBrowser) openPage(_ context.Context, target string, opts OpenP
|
||||
|
||||
resp, err := page.Goto(target, pwOpts)
|
||||
if err != nil {
|
||||
_ = page.Close()
|
||||
return nil, err
|
||||
}
|
||||
|
||||
@@ -277,8 +284,8 @@ func (b playWrightBrowser) Open(ctx context.Context, url string, opts OpenPageOp
|
||||
|
||||
func (b playWrightBrowser) Close() error {
|
||||
return errors.Join(
|
||||
b.browser.Close(),
|
||||
b.ctx.Close(),
|
||||
b.browser.Close(),
|
||||
b.pw.Stop(),
|
||||
)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user