From 0ff61ea6f96192e356013eb6bd68493fd2547dcb Mon Sep 17 00:00:00 2001 From: Corentin Chary Date: Tue, 21 May 2024 15:28:47 +0200 Subject: [PATCH 1/5] uds-stream: close unused sockets We need to make sure we close the socket file descriptors before forgetting them otherwise we leak fds. --- src/main/java/com/timgroup/statsd/UnixStreamClientChannel.java | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/main/java/com/timgroup/statsd/UnixStreamClientChannel.java b/src/main/java/com/timgroup/statsd/UnixStreamClientChannel.java index 8b4ab17a..86f069ef 100644 --- a/src/main/java/com/timgroup/statsd/UnixStreamClientChannel.java +++ b/src/main/java/com/timgroup/statsd/UnixStreamClientChannel.java @@ -136,9 +136,11 @@ private void connect() throws IOException { } if (!delegate.connect(address)) { if (connectionTimeout > 0 && System.nanoTime() > deadline) { + delegate.close(); throw new IOException("Connection timed out"); } if (!delegate.finishConnect()) { + delegate.close(); throw new IOException("Connection failed"); } } From fd2efdc8ab3f321bcac88d60afea7d5cf658ece5 Mon Sep 17 00:00:00 2001 From: Corentin Chary Date: Tue, 21 May 2024 16:10:56 +0200 Subject: [PATCH 2/5] uds-stream: catch more exceptions --- .../statsd/UnixStreamClientChannel.java | 42 ++++++++++++------- 1 file changed, 27 insertions(+), 15 deletions(-) diff --git a/src/main/java/com/timgroup/statsd/UnixStreamClientChannel.java b/src/main/java/com/timgroup/statsd/UnixStreamClientChannel.java index 86f069ef..b64acfc5 100644 --- a/src/main/java/com/timgroup/statsd/UnixStreamClientChannel.java +++ b/src/main/java/com/timgroup/statsd/UnixStreamClientChannel.java @@ -1,5 +1,6 @@ package com.timgroup.statsd; +import java.net.SocketOption; import jnr.unixsocket.UnixSocketAddress; import jnr.unixsocket.UnixSocketChannel; import jnr.unixsocket.UnixSocketOptions; @@ -134,28 +135,39 @@ private void connect() throws IOException { // We'd have better timeout support if we used Java 16's native Unix domain socket support (JEP 380) delegate.setOption(UnixSocketOptions.SO_SNDTIMEO, connectionTimeout); } - if (!delegate.connect(address)) { - if (connectionTimeout > 0 && System.nanoTime() > deadline) { - delegate.close(); - throw new IOException("Connection timed out"); + try { + if (!delegate.connect(address)) { + if (connectionTimeout > 0 && System.nanoTime() > deadline) { + closeSafe(delegate); + throw new IOException("Connection timed out"); + } + if (!delegate.finishConnect()) { + closeSafe(delegate); + throw new IOException("Connection failed"); + } } - if (!delegate.finishConnect()) { - delegate.close(); - throw new IOException("Connection failed"); + + delegate.setOption(UnixSocketOptions.SO_SNDTIMEO, Math.max(timeout, 0)); + if (bufferSize > 0) { + delegate.setOption(UnixSocketOptions.SO_SNDBUF, bufferSize); } + } catch (Exception e) { + closeSafe(delegate); + throw e; } - if (timeout > 0) { - delegate.setOption(UnixSocketOptions.SO_SNDTIMEO, timeout); - } else { - delegate.setOption(UnixSocketOptions.SO_SNDTIMEO, 0); - } - if (bufferSize > 0) { - delegate.setOption(UnixSocketOptions.SO_SNDBUF, bufferSize); - } + this.delegate = delegate; } + static private void closeSafe(UnixSocketChannel channel) { + try { + channel.close(); + } catch (IOException e) { + // ignore + } + } + @Override public void close() throws IOException { disconnect(); From d929fba56d82bb8854e2d249d70868db76409c9d Mon Sep 17 00:00:00 2001 From: Corentin Chary Date: Tue, 21 May 2024 16:14:11 +0200 Subject: [PATCH 3/5] uds-stream: refactor try --- .../timgroup/statsd/UnixStreamClientChannel.java | 14 ++++++-------- 1 file changed, 6 insertions(+), 8 deletions(-) diff --git a/src/main/java/com/timgroup/statsd/UnixStreamClientChannel.java b/src/main/java/com/timgroup/statsd/UnixStreamClientChannel.java index b64acfc5..321e66f3 100644 --- a/src/main/java/com/timgroup/statsd/UnixStreamClientChannel.java +++ b/src/main/java/com/timgroup/statsd/UnixStreamClientChannel.java @@ -138,11 +138,9 @@ private void connect() throws IOException { try { if (!delegate.connect(address)) { if (connectionTimeout > 0 && System.nanoTime() > deadline) { - closeSafe(delegate); throw new IOException("Connection timed out"); } if (!delegate.finishConnect()) { - closeSafe(delegate); throw new IOException("Connection failed"); } } @@ -152,7 +150,11 @@ private void connect() throws IOException { delegate.setOption(UnixSocketOptions.SO_SNDBUF, bufferSize); } } catch (Exception e) { - closeSafe(delegate); + try { + delegate.close(); + } catch (IOException __) { + // ignore + } throw e; } @@ -161,11 +163,7 @@ private void connect() throws IOException { } static private void closeSafe(UnixSocketChannel channel) { - try { - channel.close(); - } catch (IOException e) { - // ignore - } + } @Override From 627ff1ebed77c32d5a49855ac78e1c2544e9298b Mon Sep 17 00:00:00 2001 From: Corentin Chary Date: Tue, 21 May 2024 16:19:48 +0200 Subject: [PATCH 4/5] Remove unused import --- src/main/java/com/timgroup/statsd/UnixStreamClientChannel.java | 1 - 1 file changed, 1 deletion(-) diff --git a/src/main/java/com/timgroup/statsd/UnixStreamClientChannel.java b/src/main/java/com/timgroup/statsd/UnixStreamClientChannel.java index 321e66f3..f2ffb561 100644 --- a/src/main/java/com/timgroup/statsd/UnixStreamClientChannel.java +++ b/src/main/java/com/timgroup/statsd/UnixStreamClientChannel.java @@ -1,6 +1,5 @@ package com.timgroup.statsd; -import java.net.SocketOption; import jnr.unixsocket.UnixSocketAddress; import jnr.unixsocket.UnixSocketChannel; import jnr.unixsocket.UnixSocketOptions; From c59bcdbd3483c8b67a524c4bb0d6df7b9e023a24 Mon Sep 17 00:00:00 2001 From: Corentin Chary Date: Tue, 21 May 2024 16:27:35 +0200 Subject: [PATCH 5/5] Remove empty function --- .../java/com/timgroup/statsd/UnixStreamClientChannel.java | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/src/main/java/com/timgroup/statsd/UnixStreamClientChannel.java b/src/main/java/com/timgroup/statsd/UnixStreamClientChannel.java index f2ffb561..fa8019a1 100644 --- a/src/main/java/com/timgroup/statsd/UnixStreamClientChannel.java +++ b/src/main/java/com/timgroup/statsd/UnixStreamClientChannel.java @@ -160,11 +160,7 @@ private void connect() throws IOException { this.delegate = delegate; } - - static private void closeSafe(UnixSocketChannel channel) { - - } - + @Override public void close() throws IOException { disconnect();