Skip to content
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

Fix rare race condition in ZClient causing healthy connections to be discarded #2924

Merged
merged 3 commits into from
Jun 23, 2024
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 @@ -77,6 +77,8 @@ abstract class AsyncBodyReader extends SimpleChannelInboundHandler[HttpContent](
val _ = ctx.channel().config().setAutoRead(previousAutoRead)
}

protected def onLastMessage(): Unit = ()

override def channelRead0(
ctx: ChannelHandlerContext,
msg: HttpContent,
Expand All @@ -87,6 +89,12 @@ abstract class AsyncBodyReader extends SimpleChannelInboundHandler[HttpContent](
val isLast = msg.isInstanceOf[LastHttpContent]
val content = ByteBufUtil.getBytes(msg.content())

if (isLast) {
readingDone = true
ctx.channel().pipeline().remove(this)
onLastMessage()
}

state match {
case State.Buffering =>
// `connect` method hasn't been called yet, add all incoming content to the buffer
Expand All @@ -103,13 +111,7 @@ abstract class AsyncBodyReader extends SimpleChannelInboundHandler[HttpContent](
callback(Chunk.fromArray(content), isLast)
}

if (isLast) {
readingDone = true
ctx.channel().pipeline().remove(this)
} else {
ctx.read()
}
()
if (!isLast) ctx.read(): Unit
}
}

Expand Down Expand Up @@ -137,6 +139,8 @@ abstract class AsyncBodyReader extends SimpleChannelInboundHandler[HttpContent](
}

object AsyncBodyReader {
private val FnUnit = () => ()

sealed trait State

object State {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,18 +35,16 @@ final class ClientResponseStreamHandler(

private implicit val unsafe: Unsafe = Unsafe.unsafe

override def onLastMessage(): Unit =
if (keepAlive)
onComplete.unsafe.done(Exit.succeed(ChannelState.forStatus(status)))
else
onComplete.unsafe.done(Exit.succeed(ChannelState.Invalid))

override def channelRead0(ctx: ChannelHandlerContext, msg: HttpContent): Unit = {
val isLast = msg.isInstanceOf[LastHttpContent]
super.channelRead0(ctx, msg)

if (isLast) {
if (keepAlive)
onComplete.unsafe.done(Exit.succeed(ChannelState.forStatus(status)))
else {
onComplete.unsafe.done(Exit.succeed(ChannelState.Invalid))
ctx.close(): Unit
}
}
if (isLast && !keepAlive) ctx.close(): Unit
}

override def exceptionCaught(ctx: ChannelHandlerContext, cause: Throwable): Unit =
Expand Down
Loading