In libuv v1.x (963ecc82) in Windows 10, I noticed that 'uv_read_start'
over a 'uv_tty_t' calls the 'uv_alloc_cb' before it returns. The same
does not seem to happen in Linux, nor even in Windows if we use other
'uv_tcp_t' ir 'uv_pipe_t'. Is this expected?

It was a problem for me because my code only sets up the environment
for the callback if 'uv_read_start' returns successfully (err >= 0).
Also, from the way the general design of the event loop of libuv is
described (http://docs.libuv.org/en/v1.x/design.html), I was surprised
that callbacks can be invoked inside an API call.

I attached a small example to reproduce this scenario. On Linux, it
works as expected:

```
libuv_ttyinlineread.c:38:main(server started!)
I type this line
libuv_ttyinlineread.c:14:on_alloc(size=65536)
libuv_ttyinlineread.c:9:on_read(nread=17)
```

But on Windows 10, I get this:

```
libuv_ttyinlineread.c:14:on_alloc(size=8192)
Assertion failed: handle->data, file libuv_ttyinlineread.c, line 15
```

Thanks in advance.

-- 
Renato Maia

-- 
You received this message because you are subscribed to the Google Groups 
"libuv" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to libuv+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/libuv/CAB-PuDqxTkLpXMwh52Z7Z%3DuCMTceY2S9OpYk8SjZecvV1KzizQ%40mail.gmail.com.
#include <assert.h>
#include <uv.h>

#define BUFFER_SIZE 1024
#define log(F,V) printf("%s:%d:%s("F")\n", __FILE__, __LINE__, __func__, V);

static void on_read (uv_stream_t *stream, ssize_t nread, const uv_buf_t *buffer)
{
        log("nread=%zd", nread);
}

static void on_alloc (uv_handle_t *handle, size_t suggested_size, uv_buf_t 
*buffer)
{
        log("size=%zd", suggested_size);
        assert(handle->data);
        buffer->base = handle->data;
        buffer->len = BUFFER_SIZE;
}

int main(int argc, char const *argv[])
{
        uv_loop_t loop;
        uv_tty_t stdin_tty;
        int err;
        char buffer[BUFFER_SIZE];

        err = uv_loop_init(&loop);
        assert(err >= 0);

        err = uv_tty_init(&loop, &stdin_tty, 0, 0);
        assert(err >= 0);

        stdin_tty.data = NULL;
        err = uv_read_start((uv_stream_t *)&stdin_tty, on_alloc, on_read);
        assert(err >= 0);
        stdin_tty.data = buffer;

        log("%s", "server started!");

        err = uv_run(&loop, UV_RUN_DEFAULT);
        assert(err >= 0);

        return 0;
}

Reply via email to