Skip to content

select() emulation doesn't handle POLLHUP/POLLERR/POLLPRI correctly #35

Closed

Description

@bonzini

POLLHUP should set the file descriptor's bit in rfds, POLLERR should set it in wfds and rfds (but not xfds), POLLPRI should set it in xfds. As to POLLRDHUP, I guess it should set wfds since writes would always fail with EPIPE and thus not block... but Linux just ignores in its select() code which is a wrapper for poll() like OSv's.

Seriously... What an awesome project, and the C++ code is the nicest I've ever seen! Keep it up!

Activity

  1. ghost assigned on Sep 17, 2013
  2. nyh commented on Sep 18, 2013

    @nyh
    Contributor

    Hi Paulo, thanks for the compliments, and for the bug report.

    I'm looking into select() and pipe() now, to look into these edge cases (and add them to the unit tests, e.g., tests/tst-pipe.cc).

    What I'm wondering, shouldn't POLLHUP always be accompanied by a POLLIN? If poll returned POLLIN|POLLHUP instead of just POLLHUP, the current test in select() would already be fine. If poll returns POLLHUP only, our select() isn't correct. So I need to see what we did in OSv. As far as I know, this issue (whether to return both POLLIN and POLLHUP, or only one) is different in different Unix implementations.
    But you're right, we can write a select() wrapper that will work over poll() in any Unix-like implementation, and it should check for POLLIN|POLLHUP.

    It also seems our select()'s handling of exceptfds isn't accurate, to say the least (we don't even look at it when building the poll argument).

  3. bonzini commented on Sep 18, 2013

    @bonzini
    Author

    Yeah, I didn't really look that much into the pipe and socket code that produces POLLIN/POLLHUP. Always returning POLLIN|POLLHUP would be the sane thing to do, but since effectively anyone can build their own pollable file descriptor in OSv I think it's more robust to not assume that.

    Based on some quick testing, the pipe_buffer.cc methods have the following differences wrt Linux:

    int pipe_buffer::read_events_unlocked()
    {
        int ret = 0;
        ret |= !q.empty() ? POLLIN : 0;
        ret |= !sender ? POLLRDHUP : 0;
    

    Linux never returns POLLRDHUP for pipes (I'd get rid of it completely :) as it seems pretty obscure and you're using BSD networking code that doesn't have it). It returns POLLHUP if there is no sender. It can return POLLIN|POLLHUP so the logic is otherwise okay.

        return ret;
    }
    
    int pipe_buffer::write_events_unlocked()
    {
        if (!receiver) {
            return POLLHUP;
        }
    

    Linux here returns POLLOUT|POLLERR, matching the fact that the write will return EPIPE.

        int ret = 0;
        ret |= q.size() < max_buf ? POLLOUT : 0;
        return ret;
    }
    
  4. nyh commented on Sep 30, 2014

    @nyh
    Contributor

    Commits fixing most of the bugs reported in this patch:
    commit 554e80f
    commit 3511724
    commit f6d134b
    commit b53d39a

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

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions