Skip to content

Commit 24633aa

Browse files
authored
Return false from io_wait for timeouts. (#472)
1 parent fbb7c77 commit 24633aa

3 files changed

Lines changed: 19 additions & 2 deletions

File tree

‎lib/async/scheduler.rb‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -307,6 +307,7 @@ def address_resolve(hostname)
307307
# @parameter io [IO] The IO object to wait on.
308308
# @parameter events [Integer] The events to wait for, e.g. `IO::READABLE`, `IO::WRITABLE`, etc.
309309
# @parameter timeout [Float | Nil] The maximum time to wait, or if nil, indefinitely.
310+
# @returns [Integer | false] The subset of events that are ready, or `false` if the timeout expires.
310311
def io_wait(io, events, timeout = nil)
311312
fiber = Fiber.current
312313
expired = false
@@ -326,8 +327,8 @@ def io_wait(io, events, timeout = nil)
326327

327328
# A selector wait may return a falsy result when the fiber is resumed without the requested IO becoming ready. For example, a deferred unblock from a previous blocking operation may arrive after the fiber has moved on to this wait. Retry these stale or spurious wake-ups without resetting the original timer.
328329
until result = @selector.io_wait(fiber, io, events)
329-
# If the original timer resumed the fiber, the falsy result represents the timeout rather than a spurious wake-up:
330-
return nil if expired
330+
# If the original timer resumed the fiber, the false result represents the timeout rather than a spurious wake-up:
331+
return false if expired
331332
end
332333

333334
return result

‎releases.md‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
# Releases
22

3+
## Unreleased
4+
5+
- Fixed `Scheduler#io_wait` returning `nil` instead of `false` when an explicit timeout expired. Native callers such as `Socket#connect` with `connect_timeout:` distinguish a timeout by checking for `false`, so the `nil` caused `TypeError: no implicit conversion from nil to integer` instead of the intended `IO::TimeoutError`.
6+
37
## v2.45.0
48

59
- Fixed scheduler I/O and process waits returning prematurely after stale or interrupted wake-ups. I/O waits now preserve their original timeout, while blocking process waits retry and non-blocking `Process::WNOHANG` waits still return `nil`.

‎test/async/scheduler/io.rb‎

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,18 @@
2424
s2.close
2525
end
2626

27+
it "returns false from io_wait when an explicit timeout expires" do
28+
s1, s2 = Socket.pair :UNIX, :STREAM, 0
29+
30+
# Native callers (e.g. `wait_connectable` in `Socket#connect` with `connect_timeout:`) distinguish a timeout from a readiness mask by checking for `false`, matching the non-scheduler `rb_io_wait`:
31+
result = reactor.io_wait(s1, IO::READABLE, 0.001)
32+
33+
expect(result).to be == false
34+
ensure
35+
s1.close
36+
s2.close
37+
end
38+
2739
it "can read a single character" do
2840
s1, s2 = Socket.pair :UNIX, :STREAM, 0
2941

0 commit comments

Comments
 (0)