Skip to content

Commit ea4647b

Browse files
authored
Handle interrupts: Socket.connect is interruptible in a virtual thread (#1203)
JAVA-5138
1 parent 808ad6c commit ea4647b

File tree

2 files changed

+9
-3
lines changed

2 files changed

+9
-3
lines changed

driver-core/src/main/com/mongodb/internal/connection/SocketStream.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@
4545
import static com.mongodb.assertions.Assertions.notNull;
4646
import static com.mongodb.internal.connection.SocketStreamHelper.configureSocket;
4747
import static com.mongodb.internal.connection.SslHelper.configureSslSocket;
48+
import static com.mongodb.internal.thread.InterruptionUtil.translateInterruptedException;
4849
import static java.util.concurrent.TimeUnit.MILLISECONDS;
4950

5051
/**
@@ -79,7 +80,8 @@ public void open() {
7980
inputStream = socket.getInputStream();
8081
} catch (IOException e) {
8182
close();
82-
throw new MongoSocketOpenException("Exception opening socket", getAddress(), e);
83+
throw translateInterruptedException(e, "Interrupted while connecting")
84+
.orElseThrow(() -> new MongoSocketOpenException("Exception opening socket", getAddress(), e));
8385
}
8486
}
8587

@@ -240,7 +242,7 @@ public void close() {
240242
if (socket != null) {
241243
socket.close();
242244
}
243-
} catch (IOException e) {
245+
} catch (IOException | RuntimeException e) {
244246
// ignore
245247
}
246248
}

driver-core/src/main/com/mongodb/internal/connection/SocksSocket.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,11 @@ public void connect(final SocketAddress endpoint, final int timeoutMs) throws IO
105105
* 1. Enforces self-closing under RFC 1928 if METHOD is X'FF'.
106106
* 2. Handles all other errors during connection, distinct from external closures.
107107
*/
108-
close();
108+
try {
109+
close();
110+
} catch (Exception closeException) {
111+
socketException.addSuppressed(closeException);
112+
}
109113
throw socketException;
110114
}
111115
}

0 commit comments

Comments
 (0)