Skip to content

Commit ced42ed

Browse files
authored
fix(bridge): close rpc on disconnect (#129)
## Description Close the bridge RPC channel when the app WebSocket closes so in-flight RPC calls fail immediately instead of waiting for a timeout. ## Related Issue None. ## Context The bridge already observes app socket disconnects, but pending birpc calls were left alive until timeout. Closing the RPC channel on disconnect gives higher layers a faster and more accurate signal that the app session is gone, which is especially useful when the app crashes or drops the bridge unexpectedly.
1 parent 7c63e22 commit ced42ed

2 files changed

Lines changed: 24 additions & 2 deletions

File tree

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@react-native-harness/bridge': patch
3+
---
4+
5+
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.

packages/bridge/src/server.ts

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -140,13 +140,16 @@ export const createHarnessBridge = async (
140140
wss.on('connection', (ws: WebSocket) => {
141141
bridgeLogger.debug('app connected');
142142
const binaryStore = new BinaryStore();
143+
let readyConnection: AppConnection | null = null;
144+
let disconnected = false;
143145

144146
const serverFunctions: BridgeServerFunctions = {
145147
reportReady: (device) => {
146148
const conn: AppConnection = {
147149
device,
148150
runTests: (testPath, opts) => rpc.runTests(testPath, opts),
149151
};
152+
readyConnection = conn;
150153
currentConnection = conn;
151154
bridgeLogger.debug(
152155
'app ready: platform=%s model=%s',
@@ -209,11 +212,25 @@ export const createHarnessBridge = async (
209212
},
210213
);
211214

212-
ws.on('close', () => {
215+
const disconnect = (reason?: Error) => {
216+
if (disconnected) return;
217+
disconnected = true;
218+
213219
bridgeLogger.debug('app disconnected');
214220
binaryStore.dispose();
215-
currentConnection = null;
221+
if (currentConnection === readyConnection) {
222+
currentConnection = null;
223+
}
224+
rpc.$close(reason ?? new Error('App bridge disconnected'));
216225
emitter.emit('disconnected');
226+
};
227+
228+
ws.on('close', () => {
229+
disconnect();
230+
});
231+
232+
ws.on('error', (error) => {
233+
disconnect(error instanceof Error ? error : new Error('App bridge socket error'));
217234
});
218235
});
219236

0 commit comments

Comments
 (0)