-
Notifications
You must be signed in to change notification settings - Fork 1.1k
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
KTOR-8244: Fixed an issue where canceling the incoming flow of OkHttpSSESession/DefaultClientSSESession failed to disconnect the SSE session. #4694
Conversation
Hey @hehua2008, lgtm. Could you add a test, so we can merge? :) |
For same bug in DefaultClientSSESession: }.catch { cause ->
when (cause) {
is CancellationException -> {
// CancellationException will be handled by onCompletion operator
}
else -> {
LOGGER.trace { "Error during SSE session processing: $cause" }
close()
throw cause
}
}
}.onCompletion { cause ->
// Because catch operator only catch throwable occurs in upstream flow, so we use onCompletion operator instead
// to handle CancellationException occurs in either upstream flow or downstream flow.
if (cause is CancellationException) {
close()
}
} |
I have added testCancelSseSession in ServerSentEventsTest for ktor-client: @Test
fun testCancelSseSession() = clientTests {
config {
install(SSE)
}
test { client ->
coroutineScope {
val job = launch {
val session = client.serverSentEventsSession("$TEST_SERVER/sse/hello?times=20&interval=100")
try {
session.incoming.collect()
} finally {
withContext(NonCancellable) {
delay(500)
assertFalse(session.isActive)
}
}
}
delay(500)
job.cancelAndJoin()
}
}
} |
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.
lgtm
Hey @hehua2008, thank you for the PR! Could you please fix import ordering in |
8912c86
to
a4c13db
Compare
Sorry for that, I have fixed it. |
a4c13db
to
b0d6f5e
Compare
…failed to disconnect the SSE session.
…ession failed to disconnect the SSE session.
Head branch was pushed to by a user without write access
b0d6f5e
to
3f6c365
Compare
When we can expect it to be released? |
In the next patch release of Ktor 3.1.2. |
I would like to know some est. date, cause it's a critical defect in our app and we can not release it without that fix. Thank you |
Hey, @Savrov! We usually do monthly releases, and the next one is planned for the last week of March. |
Fixed an issue where canceling the incoming flow of OkHttpSSESession failed to disconnect the SSE session.
Subsystem
Client
ktor-client/ktor-client-okhttp
Motivation
Refer to a bug/ticket #4692.
In the OkHttpSSESession implementation of SSESession, cancel incoming flow will NOT trigger serverSentEventsSource.cancel(), which will keep SSE still connecting !
Solution
First,
val incoming: Flow<ServerSentEvent>
in OkHttpSSESession should always return the same Flow instance, just as DefaultClientSSESession did.Second, cancel
_incoming.receiveAsFlow()
will just cancel the Flow but will NOT cancel the _incoming Channel, so we should use_incoming.consumeAsFlow()
instead.Third, if
incoming.trySendBlocking(ServerSentEvent(data, type, id))
fails with a CancellationException, we should rethrow the CancellationException to letfun onFailure(eventSource: EventSource, t: Throwable?, response: Response?)
handle it and close the SSE session.