forked from nodejs/node
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
core: implement runtime flag to trace sync io
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: nodejs#1674 PR-URL: nodejs#1707 Reviewed-By: Ben Noordhuis <[email protected]> Reviewed-By: Fedor Indutny <[email protected]> Reviewed-By: Jeremiah Senkpiel <[email protected]> Reviewed-By: Petka Antonov <[email protected]>
- Loading branch information
1 parent
0662747
commit c1de6d2
Showing
10 changed files
with
137 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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', ' '])); | ||
} | ||
|