@@ -134,26 +134,33 @@ private void connect() throws IOException {
134134 // We'd have better timeout support if we used Java 16's native Unix domain socket support (JEP 380)
135135 delegate .setOption (UnixSocketOptions .SO_SNDTIMEO , connectionTimeout );
136136 }
137- if (!delegate .connect (address )) {
138- if (connectionTimeout > 0 && System .nanoTime () > deadline ) {
139- throw new IOException ("Connection timed out" );
137+ try {
138+ if (!delegate .connect (address )) {
139+ if (connectionTimeout > 0 && System .nanoTime () > deadline ) {
140+ throw new IOException ("Connection timed out" );
141+ }
142+ if (!delegate .finishConnect ()) {
143+ throw new IOException ("Connection failed" );
144+ }
140145 }
141- if (!delegate .finishConnect ()) {
142- throw new IOException ("Connection failed" );
146+
147+ delegate .setOption (UnixSocketOptions .SO_SNDTIMEO , Math .max (timeout , 0 ));
148+ if (bufferSize > 0 ) {
149+ delegate .setOption (UnixSocketOptions .SO_SNDBUF , bufferSize );
143150 }
151+ } catch (Exception e ) {
152+ try {
153+ delegate .close ();
154+ } catch (IOException __ ) {
155+ // ignore
156+ }
157+ throw e ;
144158 }
145159
146- if (timeout > 0 ) {
147- delegate .setOption (UnixSocketOptions .SO_SNDTIMEO , timeout );
148- } else {
149- delegate .setOption (UnixSocketOptions .SO_SNDTIMEO , 0 );
150- }
151- if (bufferSize > 0 ) {
152- delegate .setOption (UnixSocketOptions .SO_SNDBUF , bufferSize );
153- }
160+
154161 this .delegate = delegate ;
155162 }
156-
163+
157164 @ Override
158165 public void close () throws IOException {
159166 disconnect ();
0 commit comments