Skip to content

Commit

Permalink
Fix WebSocketModule not closing connections on reload
Browse files Browse the repository at this point in the history
Summary:
Saw in the logs an ever increasing number of warnings coming from WebSocketModule about requesting an instance that has already gone away.

On module invalidation we should close all outstanding websockets, as they will no longer be able to send events to JS.

Changelog: [Android][Fixed] On instance destroy, websockets are correctly closed

Reviewed By: mdvacca

Differential Revision: D40897255

fbshipit-source-id: 1578de8baa342479d14ee1070c3314d45c7fbd8d
  • Loading branch information
javache authored and facebook-github-bot committed Nov 2, 2022
1 parent 6a23b13 commit b5ea5a2
Showing 1 changed file with 11 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -58,10 +58,18 @@ public WebSocketModule(ReactApplicationContext context) {
mCookieHandler = new ForwardingCookieHandler(context);
}

private void sendEvent(String eventName, WritableMap params) {
ReactApplicationContext reactApplicationContext = getReactApplicationContextIfActiveOrWarn();
@Override
public void invalidate() {
for (WebSocket socket : mWebSocketConnections.values()) {
socket.close(1001 /* endpoint is going away */, null);
}
mWebSocketConnections.clear();
mContentHandlers.clear();
}

if (reactApplicationContext != null) {
private void sendEvent(String eventName, WritableMap params) {
ReactApplicationContext reactApplicationContext = getReactApplicationContext();
if (reactApplicationContext.hasActiveReactInstance()) {
reactApplicationContext
.getJSModule(DeviceEventManagerModule.RCTDeviceEventEmitter.class)
.emit(eventName, params);
Expand Down

0 comments on commit b5ea5a2

Please sign in to comment.