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

ruby-changes:52396

From: nagachika <ko1@a...>
Date: Sat, 1 Sep 2018 14:40:08 +0900 (JST)
Subject: [ruby-changes:52396] nagachika:r64605 (ruby_2_5): merge revision(s) 64007, 64019, 64020: [Backport #14929]

nagachika	2018-09-01 14:40:02 +0900 (Sat, 01 Sep 2018)

  New Revision: 64605

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

  Log:
    merge revision(s) 64007,64019,64020: [Backport #14929]
    
    thread.c (do_select): fix leak on exception
    
    When do_select is interrupted and raise happens from
    RUBY_VM_CHECK_INTS_BLOCKING, the original FD sets we copied
    do not get freed, leading to a memory leak.  Wrap up all the
    FD sets into a Ruby object to ensure the GC can release an
    allocations made for rb_fdset_t.
    
    This leak existed since Ruby 2.0.0 (r36430)
    
    [Bug #14929]
    
    increase timeout seconds.
    * test/ruby/test_io.rb (test_select_leak): increase timeout seconds
      to pass this test on a high-load machine.
    
    
    60 sec is not enough at all

  Modified directories:
    branches/ruby_2_5/
  Modified files:
    branches/ruby_2_5/test/ruby/test_io.rb
    branches/ruby_2_5/thread.c
    branches/ruby_2_5/version.h
Index: ruby_2_5/test/ruby/test_io.rb
===================================================================
--- ruby_2_5/test/ruby/test_io.rb	(revision 64604)
+++ ruby_2_5/test/ruby/test_io.rb	(revision 64605)
@@ -3742,4 +3742,21 @@ __END__ https://github.com/ruby/ruby/blob/trunk/ruby_2_5/test/ruby/test_io.rb#L3742
       con.close
     end
   end if Socket.const_defined?(:MSG_OOB)
+
+  def test_select_leak
+    assert_no_memory_leak([], <<-"end;", <<-"end;", rss: true, timeout: 60)
+      r, w = IO.pipe
+      rset = [r]
+      wset = [w]
+      Thread.new { IO.select(rset, wset, nil, 0) }.join
+    end;
+      20_000.times do
+        th = Thread.new { IO.select(rset, wset) }
+        Thread.pass until th.stop?
+        th.kill
+        th.join
+        GC.start
+      end
+    end;
+  end
 end
Index: ruby_2_5/thread.c
===================================================================
--- ruby_2_5/thread.c	(revision 64604)
+++ ruby_2_5/thread.c	(revision 64605)
@@ -3748,23 +3748,53 @@ update_timeval(struct timeval *timeout, https://github.com/ruby/ruby/blob/trunk/ruby_2_5/thread.c#L3748
     }
 }
 
+struct select_set {
+    rb_fdset_t read;
+    rb_fdset_t write;
+    rb_fdset_t except;
+};
+
+static size_t
+select_set_memsize(const void *p)
+{
+    return sizeof(struct select_set);
+}
+
+static void
+select_set_free(void *p)
+{
+    struct select_set *orig = p;
+
+    rb_fd_term(&orig->read);
+    rb_fd_term(&orig->write);
+    rb_fd_term(&orig->except);
+    xfree(orig);
+}
+
+static const rb_data_type_t select_set_type = {
+    "select_set",
+    {NULL, select_set_free, select_set_memsize,},
+    0, 0, RUBY_TYPED_FREE_IMMEDIATELY
+};
+
 static int
 do_select(int n, rb_fdset_t *const readfds, rb_fdset_t *const writefds,
 	  rb_fdset_t *const exceptfds, struct timeval *timeout)
 {
     int MAYBE_UNUSED(result);
     int lerrno;
-    rb_fdset_t MAYBE_UNUSED(orig_read);
-    rb_fdset_t MAYBE_UNUSED(orig_write);
-    rb_fdset_t MAYBE_UNUSED(orig_except);
     double limit = 0;
     struct timeval wait_rest;
     rb_thread_t *th = GET_THREAD();
+    VALUE o;
+    struct select_set *orig;
+
+    o = TypedData_Make_Struct(0, struct select_set, &select_set_type, orig);
 
 #define do_select_update() \
-    (restore_fdset(readfds, &orig_read), \
-     restore_fdset(writefds, &orig_write), \
-     restore_fdset(exceptfds, &orig_except), \
+    (restore_fdset(readfds, &orig->read), \
+     restore_fdset(writefds, &orig->write), \
+     restore_fdset(exceptfds, &orig->except), \
      update_timeval(timeout, limit), \
      TRUE)
 
@@ -3776,7 +3806,7 @@ do_select(int n, rb_fdset_t *const readf https://github.com/ruby/ruby/blob/trunk/ruby_2_5/thread.c#L3806
     }
 
 #define fd_init_copy(f) \
-    (f##fds) ? rb_fd_init_copy(&orig_##f, f##fds) : rb_fd_no_init(&orig_##f)
+    (f##fds) ? rb_fd_init_copy(&orig->f, f##fds) : rb_fd_no_init(&orig->f)
     fd_init_copy(read);
     fd_init_copy(write);
     fd_init_copy(except);
@@ -3791,14 +3821,13 @@ do_select(int n, rb_fdset_t *const readf https://github.com/ruby/ruby/blob/trunk/ruby_2_5/thread.c#L3821
 	    if (result < 0) lerrno = errno;
 	}, ubf_select, th, FALSE);
 
-	RUBY_VM_CHECK_INTS_BLOCKING(th->ec);
+	RUBY_VM_CHECK_INTS_BLOCKING(th->ec); /* may raise */
     } while (result < 0 && retryable(errno = lerrno) && do_select_update());
 
-#define fd_term(f) if (f##fds) rb_fd_term(&orig_##f)
-    fd_term(read);
-    fd_term(write);
-    fd_term(except);
-#undef fd_term
+    /* didn't raise, perform cleanup ourselves */
+    select_set_free(orig);
+    rb_gc_force_recycle(o);
+
 
     return result;
 }
Index: ruby_2_5/version.h
===================================================================
--- ruby_2_5/version.h	(revision 64604)
+++ ruby_2_5/version.h	(revision 64605)
@@ -1,6 +1,6 @@ https://github.com/ruby/ruby/blob/trunk/ruby_2_5/version.h#L1
 #define RUBY_VERSION "2.5.2"
 #define RUBY_RELEASE_DATE "2018-09-01"
-#define RUBY_PATCHLEVEL 84
+#define RUBY_PATCHLEVEL 85
 
 #define RUBY_RELEASE_YEAR 2018
 #define RUBY_RELEASE_MONTH 9
Index: ruby_2_5
===================================================================
--- ruby_2_5	(revision 64604)
+++ ruby_2_5	(revision 64605)

Property changes on: ruby_2_5
___________________________________________________________________
Modified: svn:mergeinfo
## -0,0 +0,1 ##
   Merged /trunk:r64007,64019-64020

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

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