Skip to content

Commit fce224c

Browse files
committed
fix(inspector): skip a closed socket's disconnect once a newer frontend connected
onClose posts disconnect() to the main thread. When a new frontend connected before it ran, that late disconnect() nulled the new connection and reset its V8 session, worker sessions, tracing and resource streams. disconnect() now takes the closing websocket and returns early when a different websocket is connected, checked again where the connection is dropped. A connect that lands while a stale disconnect() is already past the first check can still lose the stream, tracing and worker resets done before the second.
1 parent 99391e8 commit fce224c

4 files changed

Lines changed: 24 additions & 9 deletions

File tree

‎test-app/app/src/main/java/com/tns/AndroidJsV8Inspector.java‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ class AndroidJsV8Inspector {
3333

3434
private native void scheduleBreak();
3535

36-
protected static native void disconnect();
36+
protected static native void disconnect(Object socket);
3737

3838
protected native final void dispatchMessage(String message);
3939

@@ -288,7 +288,7 @@ public void run() {
288288
if (currentRuntimeLogger.isEnabled()) {
289289
Log.d("V8Inspector", "Disconnecting");
290290
}
291-
disconnect();
291+
disconnect(JsV8InspectorWebSocket.this);
292292
}
293293
});
294294
}
@@ -382,7 +382,7 @@ protected void onException(IOException exception) {
382382
}
383383
}
384384
socketClosed(JsV8InspectorWebSocket.this);
385-
disconnect();
385+
disconnect(JsV8InspectorWebSocket.this);
386386
}
387387
}
388388
}

‎test-app/runtime/src/main/cpp/JsV8InspectorClient.cpp‎

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -367,7 +367,21 @@ void JsV8InspectorClient::createInspectorSession() {
367367
v8_inspector::V8Inspector::kFullyTrusted);
368368
}
369369

370-
void JsV8InspectorClient::disconnect() {
370+
void JsV8InspectorClient::disconnect(jobject closingSocket) {
371+
JEnv env;
372+
// A socket's close is posted to the main thread, so it can run after a
373+
// newer frontend connected; that connect already tore the old one down.
374+
auto replaced = [&]() {
375+
return closingSocket != nullptr && connection_ != nullptr &&
376+
!env.isSameObject(connection_, closingSocket);
377+
};
378+
{
379+
std::lock_guard<std::mutex> lock(connectionMutex_);
380+
if (replaced()) {
381+
return;
382+
}
383+
}
384+
371385
{
372386
// Streams only have meaning to the frontend that opened them; drop any
373387
// the old connection never read to the end (or never IO.close-d).
@@ -394,11 +408,10 @@ void JsV8InspectorClient::disconnect() {
394408

395409
{
396410
std::lock_guard<std::mutex> lock(connectionMutex_);
397-
if (connection_ == nullptr) {
411+
if (connection_ == nullptr || replaced()) {
398412
return;
399413
}
400414

401-
JEnv env;
402415
env.DeleteGlobalRef(connection_);
403416
connection_ = nullptr;
404417
isConnected_ = false;

‎test-app/runtime/src/main/cpp/JsV8InspectorClient.h‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,9 @@ class JsV8InspectorClient : V8InspectorClient, v8_inspector::V8Inspector::Channe
2929
void init();
3030
void connect(jobject connection);
3131
void scheduleBreak();
32-
void disconnect();
32+
// `closingSocket` null tears down whatever frontend is connected;
33+
// otherwise only that websocket's, if it is still the connected one.
34+
void disconnect(jobject closingSocket = nullptr);
3335
void dispatchMessage(const std::string& message);
3436

3537
// Any thread. Rewrites source map urls and writes to the frontend

‎test-app/runtime/src/main/cpp/com_tns_AndroidJsV8Inspector.cpp‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,8 @@ JNIEXPORT extern "C" void Java_com_tns_AndroidJsV8Inspector_scheduleBreak(JNIEnv
2020
JsV8InspectorClient::GetInstance()->scheduleBreak();
2121
}
2222

23-
JNIEXPORT extern "C" void Java_com_tns_AndroidJsV8Inspector_disconnect(JNIEnv* env, jobject instance) {
24-
JsV8InspectorClient::GetInstance()->disconnect();
23+
JNIEXPORT extern "C" void Java_com_tns_AndroidJsV8Inspector_disconnect(JNIEnv* env, jclass clazz, jobject socket) {
24+
JsV8InspectorClient::GetInstance()->disconnect(socket);
2525
}
2626

2727
JNIEXPORT extern "C" void Java_com_tns_AndroidJsV8Inspector_socketClosed(JNIEnv* env, jclass clazz, jobject socket) {

0 commit comments

Comments
 (0)