Skip to content

Commit

Permalink
Fix chrome devtools async stack traceability
Browse files Browse the repository at this point in the history
By skipping trampoline when longStackTraces are enabled
in chrome there is no performance effect to server code
and client side debuggability is not harmed.

Fixes #542
  • Loading branch information
petkaantonov committed Mar 29, 2015
1 parent dabe3ce commit d37d0ff
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 7 deletions.
1 change: 1 addition & 0 deletions .jshintrc
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@
"Error": true,
"args": true,
"JSON": true,
"chrome": true,
"INLINE_SLICE": false,
"global": true,
"setImmediate": true,
Expand Down
49 changes: 42 additions & 7 deletions src/async.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,13 @@ var ASSERT = require("./assert.js");
var schedule = require("./schedule.js");
var Queue = require("./queue.js");
var _process = typeof process !== "undefined" ? process : undefined;
var util = require("./util.js");

function Async() {
this._isTickUsed = false;
this._lateQueue = new Queue(LATE_QUEUE_CAPACITY);
this._normalQueue = new Queue(NORMAL_QUEUE_CAPACITY);
this._trampolineEnabled = true;
var self = this;
this.drainQueues = function () {
self._drainQueues();
Expand All @@ -18,6 +20,12 @@ function Async() {
schedule.isStatic ? schedule(this.drainQueues) : schedule;
}

Async.prototype.disableTrampolineIfNecessary = function() {
if (util.hasDevTools) {
this._trampolineEnabled = false;
}
};

Async.prototype.haveItemsQueued = function () {
return this._normalQueue.length() > 0;
};
Expand Down Expand Up @@ -54,27 +62,54 @@ Async.prototype.throwLater = function(fn, arg) {

//When the fn absolutely needs to be called after
//the queue has been completely flushed
Async.prototype.invokeLater = function (fn, receiver, arg) {
function AsyncInvokeLater(fn, receiver, arg) {
ASSERT(arguments.length === 3);
fn = this._withDomain(fn);
this._lateQueue.push(fn, receiver, arg);
this._queueTick();
};
}

Async.prototype.invokeFirst = function (fn, receiver, arg) {
function AsyncInvoke(fn, receiver, arg) {
ASSERT(arguments.length === 3);
fn = this._withDomain(fn);
this._normalQueue.unshift(fn, receiver, arg);
this._normalQueue.push(fn, receiver, arg);
this._queueTick();
};
}

if (!util.hasDevTools) {
Async.prototype.invokeLater = AsyncInvokeLater;
Async.prototype.invoke = AsyncInvoke;
} else {
Async.prototype.invokeLater = function (fn, receiver, arg) {
if (this._trampolineEnabled) {
AsyncInvokeLater(fn, receiver, arg);
} else {
setTimeout(function() {
fn.call(receiver, arg);
}, 1);
}
};

Async.prototype.invoke = function (fn, receiver, arg) {
if (this._trampolineEnabled) {
AsyncInvoke(fn, receiver, arg);
} else {
setTimeout(function() {
fn.call(receiver, arg);
}, 0);
}
};
}

Async.prototype.invoke = function (fn, receiver, arg) {
Async.prototype.invokeFirst = function (fn, receiver, arg) {
ASSERT(arguments.length === 3);
fn = this._withDomain(fn);
this._normalQueue.push(fn, receiver, arg);
this._normalQueue.unshift(fn, receiver, arg);
this._queueTick();
};



Async.prototype.settlePromises = function(promise) {
this._normalQueue._pushOne(promise);
this._queueTick();
Expand Down
7 changes: 7 additions & 0 deletions src/debuggability.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@ var debugging = __DEBUG__ || (util.isNode &&
(!!process.env["BLUEBIRD_DEBUG"] ||
process.env["NODE_ENV"] === "development"));

if (debugging) {
async.disableTrampolineIfNecessary();
}

Promise.prototype._ensurePossibleRejectionHandled = function () {
this._setRejectionIsUnhandled();
async.invokeLater(this._notifyUnhandledRejection, this, undefined);
Expand Down Expand Up @@ -134,6 +138,9 @@ Promise.longStackTraces = function () {
throw new Error(LONG_STACK_TRACES_ERROR);
}
debugging = CapturedTrace.isSupported();
if (debugging) {
async.disableTrampolineIfNecessary();
}
};

Promise.hasLongStackTraces = function () {
Expand Down
2 changes: 2 additions & 0 deletions src/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -277,6 +277,8 @@ var ret = {
markAsOriginatingFromRejection: markAsOriginatingFromRejection,
classString: classString,
copyDescriptors: copyDescriptors,
hasDevTools: typeof chrome !== "undefined" && chrome &&
typeof chrome.loadTimes === "function",
isNode: typeof process !== "undefined" &&
classString(process).toLowerCase() === "[object process]"
};
Expand Down
1 change: 1 addition & 0 deletions tools/jshintrc_generator.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ var globals = fs.readFileAsync(constantsFile, "utf8").then(function(contents) {
Error: true,
args: true,
JSON: true,
chrome: true,
INLINE_SLICE: false,
global: true,
setImmediate: true,
Expand Down

0 comments on commit d37d0ff

Please sign in to comment.