Skip to content

Commit 3098976

Browse files
authored
fix(inspector): ensure socket message is copied and stored (#155)
1 parent f46c425 commit 3098976

File tree

3 files changed

+18
-4
lines changed

3 files changed

+18
-4
lines changed

NativeScript/inspector/InspectorServer.mm

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,10 @@
7878
if (size) {
7979
NSString* payload = [[NSString alloc] initWithData:(NSData*)data encoding:NSUTF16LittleEndianStringEncoding];
8080

81-
onMessage([payload UTF8String]);
81+
if (payload) {
82+
std::string result = [payload UTF8String];
83+
onMessage(result);
84+
}
8285

8386
#pragma clang diagnostic push
8487
#pragma clang diagnostic ignored "-Warc-retain-cycles"

NativeScript/inspector/JsV8InspectorClient.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,13 +44,14 @@ class JsV8InspectorClient : V8InspectorClient, V8Inspector::Channel {
4444
std::vector<std::string> messages_;
4545
bool runningNestedLoops_;
4646
dispatch_queue_t messagesQueue_;
47+
dispatch_semaphore_t messageArrived_;
4748
std::function<void (std::string)> sender_;
4849
bool isWaitingForDebugger_;
4950

5051
void enableInspector(int argc, char** argv);
5152
void notify(std::unique_ptr<StringBuffer> message);
5253
void onFrontendConnected(std::function<void (std::string)> sender);
53-
void onFrontendMessageReceived(std::string message);
54+
void onFrontendMessageReceived(std::string &message);
5455
template <class TypeName>
5556
static v8::Local<TypeName> PersistentToLocal(v8::Isolate* isolate, const v8::Persistent<TypeName>& persistent);
5657
std::string PumpMessage();

NativeScript/inspector/JsV8InspectorClient.mm

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,7 @@
8383
messages_(),
8484
runningNestedLoops_(false) {
8585
this->messagesQueue_ = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
86+
this->messageArrived_ = dispatch_semaphore_create(0);
8687
}
8788

8889
void JsV8InspectorClient::onFrontendConnected(std::function<void (std::string)> sender) {
@@ -100,9 +101,11 @@
100101
this->disconnect();
101102
}
102103

103-
void JsV8InspectorClient::onFrontendMessageReceived(std::string message) {
104+
void JsV8InspectorClient::onFrontendMessageReceived(std::string &message) {
105+
__block std::string strCopy = message;
104106
dispatch_sync(this->messagesQueue_, ^{
105-
this->messages_.push_back(message);
107+
this->messages_.push_back(strCopy);
108+
dispatch_semaphore_signal(messageArrived_);
106109
});
107110

108111
tns::ExecuteOnMainThread([this, message]() {
@@ -166,15 +169,22 @@
166169

167170
terminated_ = false;
168171
this->runningNestedLoops_ = true;
172+
bool shouldWait = false;
169173
while (!terminated_) {
170174
std::string message = this->PumpMessage();
171175
if (!message.empty()) {
172176
this->dispatchMessage(message);
177+
shouldWait = false;
178+
} else {
179+
shouldWait = true;
173180
}
174181

175182
std::shared_ptr<Platform> platform = tns::Runtime::GetPlatform();
176183
Isolate* isolate = runtime_->GetIsolate();
177184
platform::PumpMessageLoop(platform.get(), isolate, platform::MessageLoopBehavior::kDoNotWait);
185+
if(shouldWait && !terminated_) {
186+
dispatch_semaphore_wait(messageArrived_, dispatch_time(DISPATCH_TIME_NOW, 1000000)); // 1ms in ns
187+
}
178188
}
179189

180190
terminated_ = false;

0 commit comments

Comments
 (0)