Skip to content

Commit 090bd9e

Browse files
rigor789edusperoni
authored andcommitted
fix(inspector): pause debug-brk apps before they run
The app was released about a second after a frontend connected, whatever it had enabled by then, and the pause relied on a require call, which an ES module app doesn't make, while schedulePauseOnNextStatement didn't stop on the main module. The app now waits for Runtime.runIfWaitingForDebugger, or 3 seconds for frontends that don't send it, then stops on a debugger statement before any app code runs.
1 parent c839e4a commit 090bd9e

2 files changed

Lines changed: 26 additions & 22 deletions

File tree

‎NativeScript/inspector/JsV8InspectorClient.h‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ class JsV8InspectorClient : V8InspectorClient, V8Inspector::Channel {
6161
void runMessageLoopOnPause(int contextGroupId) override;
6262
void quitMessageLoopOnPause() override;
6363

64-
void scheduleBreak();
64+
void pauseBeforeApp();
6565
void registerModules();
6666

6767
void consoleLog(v8::Isolate* isolate, ConsoleAPIType method,
@@ -106,7 +106,7 @@ class JsV8InspectorClient : V8InspectorClient, V8Inspector::Channel {
106106
uint64_t sessionConnectionId_ = 0;
107107
uint64_t frontendGeneration_ = 0; // guarded by senderMutex_
108108
bool isWaitingForDebugger_ = false;
109-
bool hasScheduledDebugBreak_ = false;
109+
bool debuggerAttachedWhileWaiting_ = false;
110110

111111
std::unique_ptr<tns::inspector::TracingAgentImpl> tracing_agent_;
112112

‎NativeScript/inspector/JsV8InspectorClient.mm‎

Lines changed: 24 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,9 @@ bool ShouldRewriteSourceMapURLs() {
161161
do {
162162
CFRunLoopRunInMode(kCFRunLoopDefaultMode, 0.1, false);
163163
} while (this->isWaitingForDebugger_);
164+
if (this->debuggerAttachedWhileWaiting_) {
165+
this->pauseBeforeApp();
166+
}
164167
});
165168
CFRunLoopWakeUp(CFRunLoopGetMain());
166169
});
@@ -216,14 +219,13 @@ bool ShouldRewriteSourceMapURLs() {
216219
std::function<void(const std::string&)> sender) {
217220
std::lock_guard<std::mutex> lifecycle(this->frontendLifecycleMutex_);
218221

219-
if (this->isWaitingForDebugger_) {
220-
this->isWaitingForDebugger_ = NO;
221-
CFRunLoopRef runloop = CFRunLoopGetMain();
222-
CFRunLoopPerformBlock(runloop, (__bridge CFTypeRef)(NSRunLoopCommonModes), ^{
223-
CFRunLoopRunInMode(kCFRunLoopDefaultMode, 1, false);
224-
this->scheduleBreak();
222+
if (this->isWaitingForDebugger_ && !this->debuggerAttachedWhileWaiting_) {
223+
this->debuggerAttachedWhileWaiting_ = true;
224+
// Frontends that don't send Runtime.runIfWaitingForDebugger still get the app
225+
// going, once they've had time to enable the debugger.
226+
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, NSEC_PER_SEC * 3), dispatch_get_main_queue(), ^{
227+
this->isWaitingForDebugger_ = NO;
225228
});
226-
CFRunLoopWakeUp(runloop);
227229
}
228230

229231
bool replacesFrontend;
@@ -748,6 +750,13 @@ bool ShouldRewriteSourceMapURLs() {
748750
auto json_message = json::parse(message);
749751
std::string method = json_message["method"];
750752

753+
// The frontend's last message before it expects the app to run, so it arrives
754+
// after the Debugger.enable that the pause depends on. The Runtime domain still
755+
// answers it below.
756+
if (method == "Runtime.runIfWaitingForDebugger") {
757+
this->isWaitingForDebugger_ = NO;
758+
}
759+
751760
// Note: the Tracing domain is handled in onFrontendMessageReceived, so it
752761
// never waits on the message queue or takes the Locker below.
753762

@@ -1097,26 +1106,21 @@ json WithSessionId(json message, const std::string& sessionId) {
10971106
return result;
10981107
}
10991108

1100-
void JsV8InspectorClient::scheduleBreak() {
1109+
void JsV8InspectorClient::pauseBeforeApp() {
11011110
Isolate* isolate = isolate_;
11021111
v8::Locker locker(isolate);
11031112
Isolate::Scope isolate_scope(isolate);
11041113
HandleScope handle_scope(isolate);
1105-
auto context = isolate->GetCurrentContext();
1114+
Local<Context> context = tns::Caches::Get(isolate)->GetContext();
11061115
Context::Scope context_scope(context);
11071116

1108-
if (!this->hasScheduledDebugBreak_) {
1109-
this->hasScheduledDebugBreak_ = true;
1110-
// hack: force a debugger; statement in ModuleInternal to actually break before loading the next
1111-
// (main) script...
1112-
// FIXME: find a proper fix to not need to resort to this hack.
1113-
context->Global()
1114-
->Set(context, tns::ToV8String(isolate, "__pauseOnNextRequire"),
1115-
v8::Boolean::New(isolate, true))
1116-
.ToChecked();
1117+
// schedulePauseOnNextStatement doesn't break on the evaluation of the main
1118+
// module, and an ES module has no require call to stop at either. This stops
1119+
// before any app code has run, so its breakpoints can still be set.
1120+
Local<Script> script;
1121+
if (Script::Compile(context, tns::ToV8String(isolate, "debugger;")).ToLocal(&script)) {
1122+
script->Run(context).IsEmpty();
11171123
}
1118-
1119-
this->session_->schedulePauseOnNextStatement({}, {});
11201124
}
11211125

11221126
void JsV8InspectorClient::registerModules() {

0 commit comments

Comments
 (0)