Skip to content

Commit 83bd43e

Browse files
committed
Return false from io_wait() for timeouts
Callers of `rb_io_wait()` expect a `false` return value for timeouts, not nil. This could lead to very strange errors from code like TCPSocket.new("192.0.2.1", 80, connect_timeout: 1) which would raise TypeError: no implicit conversion from nil to integer It now raises the correct IO::TimeoutError: user specified timeout for 192.0.2.1:80
1 parent fbb7c77 commit 83bd43e

3 files changed

Lines changed: 18 additions & 2 deletions

File tree

‎lib/async/scheduler.rb‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -326,8 +326,8 @@ def io_wait(io, events, timeout = nil)
326326

327327
# 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.
328328
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
329+
# If the original timer resumed the fiber, the false result represents the timeout rather than a spurious wake-up:
330+
return false if expired
331331
end
332332

333333
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)