Skip to content

Commit 4897f94

Browse files
author
Myles Borins
committed
node_contextify: do not incept debug context
Currently a debug context is created for various calls to util. If the node debugger is being run the main context is the debug context. In this case node_contextify was freeing the debug context and causing everything to explode. This change moves around the logic and no longer frees the context. There is a concern about the dangling pointer The regression test was adapted from code submitted by @3y3 in #4815 Fixes: #4440 Fixes: #4815 Fixes: #4597 Fixes: #4952 PR-URL: #4815 Reviewed-By: Fedor Indutny <[email protected]> Reviewed-By: Ben Noordhuis <[email protected]> Reviewed-By: Rich Trott <[email protected]>
1 parent 878bcd4 commit 4897f94

File tree

3 files changed

+83
-21
lines changed

3 files changed

+83
-21
lines changed

src/node_contextify.cc

Lines changed: 10 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -241,37 +241,26 @@ class ContextifyContext {
241241

242242

243243
static void RunInDebugContext(const FunctionCallbackInfo<Value>& args) {
244-
// Ensure that the debug context has an Environment assigned in case
245-
// a fatal error is raised. The fatal exception handler in node.cc
246-
// is not equipped to deal with contexts that don't have one and
247-
// can't easily be taught that due to a deficiency in the V8 API:
248-
// there is no way for the embedder to tell if the data index is
249-
// in use.
250-
struct ScopedEnvironment {
251-
ScopedEnvironment(Local<Context> context, Environment* env)
252-
: context_(context) {
253-
const int index = Environment::kContextEmbedderDataIndex;
254-
context->SetAlignedPointerInEmbedderData(index, env);
255-
}
256-
~ScopedEnvironment() {
257-
const int index = Environment::kContextEmbedderDataIndex;
258-
context_->SetAlignedPointerInEmbedderData(index, nullptr);
259-
}
260-
Local<Context> context_;
261-
};
262-
263244
Local<String> script_source(args[0]->ToString(args.GetIsolate()));
264245
if (script_source.IsEmpty())
265246
return; // Exception pending.
266247
Local<Context> debug_context = Debug::GetDebugContext();
248+
Environment* env = Environment::GetCurrent(args);
267249
if (debug_context.IsEmpty()) {
268250
// Force-load the debug context.
269251
Debug::GetMirror(args.GetIsolate()->GetCurrentContext(), args[0]);
270252
debug_context = Debug::GetDebugContext();
271253
CHECK(!debug_context.IsEmpty());
254+
// Ensure that the debug context has an Environment assigned in case
255+
// a fatal error is raised. The fatal exception handler in node.cc
256+
// is not equipped to deal with contexts that don't have one and
257+
// can't easily be taught that due to a deficiency in the V8 API:
258+
// there is no way for the embedder to tell if the data index is
259+
// in use.
260+
const int index = Environment::kContextEmbedderDataIndex;
261+
debug_context->SetAlignedPointerInEmbedderData(index, env);
272262
}
273-
Environment* env = Environment::GetCurrent(args);
274-
ScopedEnvironment env_scope(debug_context, env);
263+
275264
Context::Scope context_scope(debug_context);
276265
Local<Script> script = Script::Compile(script_source);
277266
if (script.IsEmpty())
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
'use strict';
2+
const util = require('util');
3+
const payload = util.inspect({a: 'b'});
4+
console.log(payload);
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
'use strict';
2+
const path = require('path');
3+
const spawn = require('child_process').spawn;
4+
const assert = require('assert');
5+
6+
const common = require('../common');
7+
8+
const fixture = path.join(
9+
common.fixturesDir,
10+
'debugger-util-regression-fixture.js'
11+
);
12+
13+
const args = [
14+
'debug',
15+
fixture
16+
];
17+
18+
const proc = spawn(process.execPath, args, { stdio: 'pipe' });
19+
proc.stdout.setEncoding('utf8');
20+
proc.stderr.setEncoding('utf8');
21+
22+
function fail() {
23+
common.fail('the program should not hang');
24+
}
25+
26+
const timer = setTimeout(fail, common.platformTimeout(4000));
27+
28+
let stdout = '';
29+
let stderr = '';
30+
31+
let nextCount = 0;
32+
33+
proc.stdout.on('data', (data) => {
34+
stdout += data;
35+
if (stdout.includes('> 1') && nextCount < 1 ||
36+
stdout.includes('> 2') && nextCount < 2 ||
37+
stdout.includes('> 3') && nextCount < 3 ||
38+
stdout.includes('> 4') && nextCount < 4) {
39+
nextCount++;
40+
proc.stdin.write('n\n');
41+
}
42+
else if (stdout.includes('{ a: \'b\' }')) {
43+
clearTimeout(timer);
44+
proc.stdin.write('.exit\n');
45+
}
46+
else if (stdout.includes('program terminated')) {
47+
// Catch edge case present in v4.x
48+
// process will terminate after call to util.inspect
49+
common.fail('the program should not terminate');
50+
}
51+
});
52+
53+
proc.stderr.on('data', (data) => stderr += data);
54+
55+
// FIXME
56+
// This test has been periodically failing on certain systems due to
57+
// uncaught errors on proc.stdin. This will stop the process from
58+
// exploding but is still not an elegant solution. Likely a deeper bug
59+
// causing this problem.
60+
proc.stdin.on('error', (err) => {
61+
console.error(err);
62+
});
63+
64+
process.on('exit', (code) => {
65+
assert.equal(code, 0, 'the program should exit cleanly');
66+
assert.equal(stdout.includes('{ a: \'b\' }'), true,
67+
'the debugger should print the result of util.inspect');
68+
assert.equal(stderr, '', 'stderr should be empty');
69+
});

0 commit comments

Comments
 (0)