Skip to content

Commit 7a84dbe

Browse files
committed
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.
1 parent cb00f03 commit 7a84dbe

3 files changed

Lines changed: 34 additions & 9 deletions

File tree

src/channel.c

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -431,18 +431,16 @@ channel_open(char *hostname, int port_in, int waittime, void (*close_cb)(void))
431431
}
432432
}
433433

434-
if (waittime >= 0)
434+
if (waittime >= 0 && ret < 0)
435435
{
436436
struct timeval tv;
437-
fd_set rfds, wfds;
437+
fd_set wfds;
438438

439-
FD_ZERO(&rfds);
440439
FD_ZERO(&wfds);
441-
FD_SET(sd, &rfds);
442440
FD_SET(sd, &wfds);
443441
tv.tv_sec = waittime / 1000;
444442
tv.tv_usec = (waittime % 1000) * 1000;
445-
ret = select((int)sd+1, &rfds, &wfds, NULL, &tv);
443+
ret = select((int)sd + 1, NULL, &wfds, NULL, &tv);
446444
if (ret < 0)
447445
{
448446
SOCK_ERRNO;
@@ -452,15 +450,16 @@ channel_open(char *hostname, int port_in, int waittime, void (*close_cb)(void))
452450
sock_close(sd);
453451
return -1;
454452
}
455-
if (!FD_ISSET(sd, &rfds) && !FD_ISSET(sd, &wfds))
453+
if (!FD_ISSET(sd, &wfds))
456454
{
457-
errno = ECONNREFUSED;
458-
CHERROR("Cannot connect to port\n", "");
459-
PERROR(_("E902: Cannot connect to port"));
455+
/* don't give an error, we just timed out. */
460456
sock_close(sd);
461457
return -1;
462458
}
459+
}
463460

461+
if (waittime >= 0)
462+
{
464463
#ifdef _WIN32
465464
val = 0;
466465
ioctlsocket(sd, FIONBIO, &val);

src/testdir/test_channel.vim

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -177,3 +177,27 @@ func Test_server_crash()
177177
sleep 10m
178178
call s:kill_server()
179179
endfunc
180+
181+
" Test that trying to connect to a non-existing port fails quickly.
182+
func Test_connect_waittime()
183+
let start = reltime()
184+
let handle = ch_open('localhost:9876')
185+
if handle >= 0
186+
" Oops, port does exists.
187+
call ch_close(handle)
188+
else
189+
let elapsed = reltime(start)
190+
call assert_true(elapsed < 1.0)
191+
endif
192+
193+
let start = reltime()
194+
let handle = ch_open('localhost:9867', {'waittime': 2000})
195+
if handle >= 0
196+
" Oops, port does exists.
197+
call ch_close(handle)
198+
else
199+
" Failed connection doesn't wait the full time.
200+
let elapsed = reltime(start)
201+
call assert_true(elapsed < 1.0)
202+
endif
203+
endfunc

src/version.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -747,6 +747,8 @@ static char *(features[]) =
747747

748748
static int included_patches[] =
749749
{ /* Add new patch number below this line */
750+
/**/
751+
1286,
750752
/**/
751753
1285,
752754
/**/

0 commit comments

Comments
 (0)