Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Handle interrupts: Socket.connect is interruptible in a virtual thread #1203

Merged
merged 4 commits into from
Sep 29, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@
import static com.mongodb.assertions.Assertions.notNull;
import static com.mongodb.internal.connection.SocketStreamHelper.configureSocket;
import static com.mongodb.internal.connection.SslHelper.configureSslSocket;
import static com.mongodb.internal.thread.InterruptionUtil.translateInterruptedException;
import static java.util.concurrent.TimeUnit.MILLISECONDS;

/**
Expand Down Expand Up @@ -79,7 +80,8 @@ public void open() {
inputStream = socket.getInputStream();
} catch (IOException e) {
close();
throw new MongoSocketOpenException("Exception opening socket", getAddress(), e);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it's worth a ticket to make this consistent with read/write methods to throw IOException and do the exception transation in InternalStreamConnection. It can be dependent on the ticket to remove Stream from the API.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

throw translateInterruptedException(e, "Interrupted while connecting")
.orElseThrow(() -> new MongoSocketOpenException("Exception opening socket", getAddress(), e));
}
}

Expand Down Expand Up @@ -240,7 +242,7 @@ public void close() {
if (socket != null) {
socket.close();
}
} catch (IOException e) {
} catch (IOException | RuntimeException e) {
vbabanin marked this conversation as resolved.
Show resolved Hide resolved
// ignore
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,11 @@ public void connect(final SocketAddress endpoint, final int timeoutMs) throws IO
* 1. Enforces self-closing under RFC 1928 if METHOD is X'FF'.
* 2. Handles all other errors during connection, distinct from external closures.
*/
close();
try {
close();
} catch (Exception closeException) {
socketException.addSuppressed(closeException);
}
throw socketException;
}
}
Expand Down