From 2ab9f2e5cfd01012350bae6deccbf9defbe500a3 Mon Sep 17 00:00:00 2001 From: Eduardo Speroni Date: Fri, 27 Nov 2026 02:14:31 -0300 Subject: [PATCH] fix(inspector): don't crash when an inspector message pauses Evaluating `debugger` from the DevTools console (Runtime.evaluate) killed the app with EXC_BREAKPOINT in libdispatch ("dispatch_sync called on queue already owned by current thread"). The frontend saw Debugger.paused and then nothing: no reply to Debugger.resume, no Debugger.resumed. ScheduleMessageDrain ran its whole drain loop, dispatchMessage included, inside dispatch_sync(messageLoopQueue_). When the dispatched message paused, V8 called runMessageLoopOnPause on the same thread, and its own dispatch_sync(messageLoopQueue_) re-entered the serial queue that the drain already held. Pauses from app code (timers, breakpoints in callbacks) were not affected, because no drain is on the stack when they happen. The drain now takes messageLoopQueue_ only to read the pause loop flags, and dispatches outside it. The flag and session reset checks run before every pop rather than once. A pause inside a dispatch can end with a deferred session reset (a frontend disconnecting or reconnecting while paused), and the drain loop then carries on. It must not feed the next frontend's messages to the old session. --- NativeScript/inspector/JsV8InspectorClient.mm | 37 +++++++++++-------- 1 file changed, 22 insertions(+), 15 deletions(-) diff --git a/NativeScript/inspector/JsV8InspectorClient.mm b/NativeScript/inspector/JsV8InspectorClient.mm index 326e62b59..0a2eb325e 100644 --- a/NativeScript/inspector/JsV8InspectorClient.mm +++ b/NativeScript/inspector/JsV8InspectorClient.mm @@ -552,28 +552,35 @@ bool GetInt32(const json& value, long long& out) { void JsV8InspectorClient::ScheduleMessageDrain() { tns::ExecuteOnMainThread([this]() { - dispatch_sync(this->messageLoopQueue_, ^{ - // prevent execution if we're already pumping messages - if (runningNestedLoops_ && !terminated_) { - return; - }; + // messageLoopQueue_ must not be held across dispatchMessage: the message + // may pause, and runMessageLoopOnPause/quitMessageLoopOnPause sync on it + // from this thread. A pause inside a dispatch can end in a deferred + // session reset, so the checks run before every pop. + while (true) { + __block bool pauseLoopRunning = false; + __block bool pauseLoopPumping = false; + dispatch_sync(this->messageLoopQueue_, ^{ + pauseLoopRunning = runningNestedLoops_; + pauseLoopPumping = runningNestedLoops_ && !terminated_; + }); + if (pauseLoopPumping) { + break; + } if (this->pendingSessionReset_) { // A pause loop (possibly on another thread) is still winding down; // it schedules another drain once it has exited. - if (runningNestedLoops_) { - return; + if (pauseLoopRunning) { + break; } this->pendingSessionReset_ = false; this->SyncSessionWithFrontend(); } - std::string message; - do { - message = this->PumpMessage(); - if (!message.empty()) { - this->dispatchMessage(message); - } - } while (!message.empty()); - }); + std::string message = this->PumpMessage(); + if (message.empty()) { + break; + } + this->dispatchMessage(message); + } }); }