summaryrefslogtreecommitdiff
path: root/thread.c
diff options
context:
space:
mode:
authorKJ Tsanaktsidis <[email protected]>2024-09-13 17:40:30 +1000
committerKJ Tsanaktsidis <[email protected]>2024-09-17 10:11:44 +1000
commite08d5239b68ad61a731f4938cf963e37a5e88c25 (patch)
tree85960f18ac3f470a73cd2641ef1636ef00f7eda7 /thread.c
parent50d4840bd975a5e187c56dc4a56795cb6d02d658 (diff)
Ensure fiber scheduler is woken up when close interrupts read
If one thread is reading and another closes that socket, the close blocks waiting for the read to abort cleanly. This ensures that Ruby is totally done with the file descriptor _BEFORE_ we tell the OS to close and potentially re-use it. When the read is correctly terminated, the close should be unblocked. That currently works if closing is happening on a thread, but if it's happening on a fiber with a fiber scheduler, it does NOT work. This patch ensures that if the close happened in a fiber scheduled thread, that the scheduler is notified that the fiber is unblocked. [Bug #20723]
Notes
Notes: Merged: https://github.com/ruby/ruby/pull/11614
Diffstat (limited to 'thread.c')
-rw-r--r--thread.c8
1 files changed, 7 insertions, 1 deletions
diff --git a/thread.c b/thread.c
index 5a79200785..2a937ca278 100644
--- a/thread.c
+++ b/thread.c
@@ -1698,7 +1698,12 @@ thread_io_wake_pending_closer(struct waiting_fd *wfd)
RB_VM_LOCK_LEAVE();
if (has_waiter) {
- rb_thread_wakeup(wfd->busy->closing_thread);
+ rb_thread_t *th = rb_thread_ptr(wfd->busy->closing_thread);
+ if (th->scheduler != Qnil) {
+ rb_fiber_scheduler_unblock(th->scheduler, wfd->busy->closing_thread, wfd->busy->closing_fiber);
+ } else {
+ rb_thread_wakeup(wfd->busy->closing_thread);
+ }
rb_mutex_unlock(wfd->busy->wakeup_mutex);
}
}
@@ -2625,6 +2630,7 @@ rb_notify_fd_close(int fd, struct rb_io_close_wait_list *busy)
has_any = !ccan_list_empty(&busy->pending_fd_users);
busy->closing_thread = rb_thread_current();
+ busy->closing_fiber = rb_fiber_current();
wakeup_mutex = Qnil;
if (has_any) {
wakeup_mutex = rb_mutex_new();