diff --git a/lib/net.js b/lib/net.js index 89688b1f03..32a6ec2b26 100644 --- a/lib/net.js +++ b/lib/net.js @@ -177,7 +177,7 @@ Socket.prototype._sendString = function (data, encoding) { buffer.length - buffer.used); bytesWritten = charsWritten; } - + buffer.used += bytesWritten; self.sendQueueSize += bytesWritten; diff --git a/src/node_net2.cc b/src/node_net2.cc index 1cebd86761..cab14bb987 100644 --- a/src/node_net2.cc +++ b/src/node_net2.cc @@ -54,11 +54,16 @@ static inline Local ErrnoException(int errorno, return e; } +static inline bool SetCloseOnExec(int fd) { + return (fcntl(fd, F_SETFD, FD_CLOEXEC) != -1); +} + static inline bool SetNonBlock(int fd) { - int flags = fcntl(fd, F_GETFL, 0); - if (flags == -1) return false; - flags |= O_NONBLOCK; - return (fcntl(fd, F_SETFL, flags) != -1); + return (fcntl(fd, F_SETFL, O_NONBLOCK) != -1); +} + +static inline bool SetSockFlags(int fd) { + return SetNonBlock(fd) && SetCloseOnExec(fd); } // Creates nonblocking pipe @@ -68,7 +73,7 @@ static Handle Pipe(const Arguments& args) { if (pipe(fds) < 0) return ThrowException(ErrnoException(errno, "pipe")); - if(!SetNonBlock(fds[0]) || !SetNonBlock(fds[1])) { + if (!SetSockFlags(fds[0]) || !SetSockFlags(fds[1])) { int fcntl_errno = errno; close(fds[0]); close(fds[1]); @@ -92,7 +97,7 @@ static Handle SocketPair(const Arguments& args) { return ThrowException(ErrnoException(errno, "socketpair")); } - if (!SetNonBlock(fds[0]) || !SetNonBlock(fds[1])) { + if (!SetSockFlags(fds[0]) || !SetSockFlags(fds[1])) { int fcntl_errno = errno; close(fds[0]); close(fds[1]); @@ -137,7 +142,7 @@ static Handle Socket(const Arguments& args) { if (fd < 0) return ThrowException(ErrnoException(errno, "socket")); - if (!SetNonBlock(fd)) { + if (!SetSockFlags(fd)) { int fcntl_errno = errno; close(fd); return ThrowException(ErrnoException(fcntl_errno, "fcntl")); @@ -387,10 +392,10 @@ static Handle Accept(const Arguments& args) { return ThrowException(ErrnoException(errno, "accept")); } - if (!SetNonBlock(peer_fd)) { + if (!SetSockFlags(peer_fd)) { int fcntl_errno = errno; close(peer_fd); - return ThrowException(ErrnoException(fcntl_errno, "fcntl", "Cannot make peer non-blocking")); + return ThrowException(ErrnoException(fcntl_errno, "fcntl")); } Local peer_info = Object::New(); diff --git a/test-net-server.js b/test-net-server.js index 76f10574ed..114cb67a85 100644 --- a/test-net-server.js +++ b/test-net-server.js @@ -42,7 +42,11 @@ c.addListener('drain', function () { sys.puts("!!!client drain"); }); -c.addListener('receive', function (d) { +c.addListener('data', function (d) { sys.puts("!!!client got: " + JSON.stringify(d.toString())); + c.close(); }); +c.addListener('dataEnd', function (d) { + sys.puts("!!!client eof"); +});