Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

debugger: fix stuck debugger when debuggee exits #6332

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions lib/_debug_agent.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,10 @@ exports.start = function start() {
process.removeListener('SIGWINCH', fn);
});

// Disconnect all currently connected clients.
// Without this, the debugger agent loop will not unblock
// and the debuggee will get stuck.
agent.destroyAllClients();
agent.close();
};

Expand Down Expand Up @@ -81,6 +85,12 @@ Agent.prototype.notifyWait = function notifyWait() {
this.first = false;
};

Agent.prototype.destroyAllClients = function destroyAllClients() {
this.clients.forEach(function(client) {
client.destroy();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this ends up calling this.socket.destroy

I didn't dig too much further, but is this operation synchronous? If not there might be some unexpected behavior here

/cc @bnoordhuis

Copy link
Contributor Author

@reshnm reshnm Apr 21, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I will dig into this. I used destroy, because it was already used in the close event.
Maybe this.socket.end() would be sufficient here.

Edit: It seems that this.socket.end() also works. But it eventually will also call destroy.

});
};

function Client(agent, socket) {
Transform.call(this, {
readableObjectMode: true
Expand Down
8 changes: 8 additions & 0 deletions src/debug-agent.cc
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,11 @@ void Agent::Enable() {
parent_env()->AssignToContext(debug_context);
}

static void close_handle(uv_handle_t* handle, void* data) {
if (!uv_is_closing(handle)) {
uv_close(handle, nullptr);
}
}

void Agent::Stop() {
int err;
Expand All @@ -149,6 +154,9 @@ void Agent::Stop() {
CHECK_EQ(err, 0);

uv_close(reinterpret_cast<uv_handle_t*>(&child_signal_), nullptr);
// Close all remaining handles:
// uv_loop_close will return UV_EBUSY for handles which are not closed.
uv_walk(&child_loop_, close_handle, nullptr);
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a workaround. I can't explain why there are always two pipe handles left referenced.

[--I] signal   0x30606e8
[-AI] async    0x3060530
[---] async    0x30603e8
[R--] pipe     0x7fd8a0058520
[R--] pipe     0x7fd8a0064260

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

They are the stdout and stderr pipes. Closing them like that is not very optimal, it leaves their PipeWrap instances in an invalid state.

Proper cleanup is one of the things I have to tackle for the multi-isolate work. I'll try to get around to it later this week.

uv_run(&child_loop_, UV_RUN_NOWAIT);

err = uv_loop_close(&child_loop_);
Expand Down
2 changes: 2 additions & 0 deletions test/fixtures/debugger-stuck-regression-fixture.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
'use strict';
console.log('debug');
93 changes: 93 additions & 0 deletions test/parallel/test-debugger-stuck-regression.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
'use strict';
const path = require('path');
const spawn = require('child_process').spawn;
const assert = require('assert');

const common = require('../common');

const fixture = path.join(
common.fixturesDir,
'debugger-stuck-regression-fixture.js'
);

const args = [
'debug',
`--port=${common.PORT}`,
fixture
];

const TEST_TIMEOUT_MS = common.platformTimeout(4000);

function onTestTimeout() {
common.fail('The debuggee did not terminate.');
}

const testTimeout = setTimeout(onTestTimeout, TEST_TIMEOUT_MS);

const proc = spawn(process.execPath, args, { stdio: 'pipe' });
proc.stdout.setEncoding('utf8');
proc.stderr.setEncoding('utf8');

const STATE_WAIT_BREAK = 0;
const STATE_WAIT_PROC_TERMINATED = 1;
const STATE_EXIT = 2;

let stdout = '';
let stderr = '';
let state = 0;
let cycles = 2;

proc.stdout.on('data', (data) => {
stdout += data;

switch (state) {
case STATE_WAIT_BREAK:
// check if the debugger stopped at line 1
if (stdout.includes('> 1')) {
stdout = '';

// send continue to the debugger
proc.stdin.write('c\n');
state = STATE_WAIT_PROC_TERMINATED;
}
break;
case STATE_WAIT_PROC_TERMINATED:
// check if the debuggee has terminated
if (stdout.includes('program terminated')) {
stdout = '';

// If cycles is greater than 0,
// we re-run the debuggee.
// Otherwise exit the debugger.
if (cycles > 0) {
--cycles;
state = STATE_WAIT_BREAK;
proc.stdin.write('run\n');
} else {
// cancel the test timeout
clearTimeout(testTimeout);
state = STATE_EXIT;
proc.stdin.write('.exit\n');
}

}
break;
case STATE_EXIT:
// fall through
default:
break;
}
});

proc.stderr.on('data', (data) => {
stderr += data;
});

proc.stdin.on('error', (err) => {
console.error(err);
});

process.on('exit', (code) => {
assert.equal(code, 0, 'the program should exit cleanly');
assert.equal(stderr, '', 'stderr should be empty');
});