Skip to content

Add trace flag for configuring long stack traces #713

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

Merged
merged 1 commit into from
Jan 17, 2014
Merged
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
3 changes: 3 additions & 0 deletions Readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,9 @@ issue [#501](https://github.com/felixge/node-mysql/issues/501). (Default: `'fals
* `dateStrings`: Force date types (TIMESTAMP, DATETIME, DATE) to be returned as strings rather then
inflated into JavaScript Date objects. (Default: `false`)
* `debug`: Prints protocol details to stdout. (Default: `false`)
* `trace`: Generates stack traces on `Error` to include call site of library
entrance ("long stack traces"). Slight performance penalty for most calls.
(Default: `true`)
* `multipleStatements`: Allow multiple mysql statements per query. Be careful
with this, it exposes you to SQL injection attacks. (Default: `false`)
* `flags`: List of connection flags to use other than the default ones. It is
Expand Down
1 change: 1 addition & 0 deletions lib/ConnectionConfig.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ function ConnectionConfig(options) {
this.bigNumberStrings = options.bigNumberStrings || false;
this.dateStrings = options.dateStrings || false;
this.debug = options.debug;
this.trace = options.trace !== false;
this.stringifyObjects = options.stringifyObjects || false;
this.timezone = options.timezone || 'local';
this.flags = options.flags || '';
Expand Down
5 changes: 5 additions & 0 deletions lib/protocol/Protocol.js
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,11 @@ Protocol.prototype._enqueue = function(sequence) {
return sequence;
}

if (this._config.trace) {
// Long stack trace support
sequence._callSite = new Error;
}

this._queue.push(sequence);

var self = this;
Expand Down
11 changes: 7 additions & 4 deletions lib/protocol/sequences/Sequence.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,8 @@ function Sequence(callback) {
EventEmitter.call(this);

this._callback = callback;
this._callSite = null;
this._ended = false;

// Experimental: Long stack trace support
this._callSite = new Error;
}

Sequence.determinePacket = function(byte) {
Expand All @@ -38,7 +36,12 @@ Sequence.prototype._packetToError = function(packet) {
};

Sequence.prototype._addLongStackTrace = function(err) {
if (!this._callSite) {
return;
}

var delimiter = '\n --------------------\n' ;

if (err.stack.indexOf(delimiter) > -1) {
return;
}
Expand All @@ -62,7 +65,7 @@ Sequence.prototype.end = function(err) {
// causes a cyclic reference that the GC does not detect properly, but I was
// unable to produce a standalone version of this leak. This would be a great
// challenge for somebody interested in difficult problems : )!
delete this._callSite;
this._callSite = null;

// try...finally for exception safety
try {
Expand Down
14 changes: 14 additions & 0 deletions test/integration/connection/test-long-stack-traces-disabled.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
var common = require('../../common');
var connection = common.createConnection({trace: false});
var assert = require('assert');

var err;
connection.query('invalid sql', function(_err) {
err = _err;
});

connection.end();

process.on('exit', function() {
assert.ok(err.stack.indexOf(__filename) < 0);
});
2 changes: 0 additions & 2 deletions test/integration/connection/test-long-stack-traces.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
// Experimental: https://github.com/felixge/node-mysql/issues/198

var common = require('../../common');
var connection = common.createConnection();
var assert = require('assert');
Expand Down