cujojs / when

A solid, fast Promises/A+ and when() implementation, plus other async goodies.
Other
3.44k stars 396 forks source link

cross library nextTick interleaving can cause deprecation in node 0.10.x #410

Closed stefanpenner closed 9 years ago

stefanpenner commented 9 years ago

In node 0.10.x Cross library async interleaving can result in (node) warning: Recursive process.nextTick detected. This will break in the next version of node. Please use setImmediate for recursive deferral.

Although, when typically avoids this with its own micro task queue:

https://github.com/cujojs/when/blob/25b3c0ad06292d3d46ca27966cc026407eaa2184/lib/env.js#L25

In some scenarios, it is possible to interleave two such micro tasks queues in a way that still results in the limit being hit.

In addition to interleaving between different libraries due to the node_module's dupe friendly module resolution strategy, it is common to have multiple copies of the same library present and interacting with one another.

A quick example:

var A = require('./path/to/rsvp_a').Promise;
var B = require('./path/to/rsvp_b').Promise;

A.resolve().then(function() {
  console.log('first nextTick');
  B.resolve().then(function() {
    console.log('second nextTick');
    A.resolve().then(function() {
      console.log('third nextTick');
    });
  });
});

An example failing test thanks to @taras https://github.com/tildeio/rsvp.js/pull/337/files#diff-e7e77ddad631a023d39a62f3ba8b7f17R2524

this limit has been removed in node 0.11.0

https://github.com/iojs/io.js/commit/0761c90204d7a0134c657e20f91bd83bfa6e677a https://github.com/iojs/io.js/commit/0761c90204d7a0134c657e20f91bd83bfa6e677a

Unfortunately our solution was to fallback to setImmediate in node 0.10.x.

I wasn't able to think of a better solution, but that doesn't mean a better one doesn't exist so if someone has one feel free to share :)

related:

https://github.com/tildeio/rsvp.js/pull/337 https://github.com/petkaantonov/bluebird/issues/399 https://github.com/kriskowal/asap/issues/51

briancavalier commented 9 years ago

Ugh, thanks @stefanpenner. I guess I knew this was possible, but it always seemed pretty unlikely ... perhaps not so unlikely after all! Falling back to setImmediate seems reasonable. It stinks, though, that the only way to detect it seems to be a nasty node-specific version check :(

briancavalier commented 9 years ago

No longer an issue in node 0.12 and iojs

stefanpenner commented 9 years ago

Ya, thank god