joyent / libuv

Go to
https://github.com/libuv/libuv
3.27k stars 653 forks source link

uv_timer_t didn't wait for the timeout after gets() #1558

Closed imzyxwvu closed 10 years ago

imzyxwvu commented 10 years ago

I'm using libuv-0.11.29 on Windows.

The following program won't wait for timeout (3 seconds) and on_timer is called very soon if gets() blocked for more than 3 seconds.

#include <uv.h>

uv_timer_t timer;

static void on_timer(uv_timer_t *self)
{
    puts("timer ticked!");
    uv_close((uv_handle_t *)self, NULL);
}

int main()
{
    uv_loop_t *loop = uv_default_loop();
    char test[0x10000] = { 0 };

    gets(test); // This caused the problem.
    uv_timer_init(loop, &timer);
    uv_timer_start(&timer, on_timer, 3000, 0);
    puts("timer started!");
    uv_run(loop, UV_RUN_DEFAULT);

    return 0;
}
txdv commented 10 years ago

what happens if you put the uv_loop_default() behind gets?

imzyxwvu commented 10 years ago

@txdv It worked right. But if I need to get the user input when the loop is already got, must I use the TTY interface?

recp commented 10 years ago

The loop time has already initialized by uv_default_loop() before your program is waiting for an input. It seems if you will wait for an input about 3 seconds and you will be waited for the timeout as well. You can update the loop time after calling the gets, I think it should be working properly

#include <uv.h>

uv_timer_t timer;

static void on_timer(uv_timer_t *self)
{
  puts("timer ticked!");
  uv_close((uv_handle_t *)self, NULL);
}

int main()
{
  uv_loop_t *loop = uv_default_loop();
  char test[0x10000] = { 0 };

  gets(test); // This caused the problem.

  uv_update_time(loop); // <-------------------------

  uv_timer_init(loop, &timer);
  uv_timer_start(&timer, on_timer, 3000, 0);
  puts("timer started!");
  uv_run(loop, UV_RUN_DEFAULT);

  return 0;
}
imzyxwvu commented 10 years ago

Thank you.