Skip to content

Commit d0a26a7

Browse files
yungstersFacebook Github Bot 3
authored and
Facebook Github Bot 3
committed
RN: Fix Exponential WebSocket Growth from DevTools
Summary:025281230de2e316f2770a2db71f8bec6fe43a07 changed `WebSocket` to fire both `onerror` and `onclose`. However, `setupDevtools` assumed that only one of the two handlers would ever be invoked. When the handler is invoked, it re-invokes itself to keep a socket open. But since both handelrs are invoked, a series of closed or failed sockets can cause an exponential increase in sockets opened. Symptoms of this bug include eventually reaching the maximum number of file descriptors allowed by the operating system, or a non-responsive system. Within React Native applications, symptoms include a failure to load resources from the React Native server or application crashes. Reviewed By: frantic Differential Revision: D3022697 fb-gh-sync-id: 8131ecbd6d8ba30281253340d30370ff511a5efd shipit-source-id: 8131ecbd6d8ba30281253340d30370ff511a5efd
1 parent 72b274a commit d0a26a7

File tree

1 file changed

+11
-8
lines changed

1 file changed

+11
-8
lines changed

Libraries/Devtools/setupDevtools.js

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -30,18 +30,21 @@ function setupDevtools() {
3030
},
3131
},
3232
};
33-
ws.onclose = () => {
34-
setTimeout(setupDevtools, 200);
35-
closeListeners.forEach(fn => fn());
36-
};
37-
ws.onerror = error => {
38-
setTimeout(setupDevtools, 200);
39-
closeListeners.forEach(fn => fn());
40-
};
33+
ws.onclose = handleClose;
34+
ws.onerror = handleClose;
4135
ws.onopen = function () {
4236
tryToConnect();
4337
};
4438

39+
var hasClosed = false;
40+
function handleClose() {
41+
if (!hasClosed) {
42+
hasClosed = true;
43+
setTimeout(setupDevtools, 200);
44+
closeListeners.forEach(fn => fn());
45+
}
46+
}
47+
4548
function tryToConnect() {
4649
ws.send('attach:agent');
4750
var _interval = setInterval(() => ws.send('attach:agent'), 500);

0 commit comments

Comments
 (0)