Skip to content

Commit 2abb2a0

Browse files
authored
chore: do not bind remote browsers twice (#39627)
1 parent c54c03a commit 2abb2a0

8 files changed

Lines changed: 29 additions & 9 deletions

File tree

packages/playwright-core/src/cli/daemon/program.ts

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -53,12 +53,10 @@ export function decorateCLICommand(command: Command, version: string) {
5353
const socketPath = await startCliDaemonServer(sessionName, browserContext, mcpConfig, clientInfo, { persistent, exitOnClose: true });
5454
console.log(`### Success\nDaemon listening on ${socketPath}`);
5555
console.log('<EOF>');
56-
try {
56+
57+
if (!(browser as any)._connection.isRemote()) {
5758
await (browser as any)._startServer(sessionName, { workspaceDir: clientInfo.workspaceDir });
5859
browserContext.on('close', () => (browser as any)._stopServer().catch(() => {}));
59-
} catch (error) {
60-
if (!error.message.includes('Server is already running'))
61-
throw error;
6260
}
6361
} catch (error) {
6462
const message = process.env.PWDEBUGIMPL ? (error as Error).stack || (error as Error).message : (error as Error).message;

packages/playwright-core/src/client/browser.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ import { mkdirIfNeeded } from './fileUtils';
2424

2525
import type { BrowserType } from './browserType';
2626
import type { Page } from './page';
27-
import type { BrowserContextOptions, LaunchOptions, Logger } from './types';
27+
import type { BrowserContextOptions, LaunchOptions, Logger, StartServerOptions } from './types';
2828
import type * as api from '../../types/types';
2929
import type * as channels from '@protocol/channels';
3030

@@ -130,7 +130,7 @@ export class Browser extends ChannelOwner<channels.BrowserChannel> implements ap
130130
return this._initializer.version;
131131
}
132132

133-
async _startServer(title: string, options: { wsPath?: string, workspaceDir?: string } = {}): Promise<{ wsEndpoint?: string, pipeName?: string }> {
133+
async _startServer(title: string, options: StartServerOptions = {}): Promise<{ wsEndpoint?: string, pipeName?: string }> {
134134
return await this._channel.startServer({ title, ...options });
135135
}
136136

packages/playwright-core/src/client/types.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,14 @@ export type LaunchAndroidServerOptions = {
120120
wsPath?: string,
121121
};
122122

123+
export type StartServerOptions = {
124+
host?: string,
125+
port?: number,
126+
wsPath?: string,
127+
workspaceDir?: string,
128+
metadata?: Record<string, any>,
129+
};
130+
123131
export type SelectorEngine = {
124132
/**
125133
* Returns the first element matching given selector in the root's subtree.

packages/playwright-core/src/protocol/validator.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -652,8 +652,11 @@ scheme.BrowserContextEvent = tObject({
652652
scheme.BrowserCloseEvent = tOptional(tObject({}));
653653
scheme.BrowserStartServerParams = tObject({
654654
title: tString,
655+
host: tOptional(tString),
656+
port: tOptional(tInt),
655657
wsPath: tOptional(tString),
656658
workspaceDir: tOptional(tString),
659+
metadata: tOptional(tAny),
657660
});
658661
scheme.BrowserStartServerResult = tObject({
659662
wsEndpoint: tOptional(tString),

packages/playwright-core/src/server/browser.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -174,7 +174,7 @@ export abstract class Browser extends SdkObject {
174174
return video?.artifact;
175175
}
176176

177-
async startServer(title: string, options: { workspaceDir?: string, wsPath?: string, pipeName?: string }): Promise<{ wsEndpoint?: string, pipeName?: string }> {
177+
async startServer(title: string, options: channels.BrowserStartServerOptions): Promise<{ wsEndpoint?: string, pipeName?: string }> {
178178
return await this._server.start(title, options);
179179
}
180180

@@ -219,7 +219,7 @@ export class BrowserServer {
219219
this._browser = browser;
220220
}
221221

222-
async start(title: string, options: { workspaceDir?: string, wsPath?: string }): Promise<{ wsEndpoint?: string, pipeName?: string }> {
222+
async start(title: string, options: channels.BrowserStartServerOptions): Promise<{ wsEndpoint?: string, pipeName?: string }> {
223223
if (this._isStarted)
224224
throw new Error(`Server is already started.`);
225225
this._isStarted = true;
@@ -233,7 +233,7 @@ export class BrowserServer {
233233
if (options.wsPath) {
234234
const path = options.wsPath.startsWith('/') ? options.wsPath : `/${options.wsPath}`;
235235
this._wsServer = new PlaywrightWebSocketServer(this._browser, path);
236-
result.wsEndpoint = await this._wsServer.listen(0, 'localhost', path);
236+
result.wsEndpoint = await this._wsServer.listen(options.port ?? 0, options.host ?? 'localhost', path);
237237
}
238238

239239
const browserInfo: BrowserInfo = {
@@ -247,6 +247,7 @@ export class BrowserServer {
247247
wsEndpoint: result.wsEndpoint,
248248
pipeName: result.pipeName,
249249
workspaceDir: options.workspaceDir,
250+
metadata: options.metadata,
250251
});
251252
return result;
252253
}

packages/playwright-core/src/serverRegistry.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ export type EndpointInfo = {
3636
wsEndpoint?: string;
3737
pipeName?: string;
3838
workspaceDir?: string;
39+
metadata?: Record<string, any>;
3940
};
4041

4142
export type BrowserDescriptor = EndpointInfo & {

packages/protocol/src/channels.d.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1181,12 +1181,18 @@ export type BrowserContextEvent = {
11811181
export type BrowserCloseEvent = {};
11821182
export type BrowserStartServerParams = {
11831183
title: string,
1184+
host?: string,
1185+
port?: number,
11841186
wsPath?: string,
11851187
workspaceDir?: string,
1188+
metadata?: any,
11861189
};
11871190
export type BrowserStartServerOptions = {
1191+
host?: string,
1192+
port?: number,
11881193
wsPath?: string,
11891194
workspaceDir?: string,
1195+
metadata?: any,
11901196
};
11911197
export type BrowserStartServerResult = {
11921198
wsEndpoint?: string,

packages/protocol/src/protocol.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1043,8 +1043,11 @@ Browser:
10431043
title: Start server
10441044
parameters:
10451045
title: string
1046+
host: string?
1047+
port: int?
10461048
wsPath: string?
10471049
workspaceDir: string?
1050+
metadata: json?
10481051
returns:
10491052
wsEndpoint: string?
10501053
pipeName: string?

0 commit comments

Comments
 (0)