Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ import {createDeviceMock} from './InspectorDeviceUtils';
import {sendFromDebuggerToTarget} from './InspectorProtocolUtils';
import {withAbortSignalForEachTest} from './ResourceUtils';
import {withServerForEachTest} from './ServerUtils';
import nullthrows from 'nullthrows';
import until from 'wait-for-expect';

// WebSocket is unreliable when using fake timers.
Expand Down Expand Up @@ -155,136 +154,6 @@ describe.each(['HTTP', 'HTTPS'])(
}
});

test('multiple debuggers to the same page on the same device', async () => {
Comment thread
huntie marked this conversation as resolved.
Resolved
let device, debugger1, debugger2;
try {
// Connect a device to the proxy.
device = await createDeviceMock(
`${serverRef.serverBaseWsUrl}/inspector/device?device=device1&name=foo&app=bar`,
autoCleanup.signal,
);
// Capture a log of events so we can assert on their order later.
const events: Array<
| ConnectRequest
| DisconnectRequest
| {event: 'create-debugger-mock', name: string},
> = [];
device.disconnect.mockImplementation(message => {
events.push(message);
});
device.connect.mockImplementation(message => {
events.push(message);
});
// Set up the page.
device.getPages.mockImplementation(() => [
{
app: 'bar-app',
id: 'page1',
title: 'bar-title',
vm: 'bar-vm',
},
]);
let pageList: Array<PageDescription> = [];
await until(async () => {
pageList = (await fetchJson(
`${serverRef.serverBaseUrl}/json`,
// $FlowFixMe[unclear-type]
)) as any;
expect(pageList).toHaveLength(1);
});
const [{webSocketDebuggerUrl}] = pageList;

// Connect the first debugger and send a message.
events.push({event: 'create-debugger-mock', name: 'debugger1'});
debugger1 = await createDebuggerMock(
webSocketDebuggerUrl,
autoCleanup.signal,
);
await sendFromDebuggerToTarget(debugger1, device, 'page1', {
method: 'Runtime.enable',
id: 0,
});

// Connect the second debugger.
events.push({event: 'create-debugger-mock', name: 'debugger2'});
debugger2 = await createDebuggerMock(
webSocketDebuggerUrl,
autoCleanup.signal,
);

// The first debugger gets disconnected. TODO: In the future, we should
// amend the protocol to allow for multiple debuggers to connect at the
// same time.
await until(async () => {
expect([
// CLOSING
3,
// CLOSED
4,
]).toContain(debugger1.socket.readyState);
});

// Send a message from the second debugger.
await sendFromDebuggerToTarget(debugger2, device, 'page1', {
method: 'Debugger.enable',
id: 1,
});

// Check the order of `connect` and `disconnect` events received by the
// device. `create-debugger-mock` events are included for convenience.
expect(events).toEqual([
{
event: 'create-debugger-mock',
name: 'debugger1',
},
{
event: 'connect',
payload: {
pageId: 'page1',
sessionId: expect.any(String),
},
},
{
event: 'create-debugger-mock',
name: 'debugger2',
},
{
// NOTE: For debugger1
event: 'disconnect',
payload: {
pageId: 'page1',
sessionId: expect.any(String),
},
},
{
// NOTE: For debugger2
event: 'connect',
payload: {
pageId: 'page1',
sessionId: expect.any(String),
},
},
]);
// Verify that disconnect and second connect use different session IDs
// (disconnect is for first debugger, connect is for second debugger)
const disconnectEvent = nullthrows(
events.find(e => e.event === 'disconnect'),
);
const connectEvents = events.filter(e => e.event === 'connect');
expect(connectEvents).toHaveLength(2);
expect(disconnectEvent.payload.sessionId).toBe(
connectEvents[0].payload.sessionId,
);
expect(disconnectEvent?.payload.sessionId).not.toBe(
connectEvents[1].payload.sessionId,
);
} finally {
device?.close();
debugger1?.close();
debugger2?.close();
}
});

test('debugger connection to a nonexistent page is rejected', async () => {
let device, debugger_;
try {
Expand Down Expand Up @@ -665,23 +534,19 @@ describe.each(['HTTP', 'HTTPS'])(
}
});

test('page with supportsMultipleDebuggers allows concurrent debugger connections', async () => {
test('allows concurrent debugger connections to a page', async () => {
let device, debugger1, debugger2;
try {
device = await createDeviceMock(
`${serverRef.serverBaseWsUrl}/inspector/device?device=device1&name=foo&app=bar`,
autoCleanup.signal,
);
// Page with supportsMultipleDebuggers capability
device.getPages.mockImplementation(() => [
{
app: 'bar-app',
id: 'page1',
title: 'bar-title',
vm: 'bar-vm',
capabilities: {
supportsMultipleDebuggers: true,
},
},
]);
let pageList: Array<PageDescription> = [];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,11 @@
* @format
*/

import type {JsonPagesListResponse} from '../inspector-proxy/types';
import type {
ConnectRequest,
DisconnectRequest,
JsonPagesListResponse,
} from '../inspector-proxy/types';

import {fetchJson} from './FetchUtils';
import {createDebuggerMock} from './InspectorDebuggerUtils';
Expand All @@ -26,7 +30,7 @@ describe('inspector proxy concurrent sessions', () => {
const autoCleanup = withAbortSignalForEachTest();

describe('enableStandaloneFuseboxShell experiment disabled', () => {
test('page reporting supportsMultipleDebuggers:true is treated as false', async () => {
test('connecting a second debugger disconnects the first', async () => {
// Create a server with the experiment disabled
const {server} = await createServer({
logger: undefined,
Expand All @@ -39,20 +43,23 @@ describe('inspector proxy concurrent sessions', () => {

let device, debugger1, debugger2;
try {
// Connect a device with a page that explicitly reports supportsMultipleDebuggers: true
device = await createDeviceMock(
`${serverBaseWsUrl}/inspector/device?device=device1&name=foo&app=bar`,
autoCleanup.signal,
);
const events: Array<ConnectRequest | DisconnectRequest> = [];
device.connect.mockImplementation(message => {
events.push(message);
});
device.disconnect.mockImplementation(message => {
events.push(message);
});
device.getPages.mockImplementation(() => [
{
id: 'page1',
app: 'bar-app',
title: 'bar-title',
vm: 'bar-vm',
capabilities: {
supportsMultipleDebuggers: true,
},
},
]);

Expand All @@ -66,11 +73,6 @@ describe('inspector proxy concurrent sessions', () => {
expect(pageList).toHaveLength(1);
});

// Verify the capability is reported as false in the page list
expect(
pageList[0].reactNative?.capabilities?.supportsMultipleDebuggers,
).toBe(false);

const webSocketDebuggerUrl = pageList[0].webSocketDebuggerUrl;

// Connect first debugger
Expand All @@ -95,9 +97,23 @@ describe('inspector proxy concurrent sessions', () => {
// Second debugger should be open
expect(debugger2.socket.readyState).toBe(1); // OPEN

// Device should have received disconnect for first session and connect for second
await until(() => expect(device.disconnect).toHaveBeenCalledTimes(1));
await until(() => expect(device.connect).toHaveBeenCalledTimes(2));
// Device should receive disconnect for the first session before
// connect for the second
await until(() => expect(events).toHaveLength(3));
const [connect1, disconnect1, connect2] = events;
expect(connect1).toEqual({
event: 'connect',
payload: {pageId: 'page1', sessionId: expect.any(String)},
});
expect(disconnect1).toEqual({
event: 'disconnect',
payload: {pageId: 'page1', sessionId: connect1.payload.sessionId},
});
expect(connect2).toEqual({
event: 'connect',
payload: {pageId: 'page1', sessionId: expect.any(String)},
});
expect(connect2.payload.sessionId).not.toBe(connect1.payload.sessionId);
} finally {
device?.close();
debugger1?.close();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,6 @@ describe('inspector-proxy device socket handoff', () => {
test('device ID collision with multiple debuggers connected restores all connections', async () => {
let device1, device2, debugger1, debugger2, webSocketDebuggerUrl;
try {
// Connect device with a page that supports multiple debuggers
({
device: device1,
pageList: [{webSocketDebuggerUrl}],
Expand All @@ -161,9 +160,6 @@ describe('inspector-proxy device socket handoff', () => {
{
...PAGE_DEFAULTS,
vm: 'bar-vm',
capabilities: {
supportsMultipleDebuggers: true,
},
},
],
));
Expand Down Expand Up @@ -192,9 +188,6 @@ describe('inspector-proxy device socket handoff', () => {
{
...PAGE_DEFAULTS,
vm: 'bar-vm-updated',
capabilities: {
supportsMultipleDebuggers: true,
},
},
],
));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -199,9 +199,7 @@ describe('inspector proxy HTTP API', () => {
devtoolsFrontendUrl: expect.any(String),
id: 'device1-page1',
reactNative: {
capabilities: {
supportsMultipleDebuggers: false,
},
capabilities: {},
logicalDeviceId: 'device1',
},
title: 'bar-title',
Expand All @@ -216,9 +214,7 @@ describe('inspector proxy HTTP API', () => {
devtoolsFrontendUrl: expect.any(String),
id: 'device2-page1',
reactNative: {
capabilities: {
supportsMultipleDebuggers: false,
},
capabilities: {},
logicalDeviceId: 'device2',
},
title: 'bar-title',
Expand Down
24 changes: 5 additions & 19 deletions packages/dev-middleware/src/inspector-proxy/Device.js
Original file line number Diff line number Diff line change
Expand Up @@ -369,10 +369,7 @@ export default class Device {
// Clear any commands we were waiting on.
this.#deviceEventReporter?.logDisconnection('debugger');

// Check if this specific page supports multiple debuggers.
// If not (legacy mode), disconnect existing debuggers for THIS PAGE only
// before connecting the new one.
if (!this.#pageHasCapability(page, 'supportsMultipleDebuggers')) {
if (!this.#experiments.enableStandaloneFuseboxShell) {
for (const [sid, conn] of this.#debuggerConnections) {
if (conn.pageId === pageId) {
this.#terminateDebuggerConnection(
Expand Down Expand Up @@ -554,22 +551,11 @@ export default class Device {
#handleMessageFromDevice(message: MessageFromDevice) {
if (message.event === 'getPages') {
// Preserve ordering - getPages guarantees addition order.
const shouldDisableMultipleDebuggers =
!this.#experiments.enableStandaloneFuseboxShell;
this.#pages = new Map(
message.payload.map(({capabilities: rawCapabilities, ...page}) => {
const capabilities: TargetCapabilityFlags =
shouldDisableMultipleDebuggers
? {...(rawCapabilities ?? {}), supportsMultipleDebuggers: false}
: (rawCapabilities ?? {});
return [
page.id,
{
...page,
capabilities,
},
];
}),
message.payload.map(({capabilities, ...page}) => [
page.id,
{...page, capabilities: capabilities ?? {}},
]),
);

if (message.payload.length !== this.#pages.size) {
Expand Down
28 changes: 6 additions & 22 deletions packages/dev-middleware/src/inspector-proxy/__docs__/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -127,14 +127,10 @@ interface Page {
capabilities?: {
nativePageReloads?: boolean; // Target keeps the socket open across reloads
nativeSourceCodeFetching?: boolean; // Target supports Network.loadNetworkResource
supportsMultipleDebuggers?: boolean; // Supports concurrent debugger sessions
};
}
```

**Note**: The value of `supportsMultipleDebuggers` SHOULD be consistent across
all pages for a given device.

### Connection Lifecycle

**Device Registration:**
Expand Down Expand Up @@ -186,29 +182,17 @@ its page, if `sessionId` is omitted).

#### Multi-Debugger Support

Multiple debuggers can connect simultaneously to the same page when **both** the
proxy and device support session multiplexing:

1. **Session IDs**: The proxy assigns a unique, non-empty `sessionId` to each
debugger connection. All messages include this `sessionId` for routing. This
SHOULD be a UUID or other suitably unique and ephemeral identifier.

2. **Capability Detection**: Devices report `supportsMultipleDebuggers: true` in
their page capabilities to indicate session support.

3. **Backwards Compatibility**: Legacy devices ignore `sessionId` fields in
incoming messages and don't include them in responses.
Multiple debuggers can connect simultaneously to the same page. The proxy
assigns a unique, non-empty `sessionId` to each debugger connection. All
messages include this `sessionId` for routing. This SHOULD be a UUID or other
suitably unique and ephemeral identifier.

#### Connection Rules

1. **Session-Capable Device**: Multiple debuggers can connect to the same page
1. **Concurrent Sessions**: Multiple debuggers can connect to the same page
simultaneously. Each connection has an independent session.

2. **Legacy Device (no `supportsMultipleDebuggers`)**: New debugger connections
to an already-connected page disconnect the existing debugger. The proxy MUST
NOT allow multiple debuggers to connect to the same page.

3. **Device Reconnection**: If a device reconnects with the same `device` ID
2. **Device Reconnection**: If a device reconnects with the same `device` ID
while debugger connections to the same logical device are open in the proxy,
the proxy may attempt to preserve active debugger sessions by forwarding them
to the new device.
Expand Down
Loading
Loading