dumping ground for random patches and texts
 help / color / mirror / Atom feed
* [PATCH] socket: reload fptr->fd after blocking operations
@ 2017-03-26  7:07 Eric Wong
  0 siblings, 0 replies; 2+ messages in thread
From: Eric Wong @ 2017-03-26  7:07 UTC (permalink / raw)
  To: spew

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


^ permalink raw reply related	[flat|nested] 2+ messages in thread

* [PATCH] socket: reload fptr->fd after blocking operations
@ 2017-03-26 22:42 Eric Wong
  0 siblings, 0 replies; 2+ messages in thread
From: Eric Wong @ 2017-03-26 22:42 UTC (permalink / raw)
  To: spew

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.
---
 ext/socket/basicsocket.c |  3 ++-
 ext/socket/unixsocket.c  |  6 ++++--
 test/socket/test_unix.rb | 16 ++++++++++++++++
 3 files changed, 22 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
diff --git a/test/socket/test_unix.rb b/test/socket/test_unix.rb
index 7edb5e5d4f..ff72a6990f 100644
--- a/test/socket/test_unix.rb
+++ b/test/socket/test_unix.rb
@@ -699,4 +699,20 @@ def test_accept_nonblock
       assert_equal :wait_readable, serv.accept_nonblock(exception: false)
     }
   end
+
+  def test_fd_passing_blocked
+    s1, s2 = UNIXSocket.pair
+    s1.nonblock = true
+    th = Thread.new do
+      begin
+        s1.recv_io
+      rescue => e
+        e
+      end
+    end
+    Thread.pass until th.stop?
+    s1.close
+    s2.close
+    assert_instance_of IOError, th.value
+  end
 end if defined?(UNIXSocket) && /cygwin/ !~ RUBY_PLATFORM
-- 
EW


^ permalink raw reply related	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2017-03-26 22:42 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2017-03-26 22:42 [PATCH] socket: reload fptr->fd after blocking operations Eric Wong
  -- strict thread matches above, loose matches on Subject: below --
2017-03-26  7:07 Eric Wong

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).