diff --git a/lib/Page.js b/lib/Page.js index d6fa1f9c8a3..bcf5905fd1b 100644 --- a/lib/Page.js +++ b/lib/Page.js @@ -384,6 +384,7 @@ class Page extends EventEmitter { await this._client.send('Page.navigate', {url, referrer}); } catch (e) { watcher.cancel(); + helper.removeEventListeners([listener]); throw e; } const error = await navigationPromise; diff --git a/test/test.js b/test/test.js index 2337a9a463d..24f4d9f3408 100644 --- a/test/test.js +++ b/test/test.js @@ -753,6 +753,15 @@ describe('Page', function() { process.removeListener('warning', warningHandler); expect(warning).toBe(null); })); + it('should not leak listeners during bad navigation', SX(async function() { + let warning = null; + const warningHandler = w => warning = w; + process.on('warning', warningHandler); + for (let i = 0; i < 20; ++i) + await page.goto('asdf').catch(e => {/* swallow navigation error */}); + process.removeListener('warning', warningHandler); + expect(warning).toBe(null); + })); it('should navigate to dataURL and fire dataURL requests', SX(async function() { const requests = []; page.on('request', request => requests.push(request));