Skip to content

IO#wait doesn't return event type #13

@larskanis

Description

@larskanis

I'm uncertain if this issue might be discussed on the ruby bug tracker due, now that io-wait could be integrated into core.

Currently io.wait(:read_write) returns nil, false or the io object. Therefore it is not distinguishable if a read or write event was received. But this information is necessary in most cases, where IO#wait_readable or IO#wait_writable aren't suitable. On the C level both the old rb_wait_for_single_fd as well as the new rb_io_wait provide this information, but it is not passed to the Ruby level.

Returning the io object makes not much sense to me. It can not be used to chain calls together, since IO#wait can also return true. And it makes sense for IO.select but not for IO#wait where there's only a single IO object.

IMHO the return value should be changed to return an Integer event mask (out of IO::WRITABLE, IO::READABLE, IO::PRIORITY). The IO#wait method was declared as experimental in 2016. Not sure if this is still true. I doubt that there is any use of the returned IO in the wild.

Alternatively another method or method parameter could be added to enable returning the event mask.

A workaround is to use the scheduler or IO.select like so:

if Fiber.scheduler
  eventmask = Fiber.scheduler.io_wait(io, IO::WRITABLE | IO::READABLE, nil)
else
  rd, wr = IO.select([io], [io])
  eventmask = IO::READABLE if rd.any?
  eventmask |= IO::WRITABLE if wr.any?
end

Metadata

Metadata

Assignees

No one assigned

    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