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

[feature] Write data to file on SIGINT unless specified #11

Closed
wants to merge 2 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
16 changes: 16 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,22 @@ The visualizer is WIP, you are welcome to contribute with major changes to the e
* Red: time spent executing the callback code (blocking).
* Black: when the async request was made.

## SIGINT (Ctrl-C) Behavior

To help debug process that may not have a defined exit, or may have a
problematic handle keeping them open, dprof registers a `SIGINT` handler
by default.
This allows dprof to capture when a process is interrupted by the user,
and it will write the JSON data and terminate.

If this overriding terminate behavior is undesired, or interferes with an
existing handler in a program, either a `--dprof-no-sigint` flag can be
provided to the program, or a `DPROF_NO_SIGINT=1` environment variable.

```
node -r dprof my-script.js --dprof-no-sigint
```

## Format

The `dprof.json.gz` file is a GZIP compressed JSON file. It is possible
Expand Down
17 changes: 15 additions & 2 deletions dprof.js
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,20 @@ asyncHook.enable();
// Print result
//

process.on('exit', function () {
if (process.argv.indexOf('--dprof-no-sigint') === -1 &&
!process.env.hasOwnProperty('DPROF_NO_SIGINT')) {
process.on('SIGINT', function handleSIGINT() {
writeDataFile();

// Trigger node's default handler
process.removeAllListeners('SIGINT');
process.kill(process.pid, 'SIGINT');
});
}

process.on('exit', writeDataFile);

function writeDataFile() {
// even though zlib is sync, it still fires async_wrap events,
// so disable asyncWrap just to be sure.
asyncHook.disable();
Expand All @@ -163,4 +176,4 @@ process.on('exit', function () {
} else {
fs.writeFileSync('./dprof.json.gz', zlib.gzipSync(JSON.stringify(data)));
}
});
}
Loading