fix(driver): stop sending protocol messages after disconnect (#4688)

When the client only closes the input pipe, we are still
sending protocol messages over the output pipe. This could
probably lead to some errors, e.g. write buffer being full.
This commit is contained in:
Dmitry Gozman 2020-12-16 14:21:59 -08:00 committed by GitHub
parent 94f5002ae4
commit 7385b31f13
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 9 additions and 3 deletions

View File

@ -50,15 +50,17 @@ export function runServer() {
const dispatcherConnection = new DispatcherConnection(); const dispatcherConnection = new DispatcherConnection();
const transport = new Transport(process.stdout, process.stdin); const transport = new Transport(process.stdout, process.stdin);
transport.onmessage = message => dispatcherConnection.dispatch(JSON.parse(message));
dispatcherConnection.onmessage = message => transport.send(JSON.stringify(message));
transport.onclose = async () => { transport.onclose = async () => {
// Drop any messages during shutdown on the floor.
dispatcherConnection.onmessage = () => {};
// Force exit after 30 seconds. // Force exit after 30 seconds.
setTimeout(() => process.exit(0), 30000); setTimeout(() => process.exit(0), 30000);
// Meanwhile, try to gracefully close all browsers. // Meanwhile, try to gracefully close all browsers.
await gracefullyCloseAll(); await gracefullyCloseAll();
process.exit(0); process.exit(0);
}; };
transport.onmessage = message => dispatcherConnection.dispatch(JSON.parse(message));
dispatcherConnection.onmessage = message => transport.send(JSON.stringify(message));
const playwright = new Playwright(__dirname, require('../browsers.json')['browsers']); const playwright = new Playwright(__dirname, require('../browsers.json')['browsers']);
new PlaywrightDispatcher(dispatcherConnection.rootDispatcher(), playwright); new PlaywrightDispatcher(dispatcherConnection.rootDispatcher(), playwright);

View File

@ -47,7 +47,11 @@ export class Transport {
this._endian = endian; this._endian = endian;
this._closeableStream = closeable; this._closeableStream = closeable;
pipeRead.on('data', buffer => this._dispatch(buffer)); pipeRead.on('data', buffer => this._dispatch(buffer));
pipeRead.on('close', () => this.onclose && this.onclose()); pipeRead.on('close', () => {
this._closed = true;
if (this.onclose)
this.onclose();
});
this.onmessage = undefined; this.onmessage = undefined;
this.onclose = undefined; this.onclose = undefined;
} }