Created
June 13, 2011 11:20
-
-
Save dhruvbird/1022620 to your computer and use it in GitHub Desktop.
DNS callbacks are synchronous at times
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var dns = require('dns'); | |
console.log('Calling resolveSrv(localhost)'); | |
dns.lookup('localhost', function() { | |
console.log('Callback for resolve(localhost)'); | |
}); | |
console.log('After calling resolveSrv(localhost)'); | |
console.log('Calling resolveSrv(127.0.0.1)'); | |
dns.lookup('127.0.0.0', function() { | |
console.log('Callback for resolveSrv(127.0.0.1)'); | |
}); | |
console.log('After calling resolve(127.0.0.1)'); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Calling resolveSrv(localhost) | |
After calling resolveSrv(localhost) | |
Calling resolveSrv(127.0.0.1) | |
After calling resolve(127.0.0.1) | |
Callback for resolve(localhost) | |
Callback for resolveSrv(127.0.0.1) | |
(btw, this is what happens if lookup() is replaced with resolve()) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Calling resolveSrv(localhost) | |
Callback for resolve(localhost) | |
After calling resolveSrv(localhost) | |
Calling resolveSrv(127.0.0.1) | |
Callback for resolveSrv(127.0.0.1) | |
After calling resolve(127.0.0.1) |
What's the problem? You feel that the lookup() calls are blocking, right? AFAIK, there's no guarantee in which order and at which time the callbacks can really be called once they are passed to a non-blocking function. So, I'm not sure how you came up with your expected output. Both outputs can theoretically happen.
Yes, the callbacks to lookup are at times called before the function completes. I know that there is no guarantee of when the callback is actually called, but was wondering if it made sense to just guarantee that callbacks (if any) will surely be called after the complete function (caller) is finished executing. Just feel so because I've run into a bug where the author assumed otherwise and I had to debug for nearly 4 hrs.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Just ignore the fact that the console output says "resolveSrv" instead of "lookup"