Remove WebSocket delegate before closing

Summary:
This change removes ourselves as a delegate before closing.

SocketRocket uses its own internal async queue to perform most operations.

After a disconnect, we don't expect to receive any more delegate calls as the handlers may contain references which may have become invalid.

So, removing ourselves as delegates will ensure that we don't get called after a disconnect.

For sanity, we are also taking a copy of the message handler instead of a reference to it.

Reviewed By: briantkelley

Differential Revision: D31360721

fbshipit-source-id: bae5a2423757cd9064ffac28afb8b78c28a20d87
This commit is contained in:
Lorenzo Blasa
2021-10-03 01:16:40 -07:00
committed by Facebook GitHub Bot
parent 20185f37ab
commit 799d88275e
2 changed files with 5 additions and 2 deletions

View File

@@ -156,7 +156,9 @@ static constexpr int connectionKeepaliveSeconds = 10;
// not notify the delegate. SocketRocket only triggers the close event // not notify the delegate. SocketRocket only triggers the close event
// when the connection is closed from the server. // when the connection is closed from the server.
_eventHandler(facebook::flipper::SocketEvent::CLOSE); _eventHandler(facebook::flipper::SocketEvent::CLOSE);
// Clear the socket delegate before close. Ensures that we won't get
// any messages after the disconnect takes place.
_socket.delegate = nil;
[_socket close]; [_socket close];
_socket = nil; _socket = nil;
} }

View File

@@ -170,8 +170,9 @@ bool FlipperWebSocket::connect(FlipperConnectionManager* manager) {
} }
eventHandler(event); eventHandler(event);
}; };
auto messageHandler = messageHandler_;
socket_.messageHandler = ^(const std::string& message) { socket_.messageHandler = ^(const std::string& message) {
this->messageHandler_(message); messageHandler(message);
}; };
if (endpoint_.secure) { if (endpoint_.secure) {