[前][次][番号順一覧][スレッド一覧]

ruby-changes:46023

From: usa <ko1@a...>
Date: Sun, 26 Mar 2017 01:03:33 +0900 (JST)
Subject: [ruby-changes:46023] usa:r58094 (ruby_2_2): merge revision(s) 57422: [Backport #13158]

usa	2017-03-26 01:03:27 +0900 (Sun, 26 Mar 2017)

  New Revision: 58094

  https://svn.ruby-lang.org/cgi-bin/viewvc.cgi?view=revision&revision=58094

  Log:
    merge revision(s) 57422: [Backport #13158]
    
    io.c: close before wait
    
    * io.c (io_close_fptr): notify then close, and wait for other
      threads before free fptr.  [ruby-core:79262] [Bug #13158]

  Modified directories:
    branches/ruby_2_2/
  Modified files:
    branches/ruby_2_2/io.c
    branches/ruby_2_2/test/ruby/test_io.rb
    branches/ruby_2_2/thread.c
    branches/ruby_2_2/version.h
Index: ruby_2_2/io.c
===================================================================
--- ruby_2_2/io.c	(revision 58093)
+++ ruby_2_2/io.c	(revision 58094)
@@ -4263,7 +4263,7 @@ static void free_io_buffer(rb_io_buffer_ https://github.com/ruby/ruby/blob/trunk/ruby_2_2/io.c#L4263
 static void clear_codeconv(rb_io_t *fptr);
 
 static void
-fptr_finalize(rb_io_t *fptr, int noraise)
+fptr_finalize_flush(rb_io_t *fptr, int noraise)
 {
     VALUE err = Qnil;
     int fd = fptr->fd;
@@ -4323,6 +4323,12 @@ fptr_finalize(rb_io_t *fptr, int noraise https://github.com/ruby/ruby/blob/trunk/ruby_2_2/io.c#L4323
             rb_exc_raise(err);
         }
     }
+}
+
+static void
+fptr_finalize(rb_io_t *fptr, int noraise)
+{
+    fptr_finalize_flush(fptr, noraise);
     free_io_buffer(&fptr->rbuf);
     free_io_buffer(&fptr->wbuf);
     clear_codeconv(fptr);
@@ -4402,6 +4408,7 @@ rb_io_memsize(const rb_io_t *fptr) https://github.com/ruby/ruby/blob/trunk/ruby_2_2/io.c#L4408
     return size;
 }
 
+int rb_notify_fd_close(int fd);
 VALUE
 rb_io_close(VALUE io)
 {
@@ -4409,6 +4416,7 @@ rb_io_close(VALUE io) https://github.com/ruby/ruby/blob/trunk/ruby_2_2/io.c#L4416
     int fd;
     VALUE write_io;
     rb_io_t *write_fptr;
+    int busy;
 
     write_io = GetWriteIO(io);
     if (io != write_io) {
@@ -4423,7 +4431,11 @@ rb_io_close(VALUE io) https://github.com/ruby/ruby/blob/trunk/ruby_2_2/io.c#L4431
     if (fptr->fd < 0) return Qnil;
 
     fd = fptr->fd;
-    rb_thread_fd_close(fd);
+    busy = rb_notify_fd_close(fd);
+    fptr_finalize_flush(fptr, FALSE);
+    if (busy) {
+	do rb_thread_schedule(); while (rb_notify_fd_close(fd));
+    }
     rb_io_fptr_cleanup(fptr, FALSE);
 
     if (fptr->pid) {
@@ -6709,7 +6721,7 @@ io_reopen(VALUE io, VALUE nfile) https://github.com/ruby/ruby/blob/trunk/ruby_2_2/io.c#L6721
             rb_update_max_fd(fd);
             fptr->fd = fd;
 	}
-	rb_thread_fd_close(fd);
+	rb_notify_fd_close(fd);
 	if ((orig->mode & FMODE_READABLE) && pos >= 0) {
 	    if (io_seek(fptr, pos, SEEK_SET) < 0 && errno) {
 		rb_sys_fail_path(fptr->pathv);
Index: ruby_2_2/version.h
===================================================================
--- ruby_2_2/version.h	(revision 58093)
+++ ruby_2_2/version.h	(revision 58094)
@@ -1,6 +1,6 @@ https://github.com/ruby/ruby/blob/trunk/ruby_2_2/version.h#L1
 #define RUBY_VERSION "2.2.7"
 #define RUBY_RELEASE_DATE "2017-03-26"
-#define RUBY_PATCHLEVEL 426
+#define RUBY_PATCHLEVEL 427
 
 #define RUBY_RELEASE_YEAR 2017
 #define RUBY_RELEASE_MONTH 3
Index: ruby_2_2/test/ruby/test_io.rb
===================================================================
--- ruby_2_2/test/ruby/test_io.rb	(revision 58093)
+++ ruby_2_2/test/ruby/test_io.rb	(revision 58094)
@@ -3207,4 +3207,25 @@ End https://github.com/ruby/ruby/blob/trunk/ruby_2_2/test/ruby/test_io.rb#L3207
       end
     end;
   end
+
+  def test_race_closed_stream
+    bug13158 = '[ruby-core:79262] [Bug #13158]'
+    closed = nil
+    IO.pipe do |r, w|
+      thread = Thread.new do
+        begin
+          while r.gets
+          end
+        ensure
+          closed = r.closed?
+        end
+      end
+      sleep 0.01
+      r.close
+      assert_raise_with_message(IOError, /stream closed/) do
+        thread.join
+      end
+      assert_equal(true, closed, "#{bug13158}: stream should be closed")
+    end
+  end
 end
Index: ruby_2_2/thread.c
===================================================================
--- ruby_2_2/thread.c	(revision 58093)
+++ ruby_2_2/thread.c	(revision 58094)
@@ -2123,14 +2123,13 @@ rb_threadptr_reset_raised(rb_thread_t *t https://github.com/ruby/ruby/blob/trunk/ruby_2_2/thread.c#L2123
     return 1;
 }
 
-void
-rb_thread_fd_close(int fd)
+int
+rb_notify_fd_close(int fd)
 {
     rb_vm_t *vm = GET_THREAD()->vm;
     rb_thread_t *th = 0;
     int busy;
 
-  retry:
     busy = 0;
     list_for_each(&vm->living_threads, th, vmlt_node) {
 	if (th->waiting_fd == fd) {
@@ -2140,10 +2139,7 @@ rb_thread_fd_close(int fd) https://github.com/ruby/ruby/blob/trunk/ruby_2_2/thread.c#L2139
 	    busy = 1;
 	}
     }
-    if (busy) {
-	rb_thread_schedule_limits(0);
-	goto retry;
-    }
+    return busy;
 }
 
 /*

Property changes on: ruby_2_2
___________________________________________________________________
Modified: svn:mergeinfo
   Merged /trunk:r57422


--
ML: ruby-changes@q...
Info: http://www.atdot.net/~ko1/quickml/

[前][次][番号順一覧][スレッド一覧]