Skip to content

Commit 99391e8

Browse files
committed
fix(inspector): tie Network.setCacheDisabled to the websocket that sent it
A frontend's cache disable was a single flag on the client, released by disconnect(), which onClose posts to the main thread. While the main isolate was paused or busy, the cache stayed off with no frontend connected, and a stale disconnect() from an old socket cleared the flag of the frontend that had replaced it. The socket thread now passes its websocket along with each message, and the client keeps one holder per websocket that disabled the cache. onClose and onException release that socket's holder on the socket thread through socketClosed(), which takes only connectionMutex_.
1 parent bba70e4 commit 99391e8

4 files changed

Lines changed: 59 additions & 24 deletions

File tree

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

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,9 @@ class AndroidJsV8Inspector {
3737

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

40-
private native String handleMessageOnSocketThread(String message);
40+
private native String handleMessageOnSocketThread(String message, Object socket);
41+
42+
private static native void socketClosed(Object socket);
4143

4244
private Handler mainHandler;
4345

@@ -279,6 +281,7 @@ protected void onClose(NanoWSD.WebSocketFrame.CloseCode code, String reason, boo
279281
Log.d("V8Inspector", "onClose");
280282
}
281283

284+
socketClosed(JsV8InspectorWebSocket.this);
282285
mainHandler.post(new Runnable() {
283286
@Override
284287
public void run() {
@@ -303,7 +306,7 @@ protected void onMessage(final NanoWSD.WebSocketFrame message) {
303306
// too. Debugger.pause schedules a V8 interrupt and still flows
304307
// through the queue. A null return means "not handled" (queue it);
305308
// an empty string means handled with nothing left to send.
306-
String fastPathResponse = handleMessageOnSocketThread(message.getTextPayload());
309+
String fastPathResponse = handleMessageOnSocketThread(message.getTextPayload(), JsV8InspectorWebSocket.this);
307310
if (fastPathResponse != null) {
308311
if (!fastPathResponse.isEmpty()) {
309312
try {
@@ -378,6 +381,7 @@ protected void onException(IOException exception) {
378381
exception.printStackTrace();
379382
}
380383
}
384+
socketClosed(JsV8InspectorWebSocket.this);
381385
disconnect();
382386
}
383387
}

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

Lines changed: 24 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -394,11 +394,6 @@ void JsV8InspectorClient::disconnect() {
394394

395395
{
396396
std::lock_guard<std::mutex> lock(connectionMutex_);
397-
// A client's Network.setCacheDisabled ends with its connection.
398-
if (frontendCacheDisabled_) {
399-
frontendCacheDisabled_ = false;
400-
AddModuleHttpCacheInspectorDisable(-1);
401-
}
402397
if (connection_ == nullptr) {
403398
return;
404399
}
@@ -554,7 +549,8 @@ void JsV8InspectorClient::doDispatchMessage(const std::string& message) {
554549
// method handling never runs, and a worker must stay debuggable while the
555550
// main isolate is paused (and vice versa). None of this touches V8 except
556551
// the thread-safe RequestInterrupt.
557-
bool JsV8InspectorClient::handleMessageOnSocketThread(const std::string& message, std::string& response) {
552+
bool JsV8InspectorClient::handleMessageOnSocketThread(const std::string& message, jobject socket,
553+
std::string& response) {
558554
auto parsed = json::parse(message, nullptr, false);
559555
if (parsed.is_discarded() || !parsed.is_object()) {
560556
return false;
@@ -729,7 +725,7 @@ bool JsV8InspectorClient::handleMessageOnSocketThread(const std::string& message
729725
if (method == "Network.clearBrowserCache") {
730726
ClearModuleHttpCacheFromInspector();
731727
} else if (std::optional<bool> cacheDisabled = SetCacheDisabledParam(parsed)) {
732-
SetFrontendCacheDisabled(*cacheDisabled);
728+
SetCacheDisabledBy(socket, *cacheDisabled);
733729
}
734730
if (sessionId.empty()) {
735731
return false;
@@ -778,13 +774,30 @@ void JsV8InspectorClient::ClearModuleHttpCacheFromInspector() {
778774
}
779775
}
780776

781-
void JsV8InspectorClient::SetFrontendCacheDisabled(bool cacheDisabled) {
777+
void JsV8InspectorClient::SetCacheDisabledBy(jobject socket, bool cacheDisabled) {
778+
if (socket == nullptr) {
779+
return;
780+
}
781+
JEnv env;
782782
std::lock_guard<std::mutex> lock(connectionMutex_);
783-
if (connection_ == nullptr || cacheDisabled == frontendCacheDisabled_) {
783+
auto holder = std::find_if(cacheDisabledBy_.begin(), cacheDisabledBy_.end(),
784+
[&](jobject held) { return env.isSameObject(held, socket); });
785+
const bool holds = holder != cacheDisabledBy_.end();
786+
if (cacheDisabled == holds) {
784787
return;
785788
}
786-
frontendCacheDisabled_ = cacheDisabled;
787-
AddModuleHttpCacheInspectorDisable(cacheDisabled ? 1 : -1);
789+
if (cacheDisabled) {
790+
cacheDisabledBy_.push_back(env.NewGlobalRef(socket));
791+
AddModuleHttpCacheInspectorDisable(1);
792+
} else {
793+
env.DeleteGlobalRef(*holder);
794+
cacheDisabledBy_.erase(holder);
795+
AddModuleHttpCacheInspectorDisable(-1);
796+
}
797+
}
798+
799+
void JsV8InspectorClient::SocketClosed(jobject socket) {
800+
SetCacheDisabledBy(socket, false);
788801
}
789802

790803
void JsV8InspectorClient::RouteToWorker(const std::string& sessionId, const std::string& method,

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

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -48,11 +48,18 @@ class JsV8InspectorClient : V8InspectorClient, v8_inspector::V8Inspector::Channe
4848
void UnregisterWorkerTarget(int workerId);
4949
void SchedulePauseInWorker(int workerId);
5050

51-
// Runs on the websocket read thread. Returns true when the message was
52-
// handled there (response, possibly empty when replies were already
53-
// sent or none is needed, goes back to the socket); false routes the
54-
// message through the normal main-thread dispatch queue.
55-
bool handleMessageOnSocketThread(const std::string& message, std::string& response);
51+
// Runs on the websocket read thread of `socket`, the websocket the
52+
// message arrived on. Returns true when the message was handled there
53+
// (response, possibly empty when replies were already sent or none is
54+
// needed, goes back to the socket); false routes the message through
55+
// the normal main-thread dispatch queue.
56+
bool handleMessageOnSocketThread(const std::string& message, jobject socket,
57+
std::string& response);
58+
59+
// On `socket`'s read thread, once it closed or failed: ends what that
60+
// frontend held. Takes only connectionMutex_, so it never waits for a
61+
// paused or busy isolate.
62+
void SocketClosed(jobject socket);
5663

5764
void registerModules();
5865

@@ -125,7 +132,7 @@ class JsV8InspectorClient : V8InspectorClient, v8_inspector::V8Inspector::Channe
125132
// Network.setCacheDisabled on the HTTP module cache (HttpLoader.h).
126133
// Socket thread.
127134
void ClearModuleHttpCacheFromInspector();
128-
void SetFrontendCacheDisabled(bool cacheDisabled);
135+
void SetCacheDisabledBy(jobject socket, bool cacheDisabled);
129136
void AnnounceWorkerTargets();
130137

131138
std::map<std::string, WorkerTarget> workerTargets_;
@@ -165,9 +172,12 @@ class JsV8InspectorClient : V8InspectorClient, v8_inspector::V8Inspector::Channe
165172
// memory location with the bitfields above, which the main thread
166173
// writes.
167174
std::atomic<bool> isConnected_;
168-
// The connected frontend has the HTTP module cache disabled, holding one
169-
// AddModuleHttpCacheInspectorDisable count. Guarded by connectionMutex_.
170-
bool frontendCacheDisabled_ = false;
175+
// Global refs to the websockets whose frontend has the HTTP module cache
176+
// disabled, each holding one AddModuleHttpCacheInspectorDisable count
177+
// until it re-enables it or its socket closes. A replaced frontend's
178+
// socket can outlive connection_, so this is not tied to it. Guarded by
179+
// connectionMutex_.
180+
std::vector<jobject> cacheDisabledBy_;
171181

172182

173183
// {N} specific helpers

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

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,16 +24,24 @@ JNIEXPORT extern "C" void Java_com_tns_AndroidJsV8Inspector_disconnect(JNIEnv* e
2424
JsV8InspectorClient::GetInstance()->disconnect();
2525
}
2626

27+
JNIEXPORT extern "C" void Java_com_tns_AndroidJsV8Inspector_socketClosed(JNIEnv* env, jclass clazz, jobject socket) {
28+
try {
29+
JsV8InspectorClient::GetInstance()->SocketClosed(socket);
30+
} catch (...) {
31+
// must never propagate a native exception into the websocket thread
32+
}
33+
}
34+
2735
JNIEXPORT extern "C" void Java_com_tns_AndroidJsV8Inspector_dispatchMessage(JNIEnv* env, jobject instance, jstring jMessage) {
2836
std::string message = ArgConverter::jstringToString(jMessage);
2937
JsV8InspectorClient::GetInstance()->dispatchMessage(message);
3038
}
3139

32-
JNIEXPORT extern "C" jstring Java_com_tns_AndroidJsV8Inspector_handleMessageOnSocketThread(JNIEnv* env, jobject instance, jstring jMessage) {
40+
JNIEXPORT extern "C" jstring Java_com_tns_AndroidJsV8Inspector_handleMessageOnSocketThread(JNIEnv* env, jobject instance, jstring jMessage, jobject socket) {
3341
try {
3442
std::string message = ArgConverter::jstringToString(jMessage);
3543
std::string response;
36-
if (JsV8InspectorClient::GetInstance()->handleMessageOnSocketThread(message, response)) {
44+
if (JsV8InspectorClient::GetInstance()->handleMessageOnSocketThread(message, socket, response)) {
3745
// Handled; an empty string means any replies were already sent.
3846
return env->NewStringUTF(response.c_str());
3947
}

0 commit comments

Comments
 (0)