diff --git a/src/main/java/com/timgroup/statsd/UnixStreamClientChannel.java b/src/main/java/com/timgroup/statsd/UnixStreamClientChannel.java index 8b4ab17a..fa8019a1 100644 --- a/src/main/java/com/timgroup/statsd/UnixStreamClientChannel.java +++ b/src/main/java/com/timgroup/statsd/UnixStreamClientChannel.java @@ -134,26 +134,33 @@ 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) { - throw new IOException("Connection timed out"); + try { + if (!delegate.connect(address)) { + if (connectionTimeout > 0 && System.nanoTime() > deadline) { + throw new IOException("Connection timed out"); + } + if (!delegate.finishConnect()) { + throw new IOException("Connection failed"); + } } - if (!delegate.finishConnect()) { - 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) { + try { + delegate.close(); + } catch (IOException __) { + // ignore + } + 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; } - + @Override public void close() throws IOException { disconnect();