From e472f6ac1c4e271f5a8bd60816110c3940220cf8 Mon Sep 17 00:00:00 2001 From: damencho Date: Wed, 10 May 2017 15:06:15 -0500 Subject: [PATCH 1/2] Fixes leak of file descriptors by not closing DatagramSockets. The problem was reproduced with java7 and defining multiple ipv6 fake(not-operational) addresses to one of the interfaces and running for a while. Fixes jitsi/jitsi-meet#1390 and fixes jitsi/jicofo#167. --- .../java/org/ice4j/socket/DelegatingDatagramSocket.java | 6 +++--- .../java/org/ice4j/socket/MultiplexedDatagramSocket.java | 2 ++ .../org/ice4j/socket/RelayedCandidateDatagramSocket.java | 2 ++ 3 files changed, 7 insertions(+), 3 deletions(-) diff --git a/src/main/java/org/ice4j/socket/DelegatingDatagramSocket.java b/src/main/java/org/ice4j/socket/DelegatingDatagramSocket.java index cffbd913..74498640 100644 --- a/src/main/java/org/ice4j/socket/DelegatingDatagramSocket.java +++ b/src/main/java/org/ice4j/socket/DelegatingDatagramSocket.java @@ -282,10 +282,10 @@ public void bind(SocketAddress addr) @Override public void close() { - if (delegate == null) - super.close(); - else + if (delegate != null) delegate.close(); + + super.close(); } /** diff --git a/src/main/java/org/ice4j/socket/MultiplexedDatagramSocket.java b/src/main/java/org/ice4j/socket/MultiplexedDatagramSocket.java index 595830d2..086281eb 100644 --- a/src/main/java/org/ice4j/socket/MultiplexedDatagramSocket.java +++ b/src/main/java/org/ice4j/socket/MultiplexedDatagramSocket.java @@ -118,6 +118,8 @@ public int getReceiveBufferSize() public void close() { multiplexing.close(this); + + super.close(); } /** diff --git a/src/main/java/org/ice4j/socket/RelayedCandidateDatagramSocket.java b/src/main/java/org/ice4j/socket/RelayedCandidateDatagramSocket.java index 63b327bf..a3cc2d89 100644 --- a/src/main/java/org/ice4j/socket/RelayedCandidateDatagramSocket.java +++ b/src/main/java/org/ice4j/socket/RelayedCandidateDatagramSocket.java @@ -350,6 +350,8 @@ public void close() turnCandidateHarvest.hostCandidate.getTransportAddress(), this); turnCandidateHarvest.close(this); + + super.close(); } /** From b6e5d035e577d7082b66a492a94fd761e8d7eae9 Mon Sep 17 00:00:00 2001 From: Boris Grozev Date: Wed, 10 May 2017 16:14:29 -0500 Subject: [PATCH 2/2] fix: Fixes a bug which prevents the socket to close. After the delegate is closed, isClosed() would return true, which would prevent the DatagramSocket instance (super) from actually closing and releasing its resources. --- .../org/ice4j/socket/DelegatingDatagramSocket.java | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/src/main/java/org/ice4j/socket/DelegatingDatagramSocket.java b/src/main/java/org/ice4j/socket/DelegatingDatagramSocket.java index 74498640..db1984b2 100644 --- a/src/main/java/org/ice4j/socket/DelegatingDatagramSocket.java +++ b/src/main/java/org/ice4j/socket/DelegatingDatagramSocket.java @@ -115,6 +115,11 @@ static boolean logNonStun(long numOfPacket) */ private long nbSentPackets = 0; + /** + * Whether this socket has been closed. + */ + private boolean closed = false; + /** * Initializes a new DelegatingDatagramSocket instance and binds it * to any available port on the local host machine. The socket will be @@ -282,10 +287,14 @@ public void bind(SocketAddress addr) @Override public void close() { + // We want both #delegate and super to actually get closed (and release + // the FDs which they hold). But super will not close unless isClosed() + // returns false. So we update the #closed flag last. if (delegate != null) delegate.close(); super.close(); + closed = true; } /** @@ -610,7 +619,7 @@ public boolean isBound() @Override public boolean isClosed() { - return (delegate == null) ? super.isClosed() : delegate.isClosed(); + return closed; } /**