Skip to content

Commit b5226a1

Browse files
fix(worker): let a heap-limit terminate or nested console call re-enter the inspector lock (#2070)
WorkerWrapper::ConsoleLog holds inspectorMutex_ across WorkerInspectorClient::consoleLog, which allocates and runs user JS while it stringifies the arguments. A near-heap-limit callback raised there calls Terminate(), and a getter that logs re-enters ConsoleLog; both take the mutex again on the same thread and deadlocked the worker. inspectorMutex_ is now a std::recursive_mutex. It still spans the call, since a worker callback run through __runOnMainThread logs from the main thread while the worker thread can delete the client. Adds a spec for a console call nested in argument formatting.
1 parent 602e8ab commit b5226a1

7 files changed

Lines changed: 48 additions & 12 deletions

File tree

‎test-app/app/src/main/assets/app/mainpage.js‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ require("./tests/testWorkerTerminateDuringLoad");
3535
require("./tests/testWorkerTerminateAfterClose");
3636
require("./tests/testWorkerOptions");
3737
require("./tests/testWorkerResourceLimits");
38+
require("./tests/testWorkerNestedConsole");
3839
require("./tests/testMessaging");
3940
require("./tests/testInterfaceDefaultMethods");
4041
require("./tests/testInterfaceStaticMethods");
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
describe("Worker nested console call", function () {
2+
it("should keep running when formatting a logged argument logs again", function (done) {
3+
var worker = new Worker("./workerNestedConsoleWorker.js");
4+
// A worker stuck in ConsoleLog holds the inspector lock that
5+
// terminate() takes, so only the success path may terminate it.
6+
worker.onmessage = function (e) {
7+
expect(e.data).toBe("alive");
8+
worker.terminate();
9+
done();
10+
};
11+
worker.onerror = function (e) {
12+
done.fail(e.message);
13+
};
14+
});
15+
});
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
// The inspector reads Symbol.toStringTag while it formats a console argument,
2+
// so this getter runs inside the outer console.log.
3+
var arg = {};
4+
Object.defineProperty(arg, Symbol.toStringTag, {
5+
get: function () {
6+
console.log("nested");
7+
return "Nested";
8+
}
9+
});
10+
console.log(arg);
11+
postMessage("alive");

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1134,9 +1134,9 @@ void JsV8InspectorClient::sendToFrontEndCallback(const v8::FunctionCallbackInfo<
11341134
}
11351135

11361136
void JsV8InspectorClient::consoleLogCallback(Isolate* isolate, ConsoleAPIType method, const std::vector<v8::Local<v8::Value>>& args) {
1137-
// Worker isolates route to their own inspector (we're on the worker's
1138-
// thread here). Checked first so worker logging never lazily constructs
1139-
// the root client.
1137+
// Worker isolates route to their own inspector (from the worker thread,
1138+
// or the main thread under __runOnMainThread). Checked first so worker
1139+
// logging never lazily constructs the root client.
11401140
WorkerWrapper* worker = WorkerWrapper::FromIsolate(isolate);
11411141
if (worker != nullptr) {
11421142
worker->ConsoleLog(method, args);

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -74,8 +74,9 @@ class WorkerInspectorClient final : public v8_inspector::V8InspectorClient,
7474
return runningPauseLoop_.load(std::memory_order_acquire);
7575
}
7676

77-
// Worker thread. Mirrors JsV8InspectorClient::consoleLogCallback for this
78-
// isolate.
77+
// Thread holding the worker isolate's Locker (the worker thread, or the
78+
// main thread under __runOnMainThread). Mirrors
79+
// JsV8InspectorClient::consoleLogCallback for this isolate.
7980
void consoleLog(v8_inspector::ConsoleAPIType method,
8081
const std::vector<v8::Local<v8::Value>>& args);
8182

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -265,7 +265,7 @@ void WorkerWrapper::Terminate() {
265265
// A worker paused at a breakpoint sits in the inspector's nested pause
266266
// loop, not in Looper.loop() - kick it loose so TerminateExecution and
267267
// the looper quit below can take effect.
268-
std::lock_guard<std::mutex> lock(inspectorMutex_);
268+
std::lock_guard<std::recursive_mutex> lock(inspectorMutex_);
269269
if (inspector_ != nullptr) {
270270
inspector_->NotifyTerminating();
271271
}
@@ -940,7 +940,7 @@ void WorkerWrapper::CreateInspector(Isolate* isolate) {
940940

941941
auto* client = new WorkerInspectorClient(workerId_, isolate, ALooper_forThread(), url);
942942
{
943-
std::lock_guard<std::mutex> lock(inspectorMutex_);
943+
std::lock_guard<std::recursive_mutex> lock(inspectorMutex_);
944944
inspector_ = client;
945945
}
946946

@@ -952,7 +952,7 @@ void WorkerWrapper::CreateInspector(Isolate* isolate) {
952952
void WorkerWrapper::DestroyInspector() {
953953
WorkerInspectorClient* client = nullptr;
954954
{
955-
std::lock_guard<std::mutex> lock(inspectorMutex_);
955+
std::lock_guard<std::recursive_mutex> lock(inspectorMutex_);
956956
client = inspector_;
957957
inspector_ = nullptr;
958958
}
@@ -973,7 +973,7 @@ void WorkerWrapper::DestroyInspector() {
973973

974974
void WorkerWrapper::ConsoleLog(v8_inspector::ConsoleAPIType method,
975975
const std::vector<Local<Value>>& args) {
976-
std::lock_guard<std::mutex> lock(inspectorMutex_);
976+
std::lock_guard<std::recursive_mutex> lock(inspectorMutex_);
977977
if (inspector_ != nullptr) {
978978
inspector_->consoleLog(method, args);
979979
}

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

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -166,8 +166,10 @@ class WorkerWrapper : public std::enable_shared_from_this<WorkerWrapper> {
166166

167167
#ifdef APPLICATION_IN_DEBUG
168168
/*
169-
* Worker thread (console.* fires on the isolate's own thread). Forwards
170-
* to the worker's inspector so the log reaches DevTools' worker target.
169+
* Any thread holding the worker isolate's Locker: the worker thread, or
170+
* the main thread while it runs a worker callback through
171+
* __runOnMainThread. Forwards to the worker's inspector so the log
172+
* reaches DevTools' worker target.
171173
*/
172174
void ConsoleLog(v8_inspector::ConsoleAPIType method,
173175
const std::vector<v8::Local<v8::Value>>& args);
@@ -257,7 +259,13 @@ class WorkerWrapper : public std::enable_shared_from_this<WorkerWrapper> {
257259
void DestroyInspector();
258260

259261
WorkerInspectorClient* inspector_ = nullptr;
260-
std::mutex inspectorMutex_;
262+
// Held across consoleLog, which allocates and runs user JS while it
263+
// stringifies the arguments; releasing it there would let the worker
264+
// thread delete the client under a __runOnMainThread caller. Recursive
265+
// because that call re-enters on the same thread: a nested console call
266+
// from a getter, or a near-heap-limit callback raised by an allocation,
267+
// which calls Terminate().
268+
std::recursive_mutex inspectorMutex_;
261269
#endif
262270

263271
static std::mutex registryMutex_;

0 commit comments

Comments
 (0)