Skip to content

Allow IO#close to interrupt IO operations on fibers using fiber_interrupt hook. #12839

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

ioquatix
Copy link
Member

@ioquatix ioquatix commented Mar 3, 2025

Ruby's IO#close can cause IO#read, IO#write, IO#wait, IO#wait_readable and IO#wait_writable to be interrupted with an IOError (stream closed on another thread).

The fiber scheduler did not implement this for io_read, io_write and io_wait hooks. Finally after several years, someone made a bug report - see socketry/async#368 for background. In order to solve this problem for the fiber scheduler, we need to expose the ability for IO#close to interrupt a fiber.

This PR introduces a new internal C function rb_thread_io_interruptible_operation which takes an IO instance and a callback. During that callback, if the IO is closed, the callback will be interrupted.

It also introduces a new fiber scheduler method, fiber_interrupt which allows us to interrupt a specific fiber with an error, in this case with an IOError.

https://bugs.ruby-lang.org/issues/21166

This comment has been minimized.

@ioquatix ioquatix force-pushed the fiber-scheduler-fiber_interrupt-internal branch 5 times, most recently from ee34fe8 to 56d24c9 Compare March 3, 2025 08:37
@luke-gru
Copy link
Contributor

luke-gru commented Mar 3, 2025

For what it's worth this patch makes sense and LGTM, but I have no experience using the fiber scheduler and this needs another reviewer. But thank you for making the changes I suggested 😄

@ioquatix ioquatix changed the title Implement interruptible IO within the scheduler hook interface. Allow IO#close to interrupt IO operations on fibers using fiber_interrupt hook. Mar 10, 2025
@ioquatix ioquatix force-pushed the fiber-scheduler-fiber_interrupt-internal branch 3 times, most recently from 0e9790b to 4836882 Compare March 10, 2025 09:59
@ioquatix ioquatix force-pushed the fiber-scheduler-fiber_interrupt-internal branch from 4836882 to b69bafb Compare March 11, 2025 06:12
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

2 participants