Bug #9036

[PATCH] io.c: make IO#reopen("pathname") atomic for non-stdio

Added by Eric Wong 6 months ago. Updated 6 months ago.

[ruby-core:57943]
Status:Closed
Priority:Normal
Assignee:-
Category:-
Target version:-
ruby -v:ruby 2.1.0dev (2013-10-20 trunk 43369) [x86_64-linux] Backport:1.9.3: UNKNOWN, 2.0.0: UNKNOWN

Description

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 rbsysopen 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.

0001-io.c-make-IO-reopen-pathname-atomic.patch Magnifier - [PATCH] io.c: make IO#reopen("pathname") atomic (1.47 KB) Eric Wong, 10/20/2013 01:16 PM

Associated revisions

Revision 43373
Added by Nobuyoshi Nakada 6 months ago

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

  • io.c (rbioreopen): create a new, temporary FD via rbsysopen and call rbcloexec_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]

History

#1 Updated by Eric Wong 6 months ago

"normalperson (Eric Wong)" normalperson@yhbt.net wrote:

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.

Separate code paths like this:
http://bogomips.org/yahns.git/patch/?id=bcb10abe5
(review greatly appreciated!, thanks!)

#2 Updated by Nobuyoshi Nakada 6 months ago

  • Status changed from Open to Closed
  • % Done changed from 0 to 100

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 (rbioreopen): create a new, temporary FD via rbsysopen and call rbcloexec_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]

Also available in: Atom PDF