Subject: WaitForConnect may only call back to CreateClientSocket when the socket is writable or something. When it does so for a pending socket, it is falsely assumed to have succeeded and a subsequent read/write operation will fail.

From: max <[email-masked]>
Date: Mon, 10 Mar 2014 18:11:08 +0000

---
 tests/socket.test  | 4 ++--
 unix/tclUnixSock.c | 4 +++-
 2 files changed, 5 insertions(+), 3 deletions(-)

diff --git a/tests/socket.test b/tests/socket.test
index bfe6990..a21bb8d 100644
--- a/tests/socket.test
+++ b/tests/socket.test
@@ -2006,7 +2006,7 @@ test socket-14.8.0 {pending [socket -async] and nonblocking [gets], server is IP
         set sock [socket -async localhost $port]
         fconfigure $sock -blocking 0
         for {set i 0} {$i < 50} {incr i } {
-            if {[set x [gets $sock]] ne ""} break
+            if {[catch {gets $sock} x] || $x ne "" || ![fblocked $sock]} break
             after 200
         }
         set x
@@ -2032,7 +2032,7 @@ test socket-14.8.1 {pending [socket -async] and nonblocking [gets], server is IP
         set sock [socket -async localhost $port]
         fconfigure $sock -blocking 0
         for {set i 0} {$i < 50} {incr i } {
-            if {[set x [gets $sock]] ne ""} break
+            if {[catch {gets $sock} x] || $x ne "" || ![fblocked $sock]} break
             after 200
         }
         set x
diff --git a/unix/tclUnixSock.c b/unix/tclUnixSock.c
index 41d729e..6e84ed5 100644
--- a/unix/tclUnixSock.c
+++ b/unix/tclUnixSock.c
@@ -417,7 +417,9 @@ WaitForConnect(
 	errno = 0;
 	state = TclUnixWaitForFile(statePtr->fds.fd,
 		TCL_WRITABLE | TCL_EXCEPTION, timeOut);
-        CreateClientSocket(NULL, statePtr);
+        if (state != 0) {
+            CreateClientSocket(NULL, statePtr);
+        }
         if (statePtr->flags & TCP_ASYNC_CONNECT) {
             /* We are still in progress, so ignore the result of the last
              * attempt */
-- 
cgit v0.12