Skip to content

Commit 0031377

Browse files
motiz88facebook-github-bot
authored andcommitted
Correctly synchronise access to WebSocketDelegate
Summary: Changelog: [Internal] Fixes a thread safety bug in the C++ platform's `InspectorPackagerConnectionDelegate::WebSocket` implementation. Since D60520747 `IWebSocketDelegate` event calls have been required to be made on the inspector thread, but the C++ platform was making them on the platform's WebSocket thread instead. Reviewed By: christophpurrer Differential Revision: D77150289 fbshipit-source-id: f57de05eaccbbe9db674076fc9e60f8d0dd243c5
1 parent 6b85c54 commit 0031377

1 file changed

Lines changed: 35 additions & 15 deletions

File tree

packages/react-native/ReactCxxPlatform/react/devsupport/inspector/InspectorPackagerConnectionDelegate.cpp

Lines changed: 35 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -21,29 +21,49 @@ InspectorPackagerConnectionDelegate::WebSocket::WebSocket(
2121
inspectorThread_(std::move(inspectorThread)) {
2222
websocket_ = webSocketClientFactory();
2323
websocket_->setOnMessageCallback(
24-
[webSocketDelegate](const std::string& message) {
25-
if (const auto strongDelegate = webSocketDelegate.lock()) {
26-
strongDelegate->didReceiveMessage(message);
24+
[webSocketDelegate,
25+
inspectorThread = inspectorThread_](const std::string& message) {
26+
auto strongInspectorThread = inspectorThread.lock();
27+
if (!strongInspectorThread) {
28+
return;
2729
}
30+
strongInspectorThread->invokeElsePost([webSocketDelegate, message]() {
31+
if (const auto strongDelegate = webSocketDelegate.lock()) {
32+
strongDelegate->didReceiveMessage(message);
33+
}
34+
});
2835
});
2936
websocket_->setOnClosedCallback(
30-
[webSocketDelegate](const std::string& /*message*/) {
31-
if (const auto strongDelegate = webSocketDelegate.lock()) {
32-
strongDelegate->didClose();
37+
[webSocketDelegate,
38+
inspectorThread = inspectorThread_](const std::string& /*message*/) {
39+
auto strongInspectorThread = inspectorThread.lock();
40+
if (!strongInspectorThread) {
41+
return;
3342
}
43+
strongInspectorThread->invokeElsePost([webSocketDelegate]() {
44+
if (const auto strongDelegate = webSocketDelegate.lock()) {
45+
strongDelegate->didClose();
46+
}
47+
});
3448
});
3549
websocket_->connect(
36-
url, [webSocketDelegate](bool success, const std::string& message) {
37-
const auto strongDelegate = webSocketDelegate.lock();
38-
if (!strongDelegate) {
50+
url,
51+
[webSocketDelegate, inspectorThread = inspectorThread_](
52+
bool success, const std::string& message) {
53+
auto strongInspectorThread = inspectorThread.lock();
54+
if (!strongInspectorThread) {
3955
return;
4056
}
41-
42-
if (success) {
43-
strongDelegate->didOpen();
44-
} else {
45-
strongDelegate->didFailWithError(std::nullopt, message);
46-
}
57+
strongInspectorThread->invokeElsePost(
58+
[webSocketDelegate, success, message]() {
59+
if (auto strongDelegate = webSocketDelegate.lock()) {
60+
if (success) {
61+
strongDelegate->didOpen();
62+
} else {
63+
strongDelegate->didFailWithError(std::nullopt, message);
64+
}
65+
}
66+
});
4767
});
4868
}
4969

0 commit comments

Comments
 (0)