Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions lib/async/scheduler.rb
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,22 @@ def blocking_operation_wait(work)

private_constant :BlockingOperationWait

# Implements `#io_close` if the selector supports it.
module IoClose
# Close the given file descriptor.
#
# @public Since *Async v2.47* and *Ruby v4.0*.
# @asynchronous May be non-blocking.
#
# @parameter descriptor [Integer] The fd to close.
# @returns [Boolean] Whether the selector closed the descriptor.
def io_close(descriptor)
!!@selector&.io_close(descriptor)

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[P2] Preserve descriptor ownership if close is interrupted

URing's io_close can yield while draining a full submission queue, before it has queued this descriptor's close. If the task is cancelled there, Ruby has already marked the IO closed, and the exception skips its fallback close, leaking the OS descriptor. A selector shim reproduces the ownership loss: closed? is true while the fd remains open. Can we ensure the close is accepted before cancellation can unwind this hook, and add a regression test for that interruption?

end
end

private_constant :IoClose

if ::IO::Event.const_defined?(:WorkerPool)
WorkerPool = ::IO::Event::WorkerPool
else
Expand Down Expand Up @@ -99,6 +115,10 @@ def initialize(parent = nil, selector: nil, profiler: Profiler&.default, worker_
if @worker_pool
self.singleton_class.prepend(BlockingOperationWait)
end

if @selector.respond_to?(:io_close)
self.singleton_class.prepend(IoClose)
end
end

# Compute the scheduler load according to the busy and idle times that are updated by the run loop.
Expand Down
30 changes: 30 additions & 0 deletions test/async/scheduler.rb
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,36 @@
end
end

with "#io_close" do
it "delegates to selectors that support io_close" do
selector = Object.new
calls = []
selector.define_singleton_method(:io_close) do |descriptor|
calls << descriptor
true
end
scheduler = Async::Scheduler.new(selector: selector)

expect(scheduler.io_close(10)).to be == true
expect(calls).to be == [10]
end

it "is not defined when the selector does not support io_close" do
scheduler = Async::Scheduler.new(selector: Object.new)

expect(scheduler).not.to be(:respond_to?, :io_close)
end

it "lets Ruby close the descriptor after the scheduler is closed" do
selector = IO::Event::Selector.new(Fiber.current)
selector.define_singleton_method(:io_close){|descriptor| true}
scheduler = Async::Scheduler.new(selector: selector)
scheduler.close

expect(scheduler.io_close(10)).to be == false
end
end

with "#cancel" do
it "shares the generated cancellation cause with child tasks" do
cause = Async::Cancel::Cause.new("Cancelling task!")
Expand Down
53 changes: 53 additions & 0 deletions test/io.rb
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,59 @@
end

with "#close" do
it "closes the descriptor" do
r, w = IO.pipe

w.close

expect(w).to be(:closed?)
# The reader only sees the end of the stream once the descriptor is actually closed:
expect(r.read).to be == ""
ensure
r&.close
end

it "closes the descriptor using the scheduler" do
skip_unless_minimum_ruby_version("4.0")
skip "The selector does not support io_close!" unless scheduler.respond_to?(:io_close)

r, w = IO.pipe
descriptor = w.fileno

closed = []
mock(scheduler) do |mock|
mock.before(:io_close){|descriptor| closed << descriptor}
end

w.close

expect(closed).to be(:include?, descriptor)
expect(w).to be(:closed?)
expect(r.read).to be == ""
ensure
r&.close
end

it "closes the descriptor while the scheduler is terminating" do
r, w = IO.pipe

Thread.new do
Sync do
# This task is still running when the scheduler exits, so the descriptor is closed during termination, just before the selector itself is closed:
Async(transient: true) do
sleep
ensure
w.close
end
end
end.join

expect(w).to be(:closed?)
expect(r.read).to be == ""
ensure
r&.close
end

it "can interrupt reading fiber when closing" do
skip_unless_minimum_ruby_version("3.5")

Expand Down
Loading