Skip to content

Commit 2ac2b13

Browse files
committed
Listen to close instead of exit, to improve process handling
Notably useful on Windows, where some processes seem to trigger exit when handing off to another process somehow, whilst close only comes later when the process exits for real. Very noticeable with FF.
1 parent d4f8a75 commit 2ac2b13

File tree

4 files changed

+11
-11
lines changed

4 files changed

+11
-11
lines changed

src/interceptors/fresh-chrome.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ export class FreshChrome implements Interceptor {
7979
await hideWarningServer.stop();
8080

8181
browsers[proxyPort] = browser;
82-
browser.process.once('exit', () => {
82+
browser.process.once('close', () => {
8383
delete browsers[proxyPort];
8484

8585
if (Object.keys(browsers).length === 0 && chromeDetails && _.isString(chromeDetails.profile)) {
@@ -102,7 +102,7 @@ export class FreshChrome implements Interceptor {
102102
async deactivate(proxyPort: number | string) {
103103
if (this.isActive(proxyPort)) {
104104
const browser = browsers[proxyPort];
105-
const exitPromise = new Promise((resolve) => browser!.process.once('exit', resolve));
105+
const exitPromise = new Promise((resolve) => browser!.process.once('close', resolve));
106106
browser!.stop();
107107
await exitPromise;
108108
}

src/interceptors/fresh-firefox.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,7 @@ export class FreshFirefox implements Interceptor {
129129
const certInstalled = certCheckServer.waitForSuccess().catch(reportError);
130130

131131
certInstallBrowser = await this.startFirefox(certCheckServer);
132-
certInstallBrowser.process.once('exit', () => {
132+
certInstallBrowser.process.once('close', () => {
133133
certCheckServer.stop();
134134
certInstallBrowser = undefined;
135135
});
@@ -183,7 +183,7 @@ export class FreshFirefox implements Interceptor {
183183
}).catch(reportError);
184184

185185
browsers[proxyPort] = browser;
186-
browser.process.once('exit', () => {
186+
browser.process.once('close', () => {
187187
certCheckServer.stop();
188188
delete browsers[proxyPort];
189189
if (!success) {
@@ -199,9 +199,9 @@ export class FreshFirefox implements Interceptor {
199199
async deactivate(proxyPort: number | string) {
200200
if (this.isActive(proxyPort)) {
201201
const browser = browsers[proxyPort];
202-
const exitPromise = new Promise((resolve) => browser!.process.once('exit', resolve));
202+
const closePromise = new Promise((resolve) => browser!.process.once('close', resolve));
203203
browser!.stop();
204-
await exitPromise;
204+
await closePromise;
205205
}
206206
}
207207

@@ -211,7 +211,7 @@ export class FreshFirefox implements Interceptor {
211211
);
212212
if (certInstallBrowser) {
213213
certInstallBrowser.stop();
214-
return new Promise((resolve) => certInstallBrowser!.process.once('exit', resolve));
214+
return new Promise((resolve) => certInstallBrowser!.process.once('close', resolve));
215215
}
216216
}
217217
};

src/interceptors/terminal/fresh-terminal-interceptor.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ const spawnAndCollectOutput = (command: string, args: string[] = []): Promise<{
4040
stderr.on('data', (d) => stderrData.push(d));
4141

4242
childProc.once('error', reject);
43-
childProc.once('exit', () => {
43+
childProc.once('close', () => {
4444
// Note that we do _not_ check the error code
4545
resolve({
4646
stdout: Buffer.concat(stdoutData).toString(),
@@ -270,7 +270,7 @@ export class FreshTerminalInterceptor implements Interceptor {
270270
if (_.every(terminals, ts => _.isEmpty(ts))) resetShellStartupScripts();
271271
}, 500);
272272
};
273-
childProc.once('exit', onTerminalClosed);
273+
childProc.once('close', onTerminalClosed);
274274
childProc.once('error', (e) => {
275275
reportError(e);
276276
onTerminalClosed();
@@ -282,7 +282,7 @@ export class FreshTerminalInterceptor implements Interceptor {
282282

283283
await Promise.all((terminals[proxyPort] || []).map((proc) => {
284284
return new Promise((resolve) => {
285-
proc.once('exit', resolve);
285+
proc.once('close', resolve);
286286
proc.kill();
287287
});
288288
}));

test/interceptors/fresh-terminal.spec.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ describe('Fresh terminal interceptor', function () {
5454
env: Object.assign({}, process.env, terminalEnvOverrides)
5555
});
5656
await new Promise((resolve, reject) => {
57-
nodeScript.on('exit', resolve);
57+
nodeScript.on('close', resolve);
5858
nodeScript.on('error', reject);
5959
});
6060

0 commit comments

Comments
 (0)