Skip to content

Commit 11458a2

Browse files
committed
Fix issues with duplicate close of browser
While the Close() function was guarded by a mutex duplicate work would still be carried out if calling twice, such as passing the script context to the pool multiple times.
1 parent d41a1ef commit 11458a2

File tree

1 file changed

+10
-1
lines changed

1 file changed

+10
-1
lines changed

browser.go

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -187,6 +187,10 @@ func (b *Browser) Open(location string) (window Window, err error) {
187187
b.closeLock.Lock()
188188
defer b.closeLock.Unlock()
189189

190+
if b.Closed() {
191+
panic("Open window on a closed browser")
192+
}
193+
190194
// log.Debug("Browser: OpenWindow", "URL", location)
191195
resp, err := b.Client.Get(location)
192196
if err != nil {
@@ -245,16 +249,21 @@ func (b *Browser) createOptions(location string) WindowOptions {
245249
// Note: If a browser is initialized by passing a [context.Context] to the
246250
// [WithContext] option, it will be closed if the context is cancelled.
247251
func (b *Browser) Close() {
252+
b.logger().Info("Browser: Close()")
248253
b.closeLock.Lock()
249254
defer b.closeLock.Unlock()
255+
if b.Closed() {
256+
return
257+
}
250258

251-
b.logger().Info("Browser: Close()")
252259
for _, win := range b.windows {
253260
win.Close()
254261
}
255262
if b.ScriptHost != nil && b.ownsHost {
256263
b.ScriptHost.Close()
257264
}
265+
b.windows = nil
266+
b.ScriptHost = nil
258267
b.closed = true
259268
}
260269

0 commit comments

Comments
 (0)