Permalink
Browse files

fix: read timeout doesn't work well in Windows

Windows does not ensure that Sleep(1) waits just 1 milliseconds.
In some cases it waits approximately 10 milliseconds.

So we must check elapsed milliseconds each time.
  • Loading branch information...
1 parent b2255c6 commit 064aa32158bd0261b8deb06188cc99bee792f824 @wonderful-panda wonderful-panda committed Jul 29, 2016
Showing with 3 additions and 1 deletion.
  1. +3 −1 src/proc_w32.c
View
@@ -659,6 +659,7 @@ vp_pipe_read(char *args)
char *eof;
unsigned int size = 0;
HANDLE hPipe;
+ DWORD tcstart;
VP_RETURN_IF_FAIL(vp_stack_from_args(&stack, args));
VP_RETURN_IF_FAIL(vp_stack_pop_num(&stack, "%d", &fd));
@@ -678,6 +679,7 @@ vp_pipe_read(char *args)
buf += VP_HEADER_SIZE;
hPipe = (HANDLE)_get_osfhandle(fd);
+ tcstart = GetTickCount();
while (cnt > 0) {
if (!PeekNamedPipe(hPipe, NULL, 0, NULL, &n, NULL)) {
/* can be ERROR_HANDLE_EOF? */
@@ -692,7 +694,7 @@ vp_pipe_read(char *args)
return vp_stack_return_error(&_result, "PeekNamedPipe() error: %08X %s",
err, lasterror());
} else if (n == 0) {
- if (timeout-- <= 0) {
+ if (GetTickCount() - tcstart >= timeout) {
break;
}
Sleep(1);

0 comments on commit 064aa32

Please sign in to comment.