# HG changeset patch # User Christian Brabandt # Date 1454877005 -3600 # Node ID 5c30ba57aaeaae33bf6313ed0b480ce77f9f4a44 # Parent a5f1bdd2007bb8095e97dcf7d124a3f9b8be7884 commit https://github.com/vim/vim/commit/7a84dbe6be0ef0e1ffbb7148cfe4ab50b9ba8f41 Author: Bram Moolenaar Date: Sun Feb 7 21:29:00 2016 +0100 patch 7.4.1286 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. diff --git a/src/channel.c b/src/channel.c --- a/src/channel.c +++ b/src/channel.c @@ -431,18 +431,16 @@ channel_open(char *hostname, int port_in } } - 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 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); diff --git a/src/testdir/test_channel.vim b/src/testdir/test_channel.vim --- a/src/testdir/test_channel.vim +++ b/src/testdir/test_channel.vim @@ -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 diff --git a/src/version.c b/src/version.c --- a/src/version.c +++ b/src/version.c @@ -748,6 +748,8 @@ static char *(features[]) = static int included_patches[] = { /* Add new patch number below this line */ /**/ + 1286, +/**/ 1285, /**/ 1284,