From ef85afeb706b11cf65bd6bd67206bd8a8068aaf8 Mon Sep 17 00:00:00 2001 From: Can Stand Date: Fri, 2 Jun 2023 21:03:52 +0800 Subject: [PATCH] fix: may hang on unexpect browser close --- browser_type.go | 1 + connection.go | 21 ++++++++++++++++++++- 2 files changed, 21 insertions(+), 1 deletion(-) diff --git a/browser_type.go b/browser_type.go index b12bc195..02cb17e7 100644 --- a/browser_type.go +++ b/browser_type.go @@ -84,6 +84,7 @@ func (b *browserTypeImpl) Connect(url string, options ...BrowserTypeConnectOptio context.(*browserContextImpl).onClose() } browser.onClose() + connection.cleanup() } jsonPipe.On("closed", pipeClosed) connection.onmessage = func(message map[string]interface{}) error { diff --git a/connection.go b/connection.go index afa73cc1..60c9eeea 100644 --- a/connection.go +++ b/connection.go @@ -42,7 +42,26 @@ func (c *connection) Start() *Playwright { } func (c *connection) Stop() error { - return c.onClose() + err := c.onClose() + if err != nil { + return err + } + c.cleanup() + return nil +} + +func (c *connection) cleanup() { + c.callbacks.Range(func(key, value any) bool { + select { + case value.(chan callback) <- callback{ + Error: &Error{ + Name: "Error", + Message: "Connection closed", + }}: + default: + } + return true + }) } func (c *connection) Dispatch(msg *message) {