Repository navigation
fix(inspector): don't crash when an inspector message pauses - #612
edusperoni wants to merge 1 commit into
Conversation
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.
|
No actionable comments were generated in the recent review. Walkthrough
Merge Risk:
|
Typing
debuggerin the DevTools Console (Runtime.evaluatewith an expression that pauses) killed the app. The frontend sawDebugger.paused, then nothing:Debugger.resumegot no reply andDebugger.resumednever came. It looked like a hang, but the app was gone. The crash isEXC_BREAKPOINT(SIGTRAP) in libdispatch, "dispatch_sync called on queue already owned by current thread":The same code is in 9.0.3, where the drain lived in
onFrontendMessageReceived.Root cause
ScheduleMessageDrainran the whole drain loop insidedispatch_sync(messageLoopQueue_), includingdispatchMessage. When the dispatched message pauses, V8 callsrunMessageLoopOnPauseon the same thread, and its owndispatch_sync(messageLoopQueue_)re-enters the serial queue the drain already holds. libdispatch traps on that.quitMessageLoopOnPausesyncs on the same queue, so it would have hit the same trap.Pauses from app code (a timer, a breakpoint in a callback) never ran into this, because no drain is on the stack when they happen. The nested pause loop could already drain the queue on its own, and the message threading was never the problem. Only the lock scope was wrong, so no work moves to another thread.
Fix
The drain takes
messageLoopQueue_only to readrunningNestedLoops_/terminated_, and callsdispatchMessageoutside it. A message that pauses now runs the pause loop inside the drain's stack, the same way a worker'sDrainIncomingalready does.The flag and
pendingSessionReset_checks now run before every pop instead of once per drain. After a pause inside a dispatch unwinds, the drain loop carries on. If the pause ended throughDeferSessionResetFromPauseLoop(a frontend disconnecting, or a new one taking over, while paused), the swap has to happen before the loop pops the next frontend's messages. Otherwise those messages reach the old session, whose replies go to the departed connection.Testing
All on an iPhone 16 Pro simulator (iOS 26.3), with a simulator-only runtime build from this branch (
BUILD_CATALYST=false BUILD_IPHONE=false BUILD_VISION=false ./build_all_ios.sh). That build is installed as afile:tgz into a TypeScript template app. For every scenario,dwarfdump --uuidof the app's embeddedNativeScript.frameworkmatched thedist/simulator slice. Each run also checked that the app pid was still alive and that no new crash report had appeared. The CDP client is a small Node script (not committed) speaking the inspector socket's length-prefixed UTF-16LE framing.Repro (
Runtime.enable,Debugger.enable, thenRuntime.evaluate {expression: "debugger; 1"}as id 3, thenDebugger.resumeas id 4).Before (unmodified build of
main), the app crashes:After:
Each scenario below ran with the pause coming both from the Console (
Runtime.evaluatewithdebugger) and from app code (setTimeout(() => { debugger; ... })):{}reply,Debugger.resumed, code after the pause runs / evaluate result returnedDebugger.stepOvertwice: each step is answered and pauses at a new location; result is correct after resumeNetwork.loadNetworkResource,IO.read,IO.closewhile pausedDebugger.evaluateOnCallFrameandRuntime.evaluatewhile pausedscriptParsedreplay, sees the app resumed, and can pause and resume againAll 12 combinations passed. Then console stepping, console disconnect, and takeover with each pause origin ran 10 more times each: 40/40 passed, with the app alive and no crash reports. Before the fix, only the Console column failed (the app crashed at the first pause). The app-code column passed on the unmodified build too.
One more check: a
Runtime.evaluatethat spins the main run loop (NSRunLoop.currentRunLoop.runUntilDate(...)) while another message arrives. Both replies come back in order on both builds. The main queue doesn't run a second drain block while one is on the stack, so the drain can't nest that way../run_tests.sh --temp-sim: 2314 specs, 0 failures, 13 skipped. The suite has no inspector coverage, so it guards only against regressions elsewhere.Summary by CodeRabbit