[ruby-core:109971] [Ruby master Feature#18411] Introduce `Fiber.blocking` for disabling scheduler.
From:
"ko1 (Koichi Sasada)" <noreply@...>
Date:
2022-09-21 07:12:00 UTC
List:
ruby-core #109971
Issue #18411 has been updated by ko1 (Koichi Sasada).
Let me confirm that why `write_nonblock` calls a fiber scheduler?
Or does it solve if `write_nonblock` doesn't call a fiber scheduler?
----------------------------------------
Feature #18411: Introduce `Fiber.blocking` for disabling scheduler.
https://bugs.ruby-lang.org/issues/18411#change-99221
* Author: ioquatix (Samuel Williams)
* Status: Open
* Priority: Normal
----------------------------------------
When implementing pure-ruby IO scheduler, we may need to invoke some Ruby IO operations without entering the scheduler.
```ruby
def io_write(fiber, io, buffer, length)
offset = 0
while length > 0
# From offset until the end:
chunk = buffer.to_str(offset, length)
case result = io.write_nonblock(chunk, exception: false)
when :wait_readable
self.io_wait(fiber, io, IO::READABLE)
when :wait_writable
self.io_wait(fiber, io, IO::WRITABLE)
else
offset += result
length -= result
end
end
return offset
end
```
There are some cases where even in this code `read_nonblock` can invoke fiber scheduler creating infinite recursion.
Therefore, I propose to introduce `Fiber.blocking{...}` which has almost identical implementation to `Fiber.new(blocking: true) {}.resume`.
In the above code, we change the line:
```
case result = io.write_nonblock(chunk, exception: false)
```
to
```
case result = Fiber.blocking{io.write_nonblock(chunk, exception: false)}
```
This ensures that `write_nonblock` can never enter the scheduler again.
--
https://bugs.ruby-lang.org/
Unsubscribe: <mailto:ruby-core-request@ruby-lang.org?subject=unsubscribe>
<http://lists.ruby-lang.org/cgi-bin/mailman/options/ruby-core>