Skip to content

Commit ad58e49

Browse files
authored
Revert "feat(firefox): migrate to the pipe channel (#4068)" (#4073)
Mac sporadically hangs on browser close.
1 parent 1fe3c78 commit ad58e49

File tree

7 files changed

+38
-21
lines changed

7 files changed

+38
-21
lines changed

browsers.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
},
99
{
1010
"name": "firefox",
11-
"revision": "1184",
11+
"revision": "1182",
1212
"download": true
1313
},
1414
{

src/server/browserType.ts

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,9 @@ import * as path from 'path';
2020
import * as util from 'util';
2121
import { BrowserContext, normalizeProxySettings, validateBrowserContextOptions } from './browserContext';
2222
import * as browserPaths from '../utils/browserPaths';
23-
import { ConnectionTransport } from './transport';
23+
import { ConnectionTransport, WebSocketTransport } from './transport';
2424
import { BrowserOptions, Browser, BrowserProcess } from './browser';
25-
import { launchProcess, Env, envArrayToObject } from './processLauncher';
25+
import { launchProcess, Env, waitForLine, envArrayToObject } from './processLauncher';
2626
import { PipeTransport } from './pipeTransport';
2727
import { Progress, ProgressController } from './progress';
2828
import * as types from './types';
@@ -35,18 +35,22 @@ const mkdtempAsync = util.promisify(fs.mkdtemp);
3535
const existsAsync = (path: string): Promise<boolean> => new Promise(resolve => fs.stat(path, err => resolve(!err)));
3636
const DOWNLOADS_FOLDER = path.join(os.tmpdir(), 'playwright_downloads-');
3737

38+
type WebSocketNotPipe = { webSocketRegex: RegExp, stream: 'stdout' | 'stderr' };
39+
3840
export abstract class BrowserType {
3941
private _name: string;
4042
private _executablePath: string;
43+
private _webSocketNotPipe: WebSocketNotPipe | null;
4144
private _browserDescriptor: browserPaths.BrowserDescriptor;
4245
readonly _browserPath: string;
4346

44-
constructor(packagePath: string, browser: browserPaths.BrowserDescriptor) {
47+
constructor(packagePath: string, browser: browserPaths.BrowserDescriptor, webSocketOrPipe: WebSocketNotPipe | null) {
4548
this._name = browser.name;
4649
const browsersPath = browserPaths.browsersPath(packagePath);
4750
this._browserDescriptor = browser;
4851
this._browserPath = browserPaths.browserDirectory(browsersPath, browser);
4952
this._executablePath = browserPaths.executablePath(this._browserPath, browser) || '';
53+
this._webSocketNotPipe = webSocketOrPipe;
5054
}
5155

5256
executablePath(): string {
@@ -171,6 +175,7 @@ export abstract class BrowserType {
171175
handleSIGTERM,
172176
handleSIGHUP,
173177
progress,
178+
pipe: !this._webSocketNotPipe,
174179
tempDirectories,
175180
attemptToGracefullyClose: async () => {
176181
if ((options as any).__testHookGracefullyClose)
@@ -193,8 +198,14 @@ export abstract class BrowserType {
193198
};
194199
progress.cleanupWhenAborted(() => browserProcess && closeOrKill(browserProcess, progress.timeUntilDeadline()));
195200

196-
const stdio = launchedProcess.stdio as unknown as [NodeJS.ReadableStream, NodeJS.WritableStream, NodeJS.WritableStream, NodeJS.WritableStream, NodeJS.ReadableStream];
197-
transport = new PipeTransport(stdio[3], stdio[4]);
201+
if (this._webSocketNotPipe) {
202+
const match = await waitForLine(progress, launchedProcess, this._webSocketNotPipe.stream === 'stdout' ? launchedProcess.stdout : launchedProcess.stderr, this._webSocketNotPipe.webSocketRegex);
203+
const innerEndpoint = match[1];
204+
transport = await WebSocketTransport.connect(progress, innerEndpoint);
205+
} else {
206+
const stdio = launchedProcess.stdio as unknown as [NodeJS.ReadableStream, NodeJS.WritableStream, NodeJS.WritableStream, NodeJS.WritableStream, NodeJS.ReadableStream];
207+
transport = new PipeTransport(stdio[3], stdio[4]);
208+
}
198209
return { browserProcess, downloadsPath, transport };
199210
}
200211

src/server/chromium/chromium.ts

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,18 @@ import { isDebugMode, getFromENV } from '../../utils/utils';
3131

3232
export class Chromium extends BrowserType {
3333
private _devtools: CRDevTools | undefined;
34+
private _debugPort: number | undefined;
3435

3536
constructor(packagePath: string, browser: BrowserDescriptor) {
36-
super(packagePath, browser);
37+
const debugPortStr = getFromENV('PLAYWRIGHT_CHROMIUM_DEBUG_PORT');
38+
const debugPort: number | undefined = debugPortStr ? +debugPortStr : undefined;
39+
if (debugPort !== undefined) {
40+
if (Number.isNaN(debugPort))
41+
throw new Error(`PLAYWRIGHT_CHROMIUM_DEBUG_PORT must be a number, but is set to "${debugPortStr}"`);
42+
}
43+
44+
super(packagePath, browser, debugPort ? { webSocketRegex: /^DevTools listening on (ws:\/\/.*)$/, stream: 'stderr' } : null);
45+
this._debugPort = debugPort;
3746
if (isDebugMode())
3847
this._devtools = this._createDevTools();
3948
}
@@ -101,16 +110,10 @@ export class Chromium extends BrowserType {
101110
throw new Error('Arguments can not specify page to be opened');
102111
const chromeArguments = [...DEFAULT_ARGS];
103112
chromeArguments.push(`--user-data-dir=${userDataDir}`);
104-
105-
const debugPortStr = getFromENV('PLAYWRIGHT_CHROMIUM_DEBUG_PORT');
106-
if (debugPortStr) {
107-
const debugPort = +debugPortStr;
108-
if (Number.isNaN(debugPort))
109-
throw new Error(`PLAYWRIGHT_CHROMIUM_DEBUG_PORT must be a number, but is set to "${debugPortStr}"`);
110-
chromeArguments.push('--remote-debugging-port=' + debugPort);
111-
}
112-
113-
chromeArguments.push('--remote-debugging-pipe');
113+
if (this._debugPort !== undefined)
114+
chromeArguments.push('--remote-debugging-port=' + this._debugPort);
115+
else
116+
chromeArguments.push('--remote-debugging-pipe');
114117
if (options.devtools)
115118
chromeArguments.push('--auto-open-devtools-for-tabs');
116119
if (options.headless) {

src/server/electron/electron.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,7 @@ export class Electron {
163163
handleSIGTERM,
164164
handleSIGHUP,
165165
progress,
166+
pipe: true,
166167
cwd: options.cwd,
167168
tempDirectories: [],
168169
attemptToGracefullyClose: () => app!.close(),

src/server/firefox/firefox.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,8 @@ import * as types from '../types';
2929

3030
export class Firefox extends BrowserType {
3131
constructor(packagePath: string, browser: BrowserDescriptor) {
32-
super(packagePath, browser);
32+
const webSocketRegex = /^Juggler listening on (ws:\/\/.*)$/;
33+
super(packagePath, browser, { webSocketRegex, stream: 'stdout' });
3334
}
3435

3536
_connectToTransport(transport: ConnectionTransport, options: BrowserOptions): Promise<FFBrowser> {
@@ -81,7 +82,7 @@ export class Firefox extends BrowserType {
8182
firefoxArguments.push('-foreground');
8283
}
8384
firefoxArguments.push(`-profile`, userDataDir);
84-
firefoxArguments.push('-juggler-pipe');
85+
firefoxArguments.push('-juggler', '0');
8586
firefoxArguments.push(...args);
8687
if (isPersistent)
8788
firefoxArguments.push('about:blank');

src/server/processLauncher.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ export type LaunchProcessOptions = {
3535
handleSIGINT?: boolean,
3636
handleSIGTERM?: boolean,
3737
handleSIGHUP?: boolean,
38+
pipe?: boolean,
3839
pipeStdin?: boolean,
3940
tempDirectories: string[],
4041

@@ -82,7 +83,7 @@ export async function launchProcess(options: LaunchProcessOptions): Promise<Laun
8283
const cleanup = () => helper.removeFolders(options.tempDirectories);
8384

8485
const progress = options.progress;
85-
const stdio: ('ignore' | 'pipe')[] = ['ignore', 'pipe', 'pipe', 'pipe', 'pipe'];
86+
const stdio: ('ignore' | 'pipe')[] = options.pipe ? ['ignore', 'pipe', 'pipe', 'pipe', 'pipe'] : ['ignore', 'pipe', 'pipe'];
8687
if (options.pipeStdin)
8788
stdio[0] = 'pipe';
8889
progress.log(`<launching> ${options.executablePath} ${options.args.join(' ')}`);

src/server/webkit/webkit.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ import * as types from '../types';
2727

2828
export class WebKit extends BrowserType {
2929
constructor(packagePath: string, browser: BrowserDescriptor) {
30-
super(packagePath, browser);
30+
super(packagePath, browser, null /* use pipe not websocket */);
3131
}
3232

3333
_connectToTransport(transport: ConnectionTransport, options: BrowserOptions): Promise<WKBrowser> {

0 commit comments

Comments
 (0)