Skip to content

Commit

Permalink
Fix Jetty upgrade cancellation on connection close
Browse files Browse the repository at this point in the history
  • Loading branch information
Sergey Mashkov committed Feb 5, 2020
1 parent 614f637 commit 1734027
Showing 1 changed file with 8 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -31,20 +31,9 @@ object JettyUpgradeImpl : ServletUpgrade {
// for upgraded connections IDLE timeout should be significantly increased
endPoint.idleTimeout = TimeUnit.MINUTES.toMillis(60L)

withContext(engineContext) {
withContext(engineContext + CoroutineName("upgrade-scope")) {
try {
coroutineScope {
endPoint.connection.addListener(
object : Connection.Listener {
override fun onOpened(connection: Connection?) {
}

override fun onClosed(connection: Connection?) {
cancel()
}
}
)

val inputChannel = ByteChannel(autoFlush = true)
val reader = EndPointReader(endPoint, coroutineContext, inputChannel)
val writer = endPointWriter(endPoint)
Expand All @@ -54,10 +43,16 @@ object JettyUpgradeImpl : ServletUpgrade {
if (endPoint is AbstractEndPoint) {
endPoint.upgrade(reader)
}
upgrade.upgrade(
val upgradeJob = upgrade.upgrade(
inputChannel, outputChannel, coroutineContext,
coroutineContext + userContext
)

upgradeJob.invokeOnCompletion {
inputChannel.cancel()
outputChannel.close()
cancel()
}
}
} finally {
connection.close()
Expand Down

0 comments on commit 1734027

Please sign in to comment.