Skip to content

Commit c8e33da

Browse files
Simpler approach.
1 parent a814ce9 commit c8e33da

2 files changed

Lines changed: 11 additions & 16 deletions

File tree

io.c

Lines changed: 9 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -5546,20 +5546,6 @@ fptr_finalize_flush(rb_io_t *fptr, int noraise, int keepgvl)
55465546
// Need to keep FILE objects of stdin, stdout and stderr, so we are done:
55475547
done = 1;
55485548
}
5549-
// The check for Qnil here is purely defensive, we should never invoke the fiber scheduler if there is no IO object.
5550-
else if (fptr->self != Qnil && fptr->closing_count == 0) {
5551-
VALUE scheduler = rb_fiber_scheduler_current();
5552-
if (scheduler != Qnil) {
5553-
fptr->closing_count += 1;
5554-
VALUE result = rb_fiber_scheduler_io_close(scheduler, fptr->self);
5555-
fptr->closing_count -= 1;
5556-
5557-
if (!UNDEF_P(result)) {
5558-
// Scheduler handled it:
5559-
if (fptr->fd == -1) return;
5560-
}
5561-
}
5562-
}
55635549

55645550
fptr->fd = -1;
55655551
fptr->stdio_file = 0;
@@ -5579,6 +5565,15 @@ fptr_finalize_flush(rb_io_t *fptr, int noraise, int keepgvl)
55795565
done = 1;
55805566
}
55815567

5568+
VALUE scheduler = rb_fiber_scheduler_current();
5569+
if (!done && fd >= 0 && scheduler != Qnil) {
5570+
VALUE result = rb_fiber_scheduler_io_close(scheduler, RB_INT2NUM(fd));
5571+
5572+
if (!UNDEF_P(result)) {
5573+
done = 1;
5574+
}
5575+
}
5576+
55825577
if (!done && fd >= 0) {
55835578
// fptr->fd may be closed even if close fails. POSIX doesn't specify it.
55845579
// We assumes it is closed.

test/fiber/scheduler.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -257,8 +257,8 @@ def io_select(...)
257257

258258
# This hook is invoked by `IO#close`. Using a separate IO object
259259
# demonstrates that the close operation is asynchronous.
260-
def io_close(io)
261-
io.close
260+
def io_close(descriptor)
261+
Fiber.blocking{IO.for_fd(descriptor.to_i).close}
262262
end
263263

264264
# This hook is invoked by `Kernel#sleep` and `Thread::Mutex#sleep`.

0 commit comments

Comments
 (0)