Skip to content

unix: check watcher queue in uv_backend_timeout()#1568

Closed
c0nk wants to merge 1 commit into
libuv:v1.xfrom
c0nk:pr-backend-timeout
Closed

unix: check watcher queue in uv_backend_timeout()#1568
c0nk wants to merge 1 commit into
libuv:v1.xfrom
c0nk:pr-backend-timeout

Conversation

@c0nk

@c0nk c0nk commented Sep 27, 2017

Copy link
Copy Markdown

Return 0 in uv_backend_timeout() if the watcher queue is not empty.

See issue #1565.

Return 0 in uv_backend_timeout() if the watcher queue is not empty.
@bnoordhuis

Copy link
Copy Markdown
Member

I've read #1565 but I don't understand why this is necessary.

loop->watcher_queue are the file descriptors to add before the next call to epoll_wait() or kevent().

Why should uv_backend_timeout() return 0 when file descriptors are added? It seems unrelated.

@c0nk

c0nk commented Sep 30, 2017

Copy link
Copy Markdown
Author

When new file descriptors will be added you need to call uv_run again without blocking. Otherwise you can't get notified of new events (through uv_backend_fd). For example, consider the following main loop:

while (is_running) {
    uv_run(loop, UV_RUN_NOWAIT);
    int timeout = uv_backend_timeout(loop);
    // Run main event loop with timeout as max timeout.
}

Consider what happens if you call uv_read_start() from within a uv callback. Now you really need to call uv_run() again immediately (hence why you should return 0 from uv_backend_timeout).

@bnoordhuis

Copy link
Copy Markdown
Member

Sorry for the delay, the notification got buried in my inbox.

Okay, I see what you mean now but this change as it stands would busy-loop on Solaris/Illumos because of these three lines.

I don't care much about that platform but if we ever added a select(2) back-end, it would probably have the same issue, so I think we need to address this.

What might work is to claim a bit in uv_loop_t.flags and set it in uv__io_start() and uv__io_stop() when a watcher is inserted into loop->watcher_queue. If the bit is set, uv_backend_timeout() returns zero.

It's not completely watertight because all watchers might get stopped again before uv_backend_timeout() is called. In that case the bit should be cleared again but it might be difficult to implement that without also implementing some kind of reference counting.

@bnoordhuis

Copy link
Copy Markdown
Member

This PR hasn't seen action in almost two years so I'll take the liberty of closing it out.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants