Skip to content

Commit

Permalink
Fix Netty HTTP/2 handler job completion to avoid false cancellation (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
Sergey Mashkov committed Jan 10, 2020
1 parent cdd5c94 commit 6365524
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import io.netty.handler.codec.http2.*
import io.netty.handler.ssl.*
import io.netty.handler.timeout.*
import io.netty.util.concurrent.*
import kotlinx.coroutines.*
import java.nio.channels.*
import java.security.*
import java.security.cert.*
Expand Down Expand Up @@ -95,6 +96,9 @@ class NettyChannelInitializer(
ApplicationProtocolNames.HTTP_2 -> {
val handler = NettyHttp2Handler(enginePipeline, environment.application, callEventGroup, userContext)
pipeline.addLast(Http2MultiplexCodecBuilder.forServer(handler).build())
pipeline.channel().closeFuture().addListener {
handler.cancel()
}
}
ApplicationProtocolNames.HTTP_1_1 -> {
val requestQueue = NettyRequestQueue(requestQueueLimit, runningLimit)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ internal class NettyHttp2Handler(
private val callEventGroup: EventExecutorGroup,
private val userCoroutineContext: CoroutineContext
) : ChannelInboundHandlerAdapter(), CoroutineScope {
private val handlerJob = CompletableDeferred<Nothing>()
private val handlerJob = SupervisorJob(userCoroutineContext[Job])

override val coroutineContext: CoroutineContext
get() = handlerJob
Expand Down Expand Up @@ -65,15 +65,9 @@ internal class NettyHttp2Handler(

@Suppress("OverridingDeprecatedMember")
override fun exceptionCaught(ctx: ChannelHandlerContext, cause: Throwable) {
handlerJob.completeExceptionally(cause)
ctx.close()
}

override fun handlerRemoved(ctx: ChannelHandlerContext?) {
super.handlerRemoved(ctx)
handlerJob.cancel()
}

private fun startHttp2(context: ChannelHandlerContext, headers: Http2Headers) {
val requestQueue = NettyRequestQueue(1, 1)
val responseWriter = NettyResponsePipeline(context, WriterEncapsulation.Http2, requestQueue, handlerJob)
Expand Down

0 comments on commit 6365524

Please sign in to comment.