src/java.base/share/classes/sun/nio/ch/DatagramChannelImpl.java
changeset 48761 74c1fa26435a
parent 48750 ffbb784a8873
child 49001 ce06058197a4
--- a/src/java.base/share/classes/sun/nio/ch/DatagramChannelImpl.java	Thu Feb 08 11:44:21 2018 +0800
+++ b/src/java.base/share/classes/sun/nio/ch/DatagramChannelImpl.java	Thu Feb 08 10:55:21 2018 +0000
@@ -70,9 +70,6 @@
 
     // Our file descriptor
     private final FileDescriptor fd;
-
-    // fd value needed for dev/poll. This value will remain valid
-    // even after the value in the file descriptor object has been set to -1
     private final int fdVal;
 
     // The protocol family of the socket
@@ -124,7 +121,6 @@
 
     // -- End of fields protected by stateLock
 
-
     public DatagramChannelImpl(SelectorProvider sp)
         throws IOException
     {
@@ -159,16 +155,27 @@
                 throw new UnsupportedOperationException("IPv6 not available");
             }
         }
-        this.family = family;
-        this.fd = Net.socket(family, false);
-        this.fdVal = IOUtil.fdVal(fd);
-        this.state = ST_UNCONNECTED;
+
+        ResourceManager.beforeUdpCreate();
+        try {
+            this.family = family;
+            this.fd = Net.socket(family, false);
+            this.fdVal = IOUtil.fdVal(fd);
+            this.state = ST_UNCONNECTED;
+        } catch (IOException ioe) {
+            ResourceManager.afterUdpClose();
+            throw ioe;
+        }
     }
 
     public DatagramChannelImpl(SelectorProvider sp, FileDescriptor fd)
         throws IOException
     {
         super(sp);
+
+        // increment UDP count to match decrement when closing
+        ResourceManager.beforeUdpCreate();
+
         this.family = Net.isIPv6Available() ?
             StandardProtocolFamily.INET6 : StandardProtocolFamily.INET;
         this.fd = fd;
@@ -790,10 +797,9 @@
                     localAddress = Net.localAddress(fd);
 
                     // flush any packets already received.
-                    boolean blocking = false;
                     synchronized (blockingLock()) {
+                        boolean blocking = isBlocking();
                         try {
-                            blocking = isBlocking();
                             ByteBuffer tmpBuf = ByteBuffer.allocate(100);
                             if (blocking) {
                                 configureBlocking(false);