dumping ground for random patches and texts
 help / color / mirror / Atom feed
From: Eric Wong <e@80x24.org>
To: spew@80x24.org
Subject: [PATCH] socket: reload fptr->fd after blocking operations
Date: Sun, 26 Mar 2017 07:07:17 +0000	[thread overview]
Message-ID: <20170326070717.21513-1-e@80x24.org> (raw)

IO objects may be closed by other threads while inside an I/O
blocking region, setting fptr->fd to -1.  So we must reload it
to avoid waiting on an invalid FD.

FIXME: need tests
---
 ext/socket/basicsocket.c | 3 ++-
 ext/socket/unixsocket.c  | 6 ++++--
 2 files changed, 6 insertions(+), 3 deletions(-)

diff --git a/ext/socket/basicsocket.c b/ext/socket/basicsocket.c
index 3b6f22f36a..f79ab2d7cf 100644
--- a/ext/socket/basicsocket.c
+++ b/ext/socket/basicsocket.c
@@ -551,7 +551,8 @@ rsock_bsock_send(int argc, VALUE *argv, VALUE sock)
     arg.flags = NUM2INT(flags);
     while (rsock_maybe_fd_writable(arg.fd),
 	   (n = (ssize_t)BLOCKING_REGION_FD(func, &arg)) < 0) {
-	if (rb_io_wait_writable(arg.fd)) {
+	if (rb_io_wait_writable(fptr->fd)) {
+	    arg.fd = fptr->fd;
 	    continue;
 	}
 	rb_sys_fail("send(2)");
diff --git a/ext/socket/unixsocket.c b/ext/socket/unixsocket.c
index 5a44b552f8..669b33428b 100644
--- a/ext/socket/unixsocket.c
+++ b/ext/socket/unixsocket.c
@@ -264,8 +264,9 @@ unix_send_io(VALUE sock, VALUE val)
 
     arg.fd = fptr->fd;
     while ((int)BLOCKING_REGION_FD(sendmsg_blocking, &arg) == -1) {
-	if (!rb_io_wait_writable(arg.fd))
+	if (!rb_io_wait_writable(fptr->fd))
 	    rsock_sys_fail_path("sendmsg(2)", fptr->pathv);
+	arg.fd = fptr->fd;
     }
 
     return Qnil;
@@ -359,8 +360,9 @@ unix_recv_io(int argc, VALUE *argv, VALUE sock)
 
     arg.fd = fptr->fd;
     while ((int)BLOCKING_REGION_FD(recvmsg_blocking, &arg) == -1) {
-	if (!rb_io_wait_readable(arg.fd))
+	if (!rb_io_wait_readable(fptr->fd))
 	    rsock_sys_fail_path("recvmsg(2)", fptr->pathv);
+	arg.fd = fptr->fd;
     }
 
 #if FD_PASSING_BY_MSG_CONTROL
-- 
EW


             reply	other threads:[~2017-03-26  7:07 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2017-03-26  7:07 Eric Wong [this message]
  -- strict thread matches above, loose matches on Subject: below --
2017-03-26 22:42 [PATCH] socket: reload fptr->fd after blocking operations Eric Wong

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20170326070717.21513-1-e@80x24.org \
    --to=e@80x24.org \
    --cc=spew@80x24.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).