Skip to content

Instantly share code, notes, and snippets.

@Olegas
Last active December 17, 2015 07:39
Show Gist options
  • Save Olegas/5574583 to your computer and use it in GitHub Desktop.
Save Olegas/5574583 to your computer and use it in GitHub Desktop.
NodeJS 0.8+ `dns.resolve4` bug. Callback is not invoked within domain.
/**
* Checked under 0.8.11, 0.8.23, 0.10.4
*
* macbook:work olegelifantev$ node -v
* v0.10.5
*
* Expected output:
* macbook:work olegelifantev$ node dns-domain.js
* Lookup. Domain present? yes
* Resolve4. Domain present? yes
* Resolve4. Domain still present? yes
* Lookup. Domain still present? yes
*
* Actual output:
* macbook:work olegelifantev$ node dns-domain.js
* Lookup. Domain present? yes
* Resolve4. Domain present? yes
* Resolve4. Domain still present? no // Incorrect
* Lookup. Domain still present? yes
*/
var domain = require('domain');
var dns = require('dns');
var d1 = domain.create();
d1.run(function(){
console.log("Lookup. Domain present? " + (process.domain ? 'yes' : 'no'));
dns.lookup('google.com', 4, function(){
console.log("Lookup. Domain still present? " + (process.domain ? 'yes' : 'no'));
});
});
// When using DNS methods, which is bound to C-Ares, theirs callbacks are not implicitly bound to current domain
var d2 = domain.create();
d2.run(function(){
console.log("Resolve4. Domain present? " + (process.domain ? 'yes' : 'no'));
dns.resolve4('google.com', function(){
console.log("Resolve4. Domain still present? " + (process.domain ? 'yes' : 'no'));
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment