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
5 changes: 5 additions & 0 deletions .changeset/bridge-close-rpc-on-disconnect.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@react-native-harness/bridge': patch
---

Close the bridge RPC channel as soon as the app WebSocket disconnects so in-flight calls fail immediately instead of waiting for a timeout. This makes bridge shutdowns easier to detect and gives higher layers a faster, more accurate signal when the app session disappears.
21 changes: 19 additions & 2 deletions packages/bridge/src/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -140,13 +140,16 @@ export const createHarnessBridge = async (
wss.on('connection', (ws: WebSocket) => {
bridgeLogger.debug('app connected');
const binaryStore = new BinaryStore();
let readyConnection: AppConnection | null = null;
let disconnected = false;

const serverFunctions: BridgeServerFunctions = {
reportReady: (device) => {
const conn: AppConnection = {
device,
runTests: (testPath, opts) => rpc.runTests(testPath, opts),
};
readyConnection = conn;
currentConnection = conn;
bridgeLogger.debug(
'app ready: platform=%s model=%s',
Expand Down Expand Up @@ -209,11 +212,25 @@ export const createHarnessBridge = async (
},
);

ws.on('close', () => {
const disconnect = (reason?: Error) => {
if (disconnected) return;
disconnected = true;

bridgeLogger.debug('app disconnected');
binaryStore.dispose();
currentConnection = null;
if (currentConnection === readyConnection) {
currentConnection = null;
}
rpc.$close(reason ?? new Error('App bridge disconnected'));
emitter.emit('disconnected');
};

ws.on('close', () => {
disconnect();
});

ws.on('error', (error) => {
disconnect(error instanceof Error ? error : new Error('App bridge socket error'));
});
});

Expand Down
Loading