Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 12 additions & 10 deletions io.c
Original file line number Diff line number Diff line change
Expand Up @@ -5551,18 +5551,9 @@ fptr_finalize_flush(rb_io_t *fptr, int noraise, int keepgvl)
fptr->stdio_file = 0;
fptr->mode &= ~(FMODE_READABLE|FMODE_WRITABLE);

// wait for blocking operations to ensure they do not hit EBADF:
// Wait for blocking operations to ensure they do not hit EBADF:
rb_thread_io_close_wait(fptr);

// Disable for now.
// if (!done && fd >= 0) {
// VALUE scheduler = rb_fiber_scheduler_current();
// if (scheduler != Qnil) {
// VALUE result = rb_fiber_scheduler_io_close(scheduler, fptr->self);
// if (!UNDEF_P(result)) done = 1;
// }
// }

if (!done && stdio_file) {
// stdio_file is deallocated anyway even if fclose failed.
if ((maygvl_fclose(stdio_file, noraise) < 0) && NIL_P(error)) {
Expand All @@ -5574,6 +5565,15 @@ fptr_finalize_flush(rb_io_t *fptr, int noraise, int keepgvl)
done = 1;
}

VALUE scheduler = rb_fiber_scheduler_current();
if (!done && fd >= 0 && scheduler != Qnil) {
VALUE result = rb_fiber_scheduler_io_close(scheduler, RB_INT2NUM(fd));

if (!UNDEF_P(result)) {
done = RTEST(result);
}
}

if (!done && fd >= 0) {
// fptr->fd may be closed even if close fails. POSIX doesn't specify it.
// We assumes it is closed.
Expand Down Expand Up @@ -5724,10 +5724,12 @@ io_close_fptr(VALUE io)
if (!fptr) return 0;
if (fptr->fd < 0) return 0;

// This guards against multiple threads closing the same IO object:
if (rb_thread_io_close_interrupt(fptr)) {
/* calls close(fptr->fd): */
fptr_finalize_flush(fptr, FALSE, KEEPGVL);
}

rb_io_fptr_cleanup(fptr, FALSE);
return fptr;
}
Expand Down
7 changes: 7 additions & 0 deletions test/fiber/scheduler.rb
Original file line number Diff line number Diff line change
Expand Up @@ -255,6 +255,13 @@ def io_select(...)
end.value
end

# This hook is invoked by `IO#close`. Using a separate IO object
# demonstrates that the close operation is asynchronous.
def io_close(descriptor)
Fiber.blocking{IO.for_fd(descriptor.to_i).close}
return true
end

# This hook is invoked by `Kernel#sleep` and `Thread::Mutex#sleep`.
def kernel_sleep(duration = nil)
# $stderr.puts [__method__, duration, Fiber.current].inspect
Expand Down
1 change: 1 addition & 0 deletions tool/sync_default_gems.rb
Original file line number Diff line number Diff line change
Expand Up @@ -272,6 +272,7 @@ def lib((upstream, branch), gemspec_in_subdir: false)
["ext/stringio", "ext/stringio"],
["test/stringio", "test/stringio"],
["stringio.gemspec", "ext/stringio/stringio.gemspec"],
["doc/stringio", "doc/stringio"],
], exclude: [
"ext/stringio/README.md",
]),
Expand Down