Skip to content

Commit

Permalink
Work around bugs in Ruby v3.2.5.
Browse files Browse the repository at this point in the history
  • Loading branch information
ioquatix committed Oct 3, 2024
1 parent 0fe638b commit 82c564e
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 0 deletions.
4 changes: 4 additions & 0 deletions lib/io/event/selector/select.rb
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,8 @@ def again?(errno)
if Support.fiber_scheduler_v3?
# Ruby 3.3+, full IO::Buffer support.

# Read from the given IO to the buffer.
#
# @parameter length [Integer] The minimum number of bytes to read.
# @parameter offset [Integer] The offset into the buffer to read to.
def io_read(fiber, io, buffer, length, offset = 0)
Expand Down Expand Up @@ -196,6 +198,8 @@ def io_read(fiber, io, buffer, length, offset = 0)
return total
end

# Write to the given IO from the buffer.
#
# @parameter length [Integer] The minimum number of bytes to write.
# @parameter offset [Integer] The offset into the buffer to write from.
def io_write(fiber, io, buffer, length, offset = 0)
Expand Down
9 changes: 9 additions & 0 deletions lib/io/event/support.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,23 @@ def self.buffer?
IO.const_defined?(:Buffer)
end

# To be removed on 31 Mar 2025.
def self.fiber_scheduler_v1?
IO.const_defined?(:Buffer) and !Fiber.respond_to?(:blocking)
end

# To be removed on 31 Mar 2026.
def self.fiber_scheduler_v2?
# Some interface changes were back-ported incorrectly:
# https://github.com/ruby/ruby/pull/10778
# Specifically "Improvements to IO::Buffer read/write/pread/pwrite."
# Missing correct size calculation.
return false if RUBY_VERSION >= "3.2.5"

IO.const_defined?(:Buffer) and Fiber.respond_to?(:blocking) and IO::Buffer.instance_method(:read).arity == -1
end

# To become the default 31 Mar 2026.
def self.fiber_scheduler_v3?
if fiber_scheduler_v2?
return true if RUBY_VERSION >= "3.3"
Expand Down
2 changes: 2 additions & 0 deletions test/io/event/selector/buffered_io.rb
Original file line number Diff line number Diff line change
Expand Up @@ -41,11 +41,13 @@
it "can read and write at the specified offset" do
writer = Fiber.new do
buffer = IO::Buffer.new(128)
# We can't write 128 bytes because there are only +64 bytes from offset 64.
expect(selector.io_write(Fiber.current, output, buffer, 128, 64)).to be == 64
end

reader = Fiber.new do
buffer = IO::Buffer.new(128)
# Only 64 bytes are available to read.
expect(selector.io_read(Fiber.current, input, buffer, 1, 64)).to be == 64
end

Expand Down

0 comments on commit 82c564e

Please sign in to comment.