]> granicus.if.org Git - vim/commitdiff
patch 7.4.1286 v7.4.1286
authorBram Moolenaar <Bram@vim.org>
Sun, 7 Feb 2016 20:29:00 +0000 (21:29 +0100)
committerBram Moolenaar <Bram@vim.org>
Sun, 7 Feb 2016 20:29:00 +0000 (21:29 +0100)
Problem:    ch_open() with a timeout doesn't work correctly.
Solution:   Change how select() is used.  Don't give an error on timeout.
            Add a test for ch_open() failing.

src/channel.c
src/testdir/test_channel.vim
src/version.c

index 1d12ee72240d940df346e8351d357a2e06aa2b1f..8e36808a4bee2060676a3e2decfcf3232ecd5065 100644 (file)
@@ -431,18 +431,16 @@ channel_open(char *hostname, int port_in, int waittime, void (*close_cb)(void))
        }
     }
 
-    if (waittime >= 0)
+    if (waittime >= 0 && ret < 0)
     {
        struct timeval  tv;
-       fd_set          rfds, wfds;
+       fd_set          wfds;
 
-       FD_ZERO(&rfds);
        FD_ZERO(&wfds);
-       FD_SET(sd, &rfds);
        FD_SET(sd, &wfds);
        tv.tv_sec = waittime / 1000;
        tv.tv_usec = (waittime % 1000) * 1000;
-       ret = select((int)sd+1, &rfds, &wfds, NULL, &tv);
+       ret = select((int)sd + 1, NULL, &wfds, NULL, &tv);
        if (ret < 0)
        {
            SOCK_ERRNO;
@@ -452,15 +450,16 @@ channel_open(char *hostname, int port_in, int waittime, void (*close_cb)(void))
            sock_close(sd);
            return -1;
        }
-       if (!FD_ISSET(sd, &rfds) && !FD_ISSET(sd, &wfds))
+       if (!FD_ISSET(sd, &wfds))
        {
-           errno = ECONNREFUSED;
-           CHERROR("Cannot connect to port\n", "");
-           PERROR(_("E902: Cannot connect to port"));
+           /* don't give an error, we just timed out. */
            sock_close(sd);
            return -1;
        }
+    }
 
+    if (waittime >= 0)
+    {
 #ifdef _WIN32
        val = 0;
        ioctlsocket(sd, FIONBIO, &val);
index 2a56c0d2816d5419f375ddf4c743c76d88f76d90..8fcc73b41ebd701095d12e14c6f8e83a1697dd90 100644 (file)
@@ -177,3 +177,27 @@ func Test_server_crash()
   sleep 10m
   call s:kill_server()
 endfunc
+
+" Test that trying to connect to a non-existing port fails quickly.
+func Test_connect_waittime()
+  let start = reltime()
+  let handle = ch_open('localhost:9876')
+  if handle >= 0
+    " Oops, port does exists.
+    call ch_close(handle)
+  else
+    let elapsed = reltime(start)
+    call assert_true(elapsed < 1.0)
+  endif
+
+  let start = reltime()
+  let handle = ch_open('localhost:9867', {'waittime': 2000})
+  if handle >= 0
+    " Oops, port does exists.
+    call ch_close(handle)
+  else
+    " Failed connection doesn't wait the full time.
+    let elapsed = reltime(start)
+    call assert_true(elapsed < 1.0)
+  endif
+endfunc
index ea0c77055897d507c08fc493e31d34acb4b7a6a4..6634f60861a94b8098a1867b58c0c05ca7dcfebd 100644 (file)
@@ -747,6 +747,8 @@ static char *(features[]) =
 
 static int included_patches[] =
 {   /* Add new patch number below this line */
+/**/
+    1286,
 /**/
     1285,
 /**/