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

ruby-changes:47940

From: shirosaki <ko1@a...>
Date: Thu, 28 Sep 2017 22:43:27 +0900 (JST)
Subject: [ruby-changes:47940] shirosaki:r60055 (trunk): io.c: fix segfault with closing socket on Windows

shirosaki	2017-09-28 22:43:21 +0900 (Thu, 28 Sep 2017)

  New Revision: 60055

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

  Log:
    io.c: fix segfault with closing socket on Windows
    
    * io.c (fptr_finalize_flush): add an argument to keep GVL.
    * io.c (fptr_finalize): adjust for above change.
    * io.c (io_close_fptr): closing without GVL causes another
      exception while raising exception in another thread. This causes
      segfault on Windows. Keep GVL while closing when another thread
      raises.
      [Bug #13856] [ruby-core:82602]

  Modified files:
    trunk/io.c
Index: io.c
===================================================================
--- io.c	(revision 60054)
+++ io.c	(revision 60055)
@@ -4295,7 +4295,7 @@ static void free_io_buffer(rb_io_buffer_ https://github.com/ruby/ruby/blob/trunk/io.c#L4295
 static void clear_codeconv(rb_io_t *fptr);
 
 static void
-fptr_finalize_flush(rb_io_t *fptr, int noraise)
+fptr_finalize_flush(rb_io_t *fptr, int noraise, int keepgvl)
 {
     VALUE err = Qnil;
     int fd = fptr->fd;
@@ -4343,7 +4343,7 @@ fptr_finalize_flush(rb_io_t *fptr, int n https://github.com/ruby/ruby/blob/trunk/io.c#L4343
          * We assumes it is closed.  */
 
 	/**/
-	int keepgvl = !(mode & FMODE_WRITABLE);
+	keepgvl |= !(mode & FMODE_WRITABLE);
 	keepgvl |= noraise;
 	if ((maygvl_close(fd, keepgvl) < 0) && NIL_P(err))
 	    err = noraise ? Qtrue : INT2NUM(errno);
@@ -4360,7 +4360,7 @@ fptr_finalize_flush(rb_io_t *fptr, int n https://github.com/ruby/ruby/blob/trunk/io.c#L4360
 static void
 fptr_finalize(rb_io_t *fptr, int noraise)
 {
-    fptr_finalize_flush(fptr, noraise);
+    fptr_finalize_flush(fptr, noraise, FALSE);
     free_io_buffer(&fptr->rbuf);
     free_io_buffer(&fptr->wbuf);
     clear_codeconv(fptr);
@@ -4440,6 +4440,13 @@ rb_io_memsize(const rb_io_t *fptr) https://github.com/ruby/ruby/blob/trunk/io.c#L4440
     return size;
 }
 
+#ifdef _WIN32
+/* keep GVL while closing to prevent crash on Windows */
+# define KEEPGVL TRUE
+#else
+# define KEEPGVL FALSE
+#endif
+
 int rb_notify_fd_close(int fd);
 static rb_io_t *
 io_close_fptr(VALUE io)
@@ -4464,8 +4471,8 @@ io_close_fptr(VALUE io) https://github.com/ruby/ruby/blob/trunk/io.c#L4471
 
     fd = fptr->fd;
     busy = rb_notify_fd_close(fd);
-    fptr_finalize_flush(fptr, FALSE);
     if (busy) {
+	fptr_finalize_flush(fptr, FALSE, KEEPGVL);
 	do rb_thread_schedule(); while (rb_notify_fd_close(fd));
     }
     rb_io_fptr_cleanup(fptr, FALSE);

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

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