Skip to content

Commit

Permalink
Fix case where pre-domain nextTick executing post-domain callbacks re…
Browse files Browse the repository at this point in the history
…sulted in domains being ignored
  • Loading branch information
iarna authored and petkaantonov committed Mar 18, 2014
1 parent b6a3385 commit 134c396
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 2 deletions.
2 changes: 1 addition & 1 deletion Gruntfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,7 @@ module.exports = function( grunt ) {
RangeError: true,
__DEBUG__: false,
__BROWSER__: false,
process: false,
process: true,
"console": false,
"require": false,
"module": false,
Expand Down
18 changes: 17 additions & 1 deletion src/async.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ var schedule = require("./schedule.js");
var Queue = require("./queue.js");
var errorObj = require("./util.js").errorObj;
var tryCatch1 = require("./util.js").tryCatch1;
var process = require("./global.js").process;

function Async() {
this._isTickUsed = false;
Expand All @@ -27,13 +28,23 @@ Async.prototype.haveItemsQueued = function Async$haveItemsQueued() {
Async.prototype.invokeLater = function Async$invokeLater(fn, receiver, arg) {
ASSERT(typeof fn === "function");
ASSERT(arguments.length === 3);
if (process !== void 0 &&
process.domain != null &&
!fn.domain) {
fn = process.domain.bind(fn);
}
this._lateBuffer.push(fn, receiver, arg);
this._queueTick();
};

Async.prototype.invoke = function Async$invoke(fn, receiver, arg) {
ASSERT(typeof fn === "function");
ASSERT(arguments.length === 3);
if (process !== void 0 &&
process.domain != null &&
!fn.domain) {
fn = process.domain.bind(fn);
}
var functionBuffer = this._functionBuffer;
functionBuffer.push(fn, receiver, arg);
this._length = functionBuffer.length();
Expand Down Expand Up @@ -63,7 +74,12 @@ Async.prototype._consumeLateBuffer = function Async$_consumeLateBuffer() {
var res = tryCatch1(fn, receiver, arg);
if (res === errorObj) {
this._queueTick();
throw res.e;
if (fn.domain != null) {
fn.domain.emit("error", res.e);
}
else {
throw res.e;
}
}
}
};
Expand Down
35 changes: 35 additions & 0 deletions test/mocha/domain.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
"use strict";

var assert = require("assert");

var adapter = require("../../js/debug/bluebird.js");
var fulfilled = adapter.fulfilled;
var rejected = adapter.rejected;
var pending = adapter.pending;
var Promise = adapter;
var isNodeJS = typeof process !== "undefined" &&
typeof process.execPath === "string";


if (isNodeJS) {
describe("domain", function(){
specify("gh-148", function(done) {
Promise.onPossiblyUnhandledRejection(function(error,promise) {
throw error
});
var called = false;
var e = new Error();
Promise.resolve(23).then(function(){called = true});
require('domain').create()
.on('error', function(E) {
assert.equal(e, E);
assert(called);
done();
})
.run(function() {
var P = new Promise(function(resolve,reject){ reject(e) });
});

});
});
}

0 comments on commit 134c396

Please sign in to comment.