@@ -134,26 +134,33 @@ private void connect() throws IOException {
134
134
// We'd have better timeout support if we used Java 16's native Unix domain socket support (JEP 380)
135
135
delegate .setOption (UnixSocketOptions .SO_SNDTIMEO , connectionTimeout );
136
136
}
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
+ }
140
145
}
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 );
143
150
}
151
+ } catch (Exception e ) {
152
+ try {
153
+ delegate .close ();
154
+ } catch (IOException __ ) {
155
+ // ignore
156
+ }
157
+ throw e ;
144
158
}
145
159
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
+
154
161
this .delegate = delegate ;
155
162
}
156
-
163
+
157
164
@ Override
158
165
public void close () throws IOException {
159
166
disconnect ();
0 commit comments