8138819: (se) File descriptor leak when Selector.open fails
authorigerasim
Tue, 06 Oct 2015 12:00:38 +0300
changeset 32856 c654cdf1d685
parent 32855 452710dfb667
child 32877 b22f84a6b9d7
8138819: (se) File descriptor leak when Selector.open fails Reviewed-by: rriggs, alanb
jdk/src/java.base/linux/classes/sun/nio/ch/EPollSelectorImpl.java
jdk/src/java.base/macosx/classes/sun/nio/ch/KQueueSelectorImpl.java
jdk/src/java.base/solaris/classes/sun/nio/ch/DevPollSelectorImpl.java
jdk/src/java.base/unix/classes/sun/nio/ch/PollSelectorImpl.java
--- a/jdk/src/java.base/linux/classes/sun/nio/ch/EPollSelectorImpl.java	Tue Oct 06 09:12:00 2015 +0200
+++ b/jdk/src/java.base/linux/classes/sun/nio/ch/EPollSelectorImpl.java	Tue Oct 06 12:00:38 2015 +0300
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2005, 2013, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 2005, 2015, Oracle and/or its affiliates. All rights reserved.
  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  *
  * This code is free software; you can redistribute it and/or modify it
@@ -65,9 +65,23 @@
         long pipeFds = IOUtil.makePipe(false);
         fd0 = (int) (pipeFds >>> 32);
         fd1 = (int) pipeFds;
-        pollWrapper = new EPollArrayWrapper();
-        pollWrapper.initInterrupt(fd0, fd1);
-        fdToKey = new HashMap<>();
+        try {
+            pollWrapper = new EPollArrayWrapper();
+            pollWrapper.initInterrupt(fd0, fd1);
+            fdToKey = new HashMap<>();
+        } catch (Throwable t) {
+            try {
+                FileDispatcherImpl.closeIntFD(fd0);
+            } catch (IOException ioe0) {
+                t.addSuppressed(ioe0);
+            }
+            try {
+                FileDispatcherImpl.closeIntFD(fd1);
+            } catch (IOException ioe1) {
+                t.addSuppressed(ioe1);
+            }
+            throw t;
+        }
     }
 
     protected int doSelect(long timeout) throws IOException {
--- a/jdk/src/java.base/macosx/classes/sun/nio/ch/KQueueSelectorImpl.java	Tue Oct 06 09:12:00 2015 +0200
+++ b/jdk/src/java.base/macosx/classes/sun/nio/ch/KQueueSelectorImpl.java	Tue Oct 06 12:00:38 2015 +0300
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2011, 2013, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 2011, 2015, Oracle and/or its affiliates. All rights reserved.
  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  *
  * This code is free software; you can redistribute it and/or modify it
@@ -84,10 +84,24 @@
         long fds = IOUtil.makePipe(false);
         fd0 = (int)(fds >>> 32);
         fd1 = (int)fds;
-        kqueueWrapper = new KQueueArrayWrapper();
-        kqueueWrapper.initInterrupt(fd0, fd1);
-        fdMap = new HashMap<>();
-        totalChannels = 1;
+        try {
+            kqueueWrapper = new KQueueArrayWrapper();
+            kqueueWrapper.initInterrupt(fd0, fd1);
+            fdMap = new HashMap<>();
+            totalChannels = 1;
+        } catch (Throwable t) {
+            try {
+                FileDispatcherImpl.closeIntFD(fd0);
+            } catch (IOException ioe0) {
+                t.addSuppressed(ioe0);
+            }
+            try {
+                FileDispatcherImpl.closeIntFD(fd1);
+            } catch (IOException ioe1) {
+                t.addSuppressed(ioe1);
+            }
+            throw t;
+        }
     }
 
 
--- a/jdk/src/java.base/solaris/classes/sun/nio/ch/DevPollSelectorImpl.java	Tue Oct 06 09:12:00 2015 +0200
+++ b/jdk/src/java.base/solaris/classes/sun/nio/ch/DevPollSelectorImpl.java	Tue Oct 06 12:00:38 2015 +0300
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2001, 2013, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 2001, 2015, Oracle and/or its affiliates. All rights reserved.
  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  *
  * This code is free software; you can redistribute it and/or modify it
@@ -68,9 +68,23 @@
         long pipeFds = IOUtil.makePipe(false);
         fd0 = (int) (pipeFds >>> 32);
         fd1 = (int) pipeFds;
-        pollWrapper = new DevPollArrayWrapper();
-        pollWrapper.initInterrupt(fd0, fd1);
-        fdToKey = new HashMap<Integer,SelectionKeyImpl>();
+        try {
+            pollWrapper = new DevPollArrayWrapper();
+            pollWrapper.initInterrupt(fd0, fd1);
+            fdToKey = new HashMap<>();
+        } catch (Throwable t) {
+            try {
+                FileDispatcherImpl.closeIntFD(fd0);
+            } catch (IOException ioe0) {
+                t.addSuppressed(ioe0);
+            }
+            try {
+                FileDispatcherImpl.closeIntFD(fd1);
+            } catch (IOException ioe1) {
+                t.addSuppressed(ioe1);
+            }
+            throw t;
+        }
     }
 
     protected int doSelect(long timeout)
--- a/jdk/src/java.base/unix/classes/sun/nio/ch/PollSelectorImpl.java	Tue Oct 06 09:12:00 2015 +0200
+++ b/jdk/src/java.base/unix/classes/sun/nio/ch/PollSelectorImpl.java	Tue Oct 06 12:00:38 2015 +0300
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2001, 2010, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 2001, 2015, Oracle and/or its affiliates. All rights reserved.
  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  *
  * This code is free software; you can redistribute it and/or modify it
@@ -57,9 +57,23 @@
         long pipeFds = IOUtil.makePipe(false);
         fd0 = (int) (pipeFds >>> 32);
         fd1 = (int) pipeFds;
-        pollWrapper = new PollArrayWrapper(INIT_CAP);
-        pollWrapper.initInterrupt(fd0, fd1);
-        channelArray = new SelectionKeyImpl[INIT_CAP];
+        try {
+            pollWrapper = new PollArrayWrapper(INIT_CAP);
+            pollWrapper.initInterrupt(fd0, fd1);
+            channelArray = new SelectionKeyImpl[INIT_CAP];
+        } catch (Throwable t) {
+            try {
+                FileDispatcherImpl.closeIntFD(fd0);
+            } catch (IOException ioe0) {
+                t.addSuppressed(ioe0);
+            }
+            try {
+                FileDispatcherImpl.closeIntFD(fd1);
+            } catch (IOException ioe1) {
+                t.addSuppressed(ioe1);
+            }
+            throw t;
+        }
     }
 
     protected int doSelect(long timeout)