Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Loading…

defer dgram listening event #3655

Closed
wants to merge 1 commit into from

2 participants

@tjfontaine

dgram .bind's ._handle.lookup may be synchronous if bind is called without an address or if address is indeed an ip. This means 'listening' may fire before a caller has assigned a handler for the event.

This patch defers that event.

However if .bind is called with a dns name the event may be deferred a second tick since dns implicitly defers requests.

@tjfontaine tjfontaine referenced this pull request in tjfontaine/node-dns
Closed

Serve on process.nextTick #19

@bnoordhuis

I say it's a bug in .lookup() - callbacks are not supposed to run on the same tick to avoid infinite loop / stack overflow issues.

@tjfontaine

I don't disagree my initial gut said to change the behavior of lookup but then there's this comment https://github.com/joyent/node/blob/master/lib/dgram.js#L46

@bnoordhuis

Yeah, I wrote that. In retrospect, that was probably a mistake - let's revisit it.

@tjfontaine

Just to be clear, you want to revisit the bind on send flow such that we can allow lookup to be async?

@tjfontaine

Some of the dgram tests work off the assumption that .bind is synchronous, for instance calling setTTL or addMembership immediately.

In reality they should be waiting for the listening event to ensure that the handle creation has completed. if we start enforcing these semantics now it will change the behavior for the simple case of .bind()

There's also one FIXME in the tests that indicates this is actually a deficiency in libuv, that one should be able to change membership or ttl without first calling bind, I don't agree with that assesment.

@tjfontaine

I've update the pullreq with what I think are the proper semantics

@bnoordhuis bnoordhuis commented on the diff
lib/dgram.js
@@ -110,6 +105,23 @@ exports.createSocket = function(type, listener) {
return new Socket(type, listener);
};
+Socket.prototype._bind = function(ip, port) {
+ var ret;
+
+ if (!ip) {
+ if (this.type == 'udp4')
+ ip = '0.0.0.0';
+ else if (this.type == 'udp6')
+ ip = '::0';
else
  assert(0); // or throw Error("unknown socket type")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
@bnoordhuis

@tjfontaine Maybe I'm missing something subtle here but isn't this patch all you need to make lookups always async?

diff --git a/lib/dgram.js b/lib/dgram.js
index 7abd0c3..4cfc6e6 100644
--- a/lib/dgram.js
+++ b/lib/dgram.js
@@ -43,11 +43,6 @@ function isIP(address) {


 function lookup(address, family, callback) {
-  // implicit 'bind before send' needs to run on the same tick
-  var matchedFamily = isIP(address);
-  if (matchedFamily)
-    return callback(null, address, matchedFamily);
-
   if (!dns)
     dns = require('dns');
@tjfontaine

Yes that was going to be my initial change, however I was trying to preserve the behavior such that implicit bind on send wouldn't delay another tick

With either approach though it changes when the handle actually gets the fd for things like setTTL to work, this should be the behavior people were expecting before but the tests are written contrary to that, does this mean the change won't make it into 0.8?

@bnoordhuis

does this mean the change won't make it into 0.8?

Yes. Or rather - no, it won't. It's a change in behavior and therefore not eligible for inclusion in the stable branch.

@tjfontaine

It's not entirely a change anyone who calls .bind('foo.domain.com') or .bind('localhost') is already experiencing this behavior :)

@tjfontaine

Right after retesting the reason I couldn't just do that is throw new Error('implicit bind failed'); from https://github.com/joyent/node/blob/master/lib/dgram.js#L316-322 which is explicitly testing that things are created synchronously

@bnoordhuis

Right, that code should go.

@bnoordhuis

Half-merged in edd3de8. The issue's been addressed in 332fea5. Thanks, Timothy.

@bnoordhuis bnoordhuis closed this
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Jul 6, 2012
  1. @tjfontaine
This page is out of date. Refresh to see the latest.
View
33 lib/dgram.js
@@ -43,11 +43,6 @@ function isIP(address) {
function lookup(address, family, callback) {
- // implicit 'bind before send' needs to run on the same tick
- var matchedFamily = isIP(address);
- if (matchedFamily)
- return callback(null, address, matchedFamily);
-
if (!dns)
dns = require('dns');
@@ -110,6 +105,23 @@ exports.createSocket = function(type, listener) {
return new Socket(type, listener);
};
+Socket.prototype._bind = function(ip, port) {
+ var ret;
+
+ if (!ip) {
+ if (this.type == 'udp4')
+ ip = '0.0.0.0';
+ else if (this.type == 'udp6')
+ ip = '::0';
else
  assert(0); // or throw Error("unknown socket type")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
+ }
+
+ ret = this._handle.bind(ip, port || 0, /*flags=*/0);
+
+ if (!ret)
+ this._bound = true;
+
+ return ret;
+};
Socket.prototype.bind = function(port, address) {
var self = this;
@@ -119,22 +131,17 @@ Socket.prototype.bind = function(port, address) {
// resolve address first
self._handle.lookup(address, function(err, ip) {
if (!err) {
- if (self._handle.bind(ip, port || 0, /*flags=*/0)) {
+ if (self._bind(ip, port)) {
err = errnoException(errno, 'bind');
}
else {
- self._bound = true;
self._startReceiving();
self.emit('listening');
}
}
if (err) {
- // caller may not have had a chance yet to register its
- // error event listener so defer the error to the next tick
- process.nextTick(function() {
- self.emit('error', err);
- });
+ self.emit('error', err);
}
});
};
@@ -314,7 +321,7 @@ Socket.prototype._startReceiving = function() {
return;
if (!this._bound) {
- this.bind(); // bind to random port
+ this._bind(); // bind to random port
// sanity check
if (!this._bound)
View
4 test/simple/test-dgram-broadcast-multi-process.js
@@ -162,7 +162,9 @@ if (process.argv[2] !== 'child') {
// bind the address explicitly for sending
// INADDR_BROADCAST to only one interface
sendSocket.bind(common.PORT, bindAddress);
- sendSocket.setBroadcast(true);
+ sendSocket.on('listening', function () {
+ sendSocket.setBroadcast(true);
+ });
sendSocket.on('close', function() {
console.error('[PARENT] sendSocket closed');
View
43 test/simple/test-dgram-listen-after-bind.js
@@ -0,0 +1,43 @@
+// Copyright Joyent, Inc. and other Node contributors.
+//
+// Permission is hereby granted, free of charge, to any person obtaining a
+// copy of this software and associated documentation files (the
+// "Software"), to deal in the Software without restriction, including
+// without limitation the rights to use, copy, modify, merge, publish,
+// distribute, sublicense, and/or sell copies of the Software, and to permit
+// persons to whom the Software is furnished to do so, subject to the
+// following conditions:
+//
+// The above copyright notice and this permission notice shall be included
+// in all copies or substantial portions of the Software.
+//
+// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
+// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
+// NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
+// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
+// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
+// USE OR OTHER DEALINGS IN THE SOFTWARE.
+
+var common = require('../common');
+var assert = require('assert');
+var dgram = require('dgram');
+
+var socket = dgram.createSocket('udp4');
+
+socket.bind();
+
+var fired = false;
+var timer = setTimeout(function () {
+ socket.close();
+}, 100);
+
+socket.on('listening', function () {
+ clearTimeout(timer);
+ fired = true;
+ socket.close();
+});
+
+socket.on('close', function () {
+ assert(fired, 'listening should fire after bind');
+});
View
15 test/simple/test-dgram-multicast-multi-process.js
@@ -149,10 +149,13 @@ if (process.argv[2] !== 'child') {
// call is what creates the actual socket...
sendSocket.bind();
- sendSocket.setTTL(1);
- sendSocket.setBroadcast(true);
- sendSocket.setMulticastTTL(1);
- sendSocket.setMulticastLoopback(true);
+ // The socket is actually created async now
+ sendSocket.on('listening', function () {
+ sendSocket.setTTL(1);
+ sendSocket.setBroadcast(true);
+ sendSocket.setMulticastTTL(1);
+ sendSocket.setMulticastLoopback(true);
+ });
sendSocket.on('close', function() {
console.error('[PARENT] sendSocket closed');
@@ -221,5 +224,7 @@ if (process.argv[2] === 'child') {
listenSocket.bind(common.PORT);
- listenSocket.addMembership(LOCAL_BROADCAST_HOST);
+ listenSocket.on('listening', function () {
+ listenSocket.addMembership(LOCAL_BROADCAST_HOST);
+ });
}
View
22 test/simple/test-dgram-multicast-setTTL.js
@@ -26,16 +26,18 @@ var common = require('../common'),
socket = dgram.createSocket('udp4');
socket.bind(common.PORT);
-socket.setMulticastTTL(16);
+socket.on('listening', function () {
+ socket.setMulticastTTL(16);
-//Try to set an invalid TTL (valid ttl is > 0 and < 256)
-try {
- socket.setMulticastTTL(1000);
-} catch (e) {
- thrown = true;
-}
+ //Try to set an invalid TTL (valid ttl is > 0 and < 256)
+ try {
+ socket.setMulticastTTL(1000);
+ } catch (e) {
+ thrown = true;
+ }
-assert(thrown, 'Setting an invalid multicast TTL should throw some error');
+ assert(thrown, 'Setting an invalid multicast TTL should throw some error');
-//close the socket
-socket.close();
+ //close the socket
+ socket.close();
+});
Something went wrong with that request. Please try again.