Since rb_sysopen releases the GVL, calling close(fptr->fd) would
leave a window where accessing the file from another thread can hit
IOError on a closed stream

Instead, create a new, temporary FD via rb_sysopen and call
rb_cloexec_dup2 on it to atomically replace the file fptr->fd points
to. This leaves no possible window where fptr->fd is invalid to
userspace (even for any threads running w/o GVL).

Without this patch, I need to maintain separate code paths for
atomically reopening (e.g. log rotation) std{in,out,err} vs
other files in a multithreaded application.

Associated revisions

io.c (rb_io_reopen): create a new, temporary FD via rb_sysopen and
call rb_cloexec_dup2 on it to atomically replace the file fptr->fd
points to. This leaves no possible window where fptr->fd is invalid
to userspace (even for any threads running w/o GVL). based on the
patch by Eric Wong normalperson@yhbt.net at .
[Bug #9036]

io.c (rb_io_reopen): create a new, temporary FD via rb_sysopen and
call rb_cloexec_dup2 on it to atomically replace the file fptr->fd
points to. This leaves no possible window where fptr->fd is invalid
to userspace (even for any threads running w/o GVL). based on the
patch by Eric Wong normalperson@yhbt.net at .
[Bug #9036]

* io.c (rb_io_reopen): create a new, temporary FD via rb_sysopen and
call rb_cloexec_dup2 on it to atomically replace the file fptr->fd
points to. This leaves no possible window where fptr->fd is invalid
to userspace (even for any threads running w/o GVL). based on the
patch by Eric Wong <normalperson@yhbt.net> at .
[Bug #9036]

This issue was solved with changeset r43373.
Eric, thank you for reporting this issue.
Your contribution to Ruby is greatly appreciated.
May Ruby be with you.

io.c: make IO#reopen("pathname") atomic

io.c (rb_io_reopen): create a new, temporary FD via rb_sysopen and
call rb_cloexec_dup2 on it to atomically replace the file fptr->fd
points to. This leaves no possible window where fptr->fd is invalid
to userspace (even for any threads running w/o GVL). based on the
patch by Eric Wong normalperson@yhbt.net at .
[Bug #9036]