Skip to content
This repository was archived by the owner on Sep 15, 2026. It is now read-only.
This repository was archived by the owner on Sep 15, 2026. It is now read-only.

IO#wait doesn't return event type #13

Closed
ruby/ruby
#6036

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

Activity

  1. eregon commented on Mar 29, 2022

    @eregon
    Member

    That seems a bug, because the documentation says:

      io.wait(events, timeout) -> event mask or false.
      io.wait(timeout = nil, mode = :read) -> event mask or false.
    

    So it should always return an event mask or false according to the docs, and indeed that would be more useful.
    cc @ioquatix

  2. ioquatix commented on Mar 29, 2022

    @ioquatix
    Member

    The form io.wait(events, timeout) -> event mask or false. was introduced by me because the original form io.wait(timeout = nil, mode = :read) -> event mask or false. was practically useless and didn't match the C interface which already existed. To be honest, I don't think I changed the original one much, but you are right there is confusion over the interface of these methods. The C implementation returns the event mask. It's been a while since I checked io/wait implementation, but the interface introduced by me should be correct, and the old one might be different depending on the version, but in all cases it should be falsy if the wait failed and truthy if the wait succeeded, which is why I return event mask | false (rather than just event mask | 0) to try and have some level of compatibility. However, I'd have to check the actual implementation.

  3. ioquatix commented on Jun 25, 2022

    @ioquatix
    Member

    This is now fixed by #19

  4. larskanis commented on Jun 25, 2022

    @larskanis
    Author

    Thank you so much @ioquatix , this is exactly how I thought it should be implemented.

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

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