Skip to content

Commit ac7ebea

Browse files
committed
Wait for debugger to disconnect after app is done.
1 parent 2369f42 commit ac7ebea

File tree

4 files changed

+37
-2
lines changed

4 files changed

+37
-2
lines changed

src/inspector_agent.cc

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ class AsyncWriteRequest {
8484
AsyncWriteRequest(Agent* agent, const String16& message) :
8585
agent_(agent), message_(message) {}
8686
void perform() {
87-
inspector_socket_t* socket = agent_->client_socket();
87+
inspector_socket_t* socket = agent_->client_socket_;
8888
if (socket) {
8989
inspector_write(socket, message_.utf8().c_str(), message_.length());
9090
}
@@ -94,6 +94,21 @@ class AsyncWriteRequest {
9494
const String16 message_;
9595
};
9696

97+
class SetConnectedTask : public v8::Task {
98+
public:
99+
SetConnectedTask(Agent* agent, bool connected)
100+
: agent_(agent),
101+
connected_(connected) {}
102+
103+
void Run() override {
104+
agent_->connected_ = connected_;
105+
}
106+
107+
private:
108+
Agent* agent_;
109+
bool connected_;
110+
};
111+
97112
static void DisposeAsyncCb(uv_handle_t* handle) {
98113
free(handle);
99114
}
@@ -171,6 +186,7 @@ static void SendTargentsListResponse(inspector_socket_t* socket) {
171186

172187
Agent::Agent(Environment* env) : port_(5858),
173188
wait_(false),
189+
connected_(false),
174190
parent_env_(env),
175191
client_socket_(nullptr),
176192
platform_(nullptr) {
@@ -281,6 +297,10 @@ void Agent::OnRemoteData(uv_stream_t* stream, ssize_t read, const uv_buf_t* b) {
281297
agent->client_socket_ = nullptr;
282298
}
283299
DisconnectAndDispose(socket);
300+
} else {
301+
// EOF
302+
agent->platform_->CallOnForegroundThread(agent->parent_env()->isolate(),
303+
new SetConnectedTask(agent, false));
284304
}
285305
}
286306

@@ -306,6 +326,8 @@ void Agent::OnInspectorConnection(inspector_socket_t* socket) {
306326
client_socket_ = socket;
307327
inspector_read_start(socket, OnBufferAlloc, OnRemoteData);
308328
uv_sem_post(&start_sem_);
329+
platform_->CallOnForegroundThread(parent_env()->isolate(),
330+
new SetConnectedTask(this, true));
309331
}
310332

311333
bool Agent::RespondToGet(inspector_socket_t* socket, const char* path) {

src/inspector_agent.h

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,8 @@ class Agent {
5757

5858
void PostMessages();
5959

60-
inspector_socket_t* client_socket() { return client_socket_; }
60+
bool connected() { return connected_; }
61+
6162
protected:
6263
inline node::Environment* parent_env() { return parent_env_; }
6364
void InitAdaptor(Environment* env);
@@ -74,6 +75,7 @@ class Agent {
7475

7576
int port_;
7677
bool wait_;
78+
bool connected_;
7779

7880
uv_thread_t thread_;
7981
node::Environment* parent_env_;
@@ -101,7 +103,9 @@ class Agent {
101103
void OnInspectorConnection(inspector_socket_t* socket);
102104
void write(const blink::protocol::String16& message);
103105

106+
friend class AsyncWriteRequest;
104107
friend class ChannelImpl;
108+
friend class SetConnectedTask;
105109
};
106110

107111
} // namespace inspector

src/inspector_socket.cc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -319,6 +319,7 @@ static void on_close_frame_written(uv_write_t* write, int status) {
319319
static void close_frame_received(inspector_socket_t* inspector) {
320320
inspector->ws_state->received_close = true;
321321
if (!inspector->ws_state->close_sent) {
322+
invoke_read_callback(inspector, 0, 0);
322323
write_to_client(inspector, CLOSE_FRAME, sizeof(CLOSE_FRAME),
323324
on_close_frame_written);
324325
} else {

src/node.cc

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4433,6 +4433,14 @@ static void StartNodeInstance(void* arg) {
44334433
} while (more == true);
44344434
}
44354435

4436+
#if HAVE_INSPECTOR
4437+
if (env->inspector_agent()->connected())
4438+
fprintf(stderr, "Waiting for the debugger to disconnect...\n");
4439+
while (env->inspector_agent()->connected()) {
4440+
v8::platform::PumpMessageLoop(default_platform, isolate);
4441+
}
4442+
#endif
4443+
44364444
env->set_trace_sync_io(false);
44374445

44384446
int exit_code = EmitExit(env);

0 commit comments

Comments
 (0)