dumping ground for random patches and texts
 help / color / mirror / Atom feed
* [PATCH] connect_nonblock(..., exception: false) does not raise EISCONN
@ 2015-04-16  2:25 Eric Wong
  0 siblings, 0 replies; only message in thread
From: Eric Wong @ 2015-04-16  2:25 UTC (permalink / raw)
  To: spew

This is to reduce exceptions for code which issues a (IMHO,
unnecessary) second connect() syscall.
---
 ext/socket/socket.c          | 6 ++++++
 test/socket/test_nonblock.rb | 1 +
 2 files changed, 7 insertions(+)

diff --git a/ext/socket/socket.c b/ext/socket/socket.c
index a514b9a..bb23703 100644
--- a/ext/socket/socket.c
+++ b/ext/socket/socket.c
@@ -509,6 +509,12 @@ sock_connect_nonblock(int argc, VALUE *argv, VALUE sock)
             }
             rb_readwrite_sys_fail(RB_IO_WAIT_WRITABLE, "connect(2) would block");
 	}
+	if (errno == EISCONN) {
+            if (!NIL_P(opts) &&
+                    Qfalse == rb_hash_lookup2(opts, sym_exception, Qundef)) {
+                return INT2FIX(0);
+            }
+	}
 	rsock_sys_fail_raddrinfo_or_sockaddr("connect(2)", addr, rai);
     }
 
diff --git a/test/socket/test_nonblock.rb b/test/socket/test_nonblock.rb
index 6912046..2973bdb 100644
--- a/test/socket/test_nonblock.rb
+++ b/test/socket/test_nonblock.rb
@@ -69,6 +69,7 @@ class TestSocketNonblock < Test::Unit::TestCase
       assert_equal :wait_writable, rv
     end
     assert_equal([ [], [c], [] ], IO.select(nil, [c], nil, 60))
+    assert_equal 0, c.connect_nonblock(servaddr, exception: false)
     s, sockaddr = serv.accept
     assert_equal(Socket.unpack_sockaddr_in(c.getsockname),
                  Socket.unpack_sockaddr_in(sockaddr))
-- 
EW


^ permalink raw reply related	[flat|nested] only message in thread

only message in thread, other threads:[~2015-04-16  2:25 UTC | newest]

Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2015-04-16  2:25 [PATCH] connect_nonblock(..., exception: false) does not raise EISCONN 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).