From bd52dcf0bce4470a04e2ec1592cc7e943ad4a2ab Mon Sep 17 00:00:00 2001 From: "hyunghwan.chung" Date: Mon, 7 May 2018 17:22:04 +0000 Subject: [PATCH] added extra checks for socket accept --- moo/kernel/Socket.moo | 3 +-- moo/mod/sck.c | 22 +++++++++++++++++++--- 2 files changed, 20 insertions(+), 5 deletions(-) diff --git a/moo/kernel/Socket.moo b/moo/kernel/Socket.moo index 38d73ca..8fe7036 100644 --- a/moo/kernel/Socket.moo +++ b/moo/kernel/Socket.moo @@ -493,7 +493,7 @@ class ServerSocket(Socket) | cliaddr clisck cliact | cliaddr := SocketAddress new. clisck := self _accept: cliaddr. - if (clisck notError) + if (clisck notNil) { ## the _accept method doesn't invoke the initialize method. ## i should invoke it manually here. @@ -506,7 +506,6 @@ class ServerSocket(Socket) clisck beWatched. } else { clisck close }. - }. ]. } diff --git a/moo/mod/sck.c b/moo/mod/sck.c index 64c1527..87d8f88 100644 --- a/moo/mod/sck.c +++ b/moo/mod/sck.c @@ -218,22 +218,38 @@ static moo_pfrc_t pf_accept_socket (moo_t* moo, moo_mod_t* mod, moo_ooi_t nargs) newfd = accept4(fd, (struct sockaddr*)MOO_OBJ_GET_BYTE_SLOT(arg), &addrlen, SOCK_NONBLOCK | SOCK_CLOEXEC); if (newfd == -1) { - if (errno != ENOSYS) + if (errno == ENOSYS) goto normal_accept; + + if (errno != EWOULDBLOCK && errno != EAGAIN) { moo_seterrwithsyserr (moo, errno); return MOO_PF_FAILURE; } + + /* return nil if accept() is not ready to accept a socket */ + MOO_STACK_SETRET (moo, nargs, moo->_nil); + return MOO_PF_SUCCESS; } else { goto accept_done; } +normal_accept: #endif + newfd = accept(fd, (struct sockaddr*)MOO_OBJ_GET_BYTE_SLOT(arg), &addrlen); if (newfd == -1) { - moo_seterrwithsyserr (moo, errno); - return MOO_PF_FAILURE; + if (errno != EWOULDBLOCK && errno != EAGAIN) + { + moo_seterrwithsyserr (moo, errno); + return MOO_PF_FAILURE; + } + + + /* return nil if accept() is not ready to accept a socket */ + MOO_STACK_SETRET (moo, nargs, moo->_nil); + return MOO_PF_SUCCESS; } fl = fcntl(newfd, F_GETFL, 0); if (fl == -1)