mirror of https://github.com/lukechilds/node.git
Browse Source
Use the --trace-sync-io flag to print a stack trace whenever a sync method is used after the first tick, excluding during the process exit event. (e.g. fs.readFileSync()) It does not track if the warning has occurred at a specific location in the past and so will print the warning every time. Reason for not printing during the first tick of the appication is so all necessary resources can be required. Also by excluding synchronous calls during exit is necessary in case any data needs to be logged out by the application before it shuts down. Fixes: https://github.com/nodejs/io.js/issues/1674 PR-URL: https://github.com/nodejs/io.js/pull/1707 Reviewed-By: Ben Noordhuis <info@bnoordhuis.nl> Reviewed-By: Fedor Indutny <fedor@indutny.com> Reviewed-By: Jeremiah Senkpiel <fishrock123@rocketmail.com> Reviewed-By: Petka Antonov <petka_antonov@hotmail.com>v2.3.1-release
10 changed files with 137 additions and 1 deletions
@ -0,0 +1,58 @@ |
|||||
|
#include "env.h" |
||||
|
#include "env-inl.h" |
||||
|
#include "v8.h" |
||||
|
#include <stdio.h> |
||||
|
|
||||
|
namespace node { |
||||
|
|
||||
|
using v8::HandleScope; |
||||
|
using v8::Local; |
||||
|
using v8::Message; |
||||
|
using v8::StackFrame; |
||||
|
using v8::StackTrace; |
||||
|
|
||||
|
void Environment::PrintSyncTrace() const { |
||||
|
if (!trace_sync_io_) |
||||
|
return; |
||||
|
|
||||
|
HandleScope handle_scope(isolate()); |
||||
|
Local<v8::StackTrace> stack = |
||||
|
StackTrace::CurrentStackTrace(isolate(), 10, StackTrace::kDetailed); |
||||
|
|
||||
|
fprintf(stderr, "WARNING: Detected use of sync API\n"); |
||||
|
|
||||
|
for (int i = 0; i < stack->GetFrameCount() - 1; i++) { |
||||
|
Local<StackFrame> stack_frame = stack->GetFrame(i); |
||||
|
node::Utf8Value fn_name_s(isolate(), stack_frame->GetFunctionName()); |
||||
|
node::Utf8Value script_name(isolate(), stack_frame->GetScriptName()); |
||||
|
const int line_number = stack_frame->GetLineNumber(); |
||||
|
const int column = stack_frame->GetColumn(); |
||||
|
|
||||
|
if (stack_frame->IsEval()) { |
||||
|
if (stack_frame->GetScriptId() == Message::kNoScriptIdInfo) { |
||||
|
fprintf(stderr, " at [eval]:%i:%i\n", line_number, column); |
||||
|
} else { |
||||
|
fprintf(stderr, |
||||
|
" at [eval] (%s:%i:%i)\n", |
||||
|
*script_name, |
||||
|
line_number, |
||||
|
column); |
||||
|
} |
||||
|
break; |
||||
|
} |
||||
|
|
||||
|
if (fn_name_s.length() == 0) { |
||||
|
fprintf(stderr, " at %s:%i:%i\n", *script_name, line_number, column); |
||||
|
} else { |
||||
|
fprintf(stderr, |
||||
|
" at %s (%s:%i:%i)\n", |
||||
|
*fn_name_s, |
||||
|
*script_name, |
||||
|
line_number, |
||||
|
column); |
||||
|
} |
||||
|
} |
||||
|
fflush(stderr); |
||||
|
} |
||||
|
|
||||
|
} // namespace node
|
@ -0,0 +1,44 @@ |
|||||
|
'use strict'; |
||||
|
|
||||
|
const assert = require('assert'); |
||||
|
const spawn = require('child_process').spawn; |
||||
|
|
||||
|
|
||||
|
if (process.argv[2] === 'child') { |
||||
|
setImmediate(function() { |
||||
|
require('fs').readFileSync(__filename); |
||||
|
process.exit(); |
||||
|
}); |
||||
|
|
||||
|
} else { |
||||
|
(function runTest(flags) { |
||||
|
var execArgv = [flags.pop()]; |
||||
|
var args = [__filename, 'child']; |
||||
|
var child = spawn(process.execPath, execArgv.concat(args)); |
||||
|
var cntr = 0; |
||||
|
|
||||
|
child.stdout.on('data', function(chunk) { |
||||
|
throw new Error('UNREACHABLE'); |
||||
|
}); |
||||
|
|
||||
|
child.stderr.on('data', function(chunk) { |
||||
|
// Prints twice for --trace-sync-io. First for the require() and second
|
||||
|
// for the fs operation.
|
||||
|
if (/^WARNING[\s\S]*fs\.readFileSync/.test(chunk.toString())) |
||||
|
cntr++; |
||||
|
}); |
||||
|
|
||||
|
child.on('exit', function() { |
||||
|
if (execArgv[0] === '--trace-sync-io') |
||||
|
assert.equal(cntr, 2); |
||||
|
else if (execArgv[0] === ' ') |
||||
|
assert.equal(cntr, 0); |
||||
|
else |
||||
|
throw new Error('UNREACHABLE'); |
||||
|
|
||||
|
if (flags.length > 0) |
||||
|
setImmediate(runTest, flags); |
||||
|
}); |
||||
|
}(['--trace-sync-io', ' '])); |
||||
|
} |
||||
|
|
Loading…
Reference in new issue