-
Notifications
You must be signed in to change notification settings - Fork 6
Propagate Connection Closure to Client #64
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
Open
crazyrokr
wants to merge
3
commits into
JavaSaBr:develop
Choose a base branch
from
crazyrokr:propagate-connection-close
base: develop
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
12 changes: 12 additions & 0 deletions
12
rlib-network/src/main/java/javasabr/rlib/network/exception/ConnectionClosedException.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,12 @@ | ||
| package javasabr.rlib.network.exception; | ||
|
|
||
| public class ConnectionClosedException extends NetworkException { | ||
|
|
||
| public ConnectionClosedException(String remoteAddress) { | ||
| super("Connection closed: %s".formatted(remoteAddress)); | ||
| } | ||
|
|
||
| public ConnectionClosedException(String remoteAddress, Throwable cause) { | ||
| super("Connection closed: %s".formatted(remoteAddress), cause); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
98 changes: 98 additions & 0 deletions
98
rlib-network/src/test/java/javasabr/rlib/network/ConnectionCloseTest.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,98 @@ | ||
| package javasabr.rlib.network; | ||
|
|
||
| import static org.assertj.core.api.Assertions.assertThat; | ||
|
|
||
| import java.io.InputStream; | ||
| import java.net.InetSocketAddress; | ||
| import java.util.concurrent.CountDownLatch; | ||
| import java.util.concurrent.TimeUnit; | ||
| import java.util.function.Supplier; | ||
| import javasabr.rlib.network.exception.ConnectionClosedException; | ||
| import javasabr.rlib.network.impl.AbstractConnection; | ||
| import javasabr.rlib.network.impl.DefaultConnection; | ||
| import javasabr.rlib.network.packet.impl.DefaultReadableNetworkPacket; | ||
| import javasabr.rlib.network.packet.impl.StringWritableNetworkPacket; | ||
| import javasabr.rlib.network.packet.registry.ReadableNetworkPacketRegistry; | ||
| import javasabr.rlib.network.util.NetworkUtils; | ||
| import javax.net.ssl.SSLContext; | ||
| import lombok.SneakyThrows; | ||
| import org.junit.jupiter.api.Test; | ||
|
|
||
| public class ConnectionCloseTest extends BaseNetworkTest { | ||
|
|
||
| @Test | ||
| void shouldPropagateConnectionCloseToClient() throws InterruptedException { | ||
| // given | ||
| var packetRegistry = ReadableNetworkPacketRegistry.of( | ||
| DefaultReadableNetworkPacket.class, | ||
| DefaultConnection.class, | ||
| DefaultNetworkTest.ServerPackets.RequestEchoMessage.class, | ||
| DefaultNetworkTest.ServerPackets.RequestServerTime.class); | ||
| var serverNetwork = NetworkFactory.defaultServerNetwork(packetRegistry); | ||
| InetSocketAddress serverAddress = serverNetwork.start(); | ||
| serverNetwork.onAccept(AbstractConnection::close); | ||
| var clientNetwork = NetworkFactory.defaultClientNetwork(packetRegistry); | ||
| CountDownLatch closeLatch = new CountDownLatch(1); | ||
|
|
||
| // when | ||
| clientNetwork | ||
| .connectReactive(serverAddress) | ||
| .flatMapMany(AbstractConnection::receivedEvents) | ||
| .doOnError(e -> { | ||
| if (e instanceof ConnectionClosedException) { | ||
| closeLatch.countDown(); | ||
| } | ||
| }) | ||
| .subscribe(); | ||
|
|
||
| // then | ||
| assertThat(closeLatch.await(5000, TimeUnit.MILLISECONDS)) | ||
| .as("Client should be notified that connection is closed") | ||
| .isTrue(); | ||
| clientNetwork.shutdown(); | ||
| serverNetwork.shutdown(); | ||
| } | ||
|
|
||
| @Test | ||
| @SneakyThrows | ||
| void shouldCloseServerConnectionWhenClientClosesTcpChannelAbruptly() { | ||
| // Given: established SSL connection with completed handshake | ||
| InputStream keystoreFile = ConnectionCloseTest.class.getResourceAsStream("/ssl/rlib_test_cert.p12"); | ||
| SSLContext serverSslContext = NetworkUtils.createSslContext(keystoreFile, "test"); | ||
| SSLContext clientSslContext = NetworkUtils.createAllTrustedClientSslContext(); | ||
|
|
||
| try (var testNetwork = buildStringSSLNetwork(serverSslContext, clientSslContext)) { | ||
| var serverConnection = testNetwork.serverToClient; | ||
| var clientConnection = testNetwork.clientToServer; | ||
|
|
||
| // Register handler to start reading on server side | ||
| CountDownLatch dataReceivedLatch = new CountDownLatch(1); | ||
| serverConnection.onReceiveValidPacket((conn, packet) -> dataReceivedLatch.countDown()); | ||
|
|
||
| // Send data to complete SSL handshake and deliver a packet | ||
| clientConnection.sendInBackground(new StringWritableNetworkPacket<>("handshake")); | ||
|
|
||
| // Wait for the handshake to complete and data to be received | ||
| assertThat(dataReceivedLatch.await(5, TimeUnit.SECONDS)) | ||
| .as("SSL handshake should complete and data should be received by server") | ||
| .isTrue(); | ||
|
|
||
| // When: close client's raw TCP channel without SSL close_notify | ||
| clientConnection.channel().close(); | ||
|
|
||
| assertThat(awaitMillis(5000, serverConnection::closed)) | ||
| .as("Server connection should be closed after receiving EOF from abruptly closed client channel") | ||
| .isTrue(); | ||
| } | ||
| } | ||
|
|
||
| private static boolean awaitMillis(long millis, Supplier<Boolean> a) throws InterruptedException { | ||
| for (int i = 0; i < millis / 100; i++) { | ||
| if (a.get()) { | ||
| return true; | ||
| } | ||
| Thread.sleep(100); | ||
| } | ||
| return false; | ||
| } | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
it's not always about closing connection by my testing
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's correct to close the connection in
AbstractSslNetworkPacketReaderwhenreceivedBytes == -1.In non-blocking I/O (which this library appears to use), a return value of
-1from aSocketChannel.read()call signifies that the connection has been gracefully closed by the remote peer (end-of-stream). If the local connection is not closed in response to this, it may leave the channel in an inconsistent or zombie state, potentially leading to resource leaks or repeated failed reads.A test was added for this case: 6b19a97
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@crazyrokr I know about this, but I had many times during my own testing when it didn't work in this way
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In such case we have to create a test covering the mentioned scenario. If your statement is true, then the test linked above is invalid. Do you have any ideas on how to improve it to account for the scenario you mentioned?