Skip to content

Commit

Permalink
Log Abnormal Closes to Metro Websocket
Browse files Browse the repository at this point in the history
Summary:
We are running into a group seeing frequent disconnects from Metro in a specific office. These are surfaced (at least on iOS) as websocket closures, without a prior websocket error. WebSocket closure can be for a variety of reasons, and the spec for a CloseEvent is to include fields `wasClean`, `code`, and `reason`, with `code` having the most well-defined meaning.

This change makes it so that we emit extra context when the websocket is closed. That should help inform developers the reason behind any close that may be abnormal.

Changelog:
[General][Added] - Log Abnormal Closes to Metro Websocket

Reviewed By: motiz88

Differential Revision: D40660765

fbshipit-source-id: ef606d8d809af1c697a78eb00cc5666c29a8bca3
  • Loading branch information
NickGerleman authored and facebook-github-bot committed Oct 27, 2022
1 parent 279cfec commit 3982a2c
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 3 deletions.
18 changes: 16 additions & 2 deletions Libraries/Utilities/HMRClient.js
Original file line number Diff line number Diff line change
Expand Up @@ -241,9 +241,23 @@ Error: ${e.message}`;
}
});

client.on('close', data => {
client.on('close', closeEvent => {
LoadingView.hide();
setHMRUnavailableReason('Disconnected from Metro.');

// https://www.rfc-editor.org/rfc/rfc6455.html#section-7.4.1
// https://www.rfc-editor.org/rfc/rfc6455.html#section-7.1.5
const isNormalOrUnsetCloseReason =
closeEvent.code === 1000 ||
closeEvent.code === 1005 ||
closeEvent.code == null;

if (isNormalOrUnsetCloseReason) {
setHMRUnavailableReason('Disconnected from Metro.');
} else {
setHMRUnavailableReason(
`Disconnected from Metro (${closeEvent.code}: "${closeEvent.reason}").`,
);
}
});

if (isEnabled) {
Expand Down
9 changes: 8 additions & 1 deletion Libraries/WebSocket/WebSocket.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,10 @@ const CLOSED = 3;

const CLOSE_NORMAL = 1000;

// Abnormal closure where no code is provided in a control frame
// https://www.rfc-editor.org/rfc/rfc6455.html#section-7.1.5
const CLOSE_ABNORMAL = 1006;

const WEBSOCKET_EVENTS = ['close', 'error', 'message', 'open'];

let nextWebSocketId = 0;
Expand Down Expand Up @@ -260,6 +264,7 @@ class WebSocket extends (EventTarget(...WEBSOCKET_EVENTS): any) {
new WebSocketEvent('close', {
code: ev.code,
reason: ev.reason,
// TODO: missing `wasClean` (exposed on iOS as `clean` but missing on Android)
}),
);
this._unregisterEvents();
Expand All @@ -277,7 +282,9 @@ class WebSocket extends (EventTarget(...WEBSOCKET_EVENTS): any) {
);
this.dispatchEvent(
new WebSocketEvent('close', {
message: ev.message,
code: CLOSE_ABNORMAL,
reason: ev.message,
// TODO: Expose `wasClean`
}),
);
this._unregisterEvents();
Expand Down

0 comments on commit 3982a2c

Please sign in to comment.