Skip to content

Fix closing connection on cancelling requester #220

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

Merged
merged 1 commit into from Apr 13, 2022
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,18 +45,35 @@ internal suspend inline fun connect(
}

val requester = interceptors.wrapRequester(
RSocketRequester(requestContext + CoroutineName("rSocket-requester"), frameSender, streamsStorage, connection.pool)
RSocketRequester(
requestContext + CoroutineName("rSocket-requester"),
frameSender,
streamsStorage,
connection.pool
)
)
val requestHandler = interceptors.wrapResponder(
with(interceptors.wrapAcceptor(acceptor)) {
ConnectionAcceptorContext(connectionConfig, requester).accept()
}
)
val responder = RSocketResponder(requestContext + CoroutineName("rSocket-responder"), frameSender, requestHandler)
val responder = RSocketResponder(
requestContext + CoroutineName("rSocket-responder"),
frameSender,
requestHandler
)

// link completing of connection and requestHandler
connection.coroutineContext[Job]?.invokeOnCompletion { requestHandler.cancel("Connection closed", it) }
requestHandler.coroutineContext[Job]?.invokeOnCompletion { if (it != null) connection.cancel("Request handler failed", it) }
// link completing of requester, connection and requestHandler
requester.coroutineContext[Job]?.invokeOnCompletion {
connection.cancel("Requester cancelled", it)
}
requestHandler.coroutineContext[Job]?.invokeOnCompletion {
if (it != null) connection.cancel("Request handler failed", it)
}
connection.coroutineContext[Job]?.invokeOnCompletion {
requester.cancel("Connection closed", it)
requestHandler.cancel("Connection closed", it)
}

// start keepalive ticks
(connection + CoroutineName("rSocket-connection-keep-alive")).launch {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -404,4 +404,18 @@ class RSocketRequesterTest : TestWithConnection(), TestWithLeakCheck {
fun rcTerminatedOnConnectionClose() =
streamIsTerminatedOnConnectionClose { requester.requestChannel(Payload.Empty, emptyFlow()).collect() }

@Test
fun cancelRequesterToCloseConnection() = test {
val request = requester.requestStream(Payload.Empty).produceIn(GlobalScope)
connection.test {
awaitFrame { frame ->
assertTrue(frame is RequestFrame)
}
requester.cancel() //cancel requester
expectNoEventsIn(200)
}
assertFalse(connection.isActive)
assertTrue(request.isClosedForReceive)
}

}