From e61bef5f650d3e8453cc47c2f0b0533a68f7b88f Mon Sep 17 00:00:00 2001 From: Yad Smood Date: Fri, 29 Apr 2022 15:28:03 +0800 Subject: [PATCH] fix browser event leaking --- browser.go | 4 +++- browser_test.go | 15 +++++++++++++++ 2 files changed, 18 insertions(+), 1 deletion(-) diff --git a/browser.go b/browser.go index 0664eafe..80af7578 100644 --- a/browser.go +++ b/browser.go @@ -432,10 +432,12 @@ func (b *Browser) Event() <-chan *Message { } func (b *Browser) initEvents() { - b.event = goob.New(b.ctx) + ctx, cancel := context.WithCancel(b.ctx) + b.event = goob.New(ctx) event := b.client.Event() go func() { + defer cancel() for e := range event { b.event.Publish(&Message{ SessionID: proto.TargetSessionID(e.SessionID), diff --git a/browser_test.go b/browser_test.go index 2d425333..7629f16a 100644 --- a/browser_test.go +++ b/browser_test.go @@ -452,3 +452,18 @@ func TestOldBrowser(t *testing.T) { g.E(err) g.Eq(res.Revision, "@19d4547535ab5aba70b4730443f84e8153052174") } + +func TestBrowserLostConnection(t *testing.T) { + g := setup(t) + + l := launcher.New() + p := rod.New().ControlURL(l.MustLaunch()).MustConnect().MustPage(g.blank()) + + go func() { + utils.Sleep(1) + l.Kill() + }() + + _, err := p.Eval(`() => new Promise(r => {})`) + g.Err(err) +}