From 51d9b8add4ac9f533df604e1d194079312a98668 Mon Sep 17 00:00:00 2001
From: Eric Wong <normalperson@yhbt.net>
Date: Mon, 11 Apr 2011 16:39:19 +0000
Subject: [PATCH v2] io.c (rb_io_close): release GVL if possible

v2 of this change should fix issue #4558 and
be cleaner than the original proposed fixes.
---
 io.c |   68 ++++++++++++++++++++++++++++++++++++++++++++++++++++-------------
 1 files changed, 54 insertions(+), 14 deletions(-)

diff --git a/io.c b/io.c
index 46845eb..ef2f9a4 100644
--- a/io.c
+++ b/io.c
@@ -3490,10 +3490,52 @@ finish_writeconv_sync(VALUE arg)
     return finish_writeconv(p->fptr, p->noalloc);
 }
 
+static VALUE
+nogvl_close(void *ptr)
+{
+    int *fd = ptr;
+
+    return (VALUE)close(*fd);
+}
+
+static int
+maygvl_close(int fd, int keepgvl)
+{
+    if (keepgvl)
+	return close(fd);
+
+    /*
+     * close() may block for certain file types (NFS, SO_LINGER sockets,
+     * inotify), so let other threads run.
+     */
+    return (int)rb_thread_blocking_region(nogvl_close, &fd, RUBY_UBF_IO, 0);
+}
+
+static VALUE
+nogvl_fclose(void *ptr)
+{
+    FILE *file = ptr;
+
+    return (VALUE)fclose(file);
+}
+
+static int
+maygvl_fclose(FILE *file, int keepgvl)
+{
+    if (keepgvl)
+	return fclose(file);
+
+    return (int)rb_thread_blocking_region(nogvl_fclose, file, RUBY_UBF_IO, 0);
+}
+
+
 static void
 fptr_finalize(rb_io_t *fptr, int noraise)
 {
     VALUE err = Qnil;
+    int fd = fptr->fd;
+    FILE *stdio_file = fptr->stdio_file;
+
     if (fptr->writeconv) {
 	if (fptr->write_lock && !noraise) {
             struct finish_writeconv_arg arg;
@@ -3515,25 +3557,26 @@ fptr_finalize(rb_io_t *fptr, int noraise)
 		err = INT2NUM(errno);
 	}
     }
-    if (IS_PREP_STDIO(fptr) || fptr->fd <= 2) {
-        goto skip_fd_close;
+    fptr->fd = -1;
+    fptr->stdio_file = 0;
+    if (!noraise)
+	rb_thread_fd_close(fd);
+    if (IS_PREP_STDIO(fptr) || fd <= 2) {
+	/* need to keep FILE objects of stdin, stdout and stderr */
     }
-    if (fptr->stdio_file) {
-        /* fptr->stdio_file is deallocated anyway
+    else if (stdio_file) {
+        /* stdio_file is deallocated anyway
          * even if fclose failed.  */
-        if (fclose(fptr->stdio_file) < 0 && NIL_P(err))
+        if ((maygvl_fclose(stdio_file, noraise) < 0) && NIL_P(err))
             err = noraise ? Qtrue : INT2NUM(errno);
     }
-    else if (0 <= fptr->fd) {
+    else if (0 <= fd) {
         /* fptr->fd may be closed even if close fails.
          * POSIX doesn't specify it.
          * We assumes it is closed.  */
-        if (close(fptr->fd) < 0 && NIL_P(err))
+        if ((maygvl_close(fd, noraise) < 0) && NIL_P(err))
             err = noraise ? Qtrue : INT2NUM(errno);
     }
-  skip_fd_close:
-    fptr->fd = -1;
-    fptr->stdio_file = 0;
     fptr->mode &= ~(FMODE_READABLE|FMODE_WRITABLE);
 
     if (!NIL_P(err) && !noraise) {
@@ -3629,7 +3672,6 @@ VALUE
 rb_io_close(VALUE io)
 {
     rb_io_t *fptr;
-    int fd;
     VALUE write_io;
     rb_io_t *write_fptr;
 
@@ -3645,9 +3687,7 @@ rb_io_close(VALUE io)
     if (!fptr) return Qnil;
     if (fptr->fd < 0) return Qnil;
 
-    fd = fptr->fd;
     rb_io_fptr_cleanup(fptr, FALSE);
-    rb_thread_fd_close(fd);
 
     if (fptr->pid) {
 	rb_syswait(fptr->pid);
@@ -5785,7 +5825,7 @@ io_reopen(VALUE io, VALUE nfile)
 		rb_sys_fail_path(orig->pathv);
 	}
 	else {
-            fclose(fptr->stdio_file);
+            maygvl_fclose(fptr->stdio_file, 0);
             fptr->stdio_file = 0;
             fptr->fd = -1;
             if (dup2(fd2, fd) < 0)
