From 460614125be90a39c95aa252348e8d211735d6eb Mon Sep 17 00:00:00 2001 From: Ben Noordhuis Date: Wed, 10 Aug 2011 23:59:21 +0200 Subject: [PATCH] tcp: propagate libuv tcp accept() errors to net_uv.js --- lib/net_uv.js | 5 +++++ src/tcp_wrap.cc | 40 +++++++++++++++++++++------------------- 2 files changed, 26 insertions(+), 19 deletions(-) diff --git a/lib/net_uv.js b/lib/net_uv.js index 4c11be2ecf..c89c263bfa 100644 --- a/lib/net_uv.js +++ b/lib/net_uv.js @@ -653,6 +653,11 @@ function onconnection(clientHandle) { debug("onconnection"); + if (!clientHandle) { + self.emit('error', errnoException(errno, 'accept')); + return; + } + if (self.maxConnections && self.connections >= self.maxConnections) { clientHandle.close(); return; diff --git a/src/tcp_wrap.cc b/src/tcp_wrap.cc index 7e6e152cbf..8c4b9a990b 100644 --- a/src/tcp_wrap.cc +++ b/src/tcp_wrap.cc @@ -214,27 +214,29 @@ class TCPWrap : public StreamWrap { // uv_close() on the handle. assert(wrap->object_.IsEmpty() == false); - if (status != 0) { - // TODO Handle server error (set errno and call onconnection with NULL) - assert(0); - return; + Handle argv[1]; + + if (status == 0) { + // Instantiate the client javascript object and handle. + Local client_obj = tcpConstructor->NewInstance(); + + // Unwrap the client javascript object. + assert(client_obj->InternalFieldCount() > 0); + TCPWrap* client_wrap = + static_cast(client_obj->GetPointerFromInternalField(0)); + + int r = uv_accept(handle, (uv_stream_t*)&client_wrap->handle_); + + // uv_accept should always work. + assert(r == 0); + + // Successful accept. Call the onconnection callback in JavaScript land. + argv[0] = client_obj; + } else { + SetErrno(uv_last_error().code); + argv[0] = v8::Null(); } - // Instanciate the client javascript object and handle. - Local client_obj = tcpConstructor->NewInstance(); - - // Unwrap the client javascript object. - assert(client_obj->InternalFieldCount() > 0); - TCPWrap* client_wrap = - static_cast(client_obj->GetPointerFromInternalField(0)); - - int r = uv_accept(handle, (uv_stream_t*)&client_wrap->handle_); - - // uv_accept should always work. - assert(r == 0); - - // Successful accept. Call the onconnection callback in JavaScript land. - Local argv[1] = { client_obj }; MakeCallback(wrap->object_, "onconnection", 1, argv); }