Fix IO#close
use-after-free in blocking_operation_wait
and fiber_interrupt
hooks.
#13437
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
This PR fixes memory safety and interrupt handling issues discovered while debugging
IO#close
across fibers.Fix interrupt handling affecting
rb_io_blocking_operation_exit
If
rb_io_blocking_operation_exit
executes with pending interrupts, user code that checks interrupts can exit unexpectedly. Instead, move the exception check into theEC_PUSH_TAG
block so that by the time we reachrb_io_blocking_operation_exit
the exception is already propagating and it behaves more likeensure
.Without this change, the following program can hang:
This is a follow up to #12839
Fix use-after-free in
blocking_operation_wait
hookIO#close
could trigger stack-allocated memory access after the function returned. The new implementation uses a heap-allocatedBlockingOperation
instance which also exposes a C interface for (safe) native work pool execution.https://bugs.ruby-lang.org/issues/21198
Technical Implementation
The blocking operation system now provides:
API Improvements
rb_fiber_scheduler_blocking_operation_extract
- Safely extract operation data while holding GVLrb_fiber_scheduler_blocking_operation_execute
- Execute in thread pools without GVL concernsrb_fiber_scheduler_blocking_operation_cancel
- Thread-safe cancellation supportRB_FIBER_SCHEDULER_BLOCKING_OPERATION_STATUS_*
enum valuesTesting
Enhanced tests covering: