> https://bugs.ruby-lang.org/issues/14907

> I only found one minor regression from this change in
> test/lib/test/unit.rb as IO.new does not set close-on-exec
> when using jobserver FDs from make.  A possible change
> is to make IO.new set FD_CLOEXEC by default.

Maybe this is a good complimentary change:

```
diff --git a/ext/socket/basicsocket.c b/ext/socket/basicsocket.c
index 2641b4410b..0418c022cf 100644
--- a/ext/socket/basicsocket.c
+++ b/ext/socket/basicsocket.c
@@ -28,6 +28,7 @@ bsock_s_for_fd(VALUE klass, VALUE fd)
     VALUE sock = rsock_init_sock(rb_obj_alloc(klass), NUM2INT(fd));
 
     GetOpenFile(sock, fptr);
+    rb_maygvl_fd_fix_cloexec(fptr->fd);
 
     return sock;
 }
diff --git a/io.c b/io.c
index b041acdef0..ddebd09e65 100644
--- a/io.c
+++ b/io.c
@@ -8085,6 +8085,7 @@ rb_io_initialize(int argc, VALUE *argv, VALUE io)
 #else
     if (fstat(fd, &st) == -1) rb_sys_fail(0);
 #endif
+    rb_maygvl_fd_fix_cloexec(fd);
     rb_update_max_fd(fd);
 #if defined(HAVE_FCNTL) && defined(F_GETFL)
     ofmode = rb_io_oflags_fmode(oflags);
diff --git a/test/ruby/test_io.rb b/test/ruby/test_io.rb
index a6a8e78209..97ea857c3e 100644
--- a/test/ruby/test_io.rb
+++ b/test/ruby/test_io.rb
@@ -3128,6 +3128,15 @@ def test_cloexec
     }
   end
 
+  def test_for_fd_close_on_exec
+    IO.pipe do |r, w|
+      w.close_on_exec = false
+      w2 = IO.new(w.fileno, autoclose: false)
+      assert_predicate w, :close_on_exec?
+      assert_predicate w2, :close_on_exec?
+    end
+  end if have_close_on_exec?
+
   def test_ioctl_linux
     # Alpha, mips, sparc and ppc have an another ioctl request number scheme.
     # So, hardcoded 0x80045200 may fail.
diff --git a/test/socket/test_basicsocket.rb b/test/socket/test_basicsocket.rb
index d388b4f0dd..6ea8bcfdf3 100644
--- a/test/socket/test_basicsocket.rb
+++ b/test/socket/test_basicsocket.rb
@@ -150,6 +150,13 @@ def test_for_fd
       s = BasicSocket.for_fd(sock.fileno)
       assert_instance_of BasicSocket, s
       s.autoclose = false
+
+      sock.close_on_exec = false
+      s2 = BasicSocket.for_fd(sock.fileno)
+      s2.autoclose = false
+      assert_predicate s2, :close_on_exec?
+      assert_predicate sock, :close_on_exec?
+
       sock.close
     end
   end
```

Unsubscribe: <mailto:ruby-core-request / ruby-lang.org?subject=unsubscribe>
<http://lists.ruby-lang.org/cgi-bin/mailman/options/ruby-core>