From 3dd5c28f90a9e939489c0541c5aa520564fae280 Mon Sep 17 00:00:00 2001 From: Andrey Lushnikov Date: Wed, 31 Oct 2018 16:31:29 -0700 Subject: [PATCH] fix(pipe): dispatch "disconnected" event when browser is terminated (#3472) --- lib/PipeTransport.js | 6 +++++- test/puppeteer.spec.js | 8 ++++++++ 2 files changed, 13 insertions(+), 1 deletion(-) diff --git a/lib/PipeTransport.js b/lib/PipeTransport.js index 5c54b49278e..a20df8584e3 100644 --- a/lib/PipeTransport.js +++ b/lib/PipeTransport.js @@ -27,7 +27,11 @@ class PipeTransport { this._pipeWrite = pipeWrite; this._pendingMessage = ''; this._eventListeners = [ - helper.addEventListener(pipeRead, 'data', buffer => this._dispatch(buffer)) + helper.addEventListener(pipeRead, 'data', buffer => this._dispatch(buffer)), + helper.addEventListener(pipeRead, 'close', () => { + if (this.onclose) + this.onclose.call(null); + }) ]; this.onmessage = null; this.onclose = null; diff --git a/test/puppeteer.spec.js b/test/puppeteer.spec.js index 65e4b58579b..015d37aebdc 100644 --- a/test/puppeteer.spec.js +++ b/test/puppeteer.spec.js @@ -217,6 +217,14 @@ module.exports.addTests = function({testRunner, expect, defaultBrowserOptions}) await page.close(); await browser.close(); }); + it('should fire "disconnected" when closing with pipe', async() => { + const options = Object.assign({pipe: true}, defaultBrowserOptions); + const browser = await puppeteer.launch(options); + const disconnectedEventPromise = new Promise(resolve => browser.once('disconnected', resolve)); + // Emulate user exiting browser. + browser.process().kill(); + await disconnectedEventPromise; + }); it('should work with no default arguments', async() => { const options = Object.assign({}, defaultBrowserOptions); options.ignoreDefaultArgs = true;