-
-
Notifications
You must be signed in to change notification settings - Fork 470
Improve client timeout options (#2383) #3844
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -17,6 +17,8 @@ | |
| package zio.http.netty | ||
|
|
||
| import java.io.IOException | ||
| import java.util.concurrent.TimeUnit | ||
| import java.util.concurrent.atomic.AtomicReference | ||
|
|
||
| import scala.collection.mutable | ||
|
|
||
|
|
@@ -28,15 +30,18 @@ import zio.http.netty.NettyBody.UnsafeAsync | |
| import io.netty.buffer.ByteBufUtil | ||
| import io.netty.channel.{ChannelHandlerContext, SimpleChannelInboundHandler} | ||
| import io.netty.handler.codec.http.{HttpContent, LastHttpContent} | ||
| import io.netty.util.concurrent.ScheduledFuture | ||
|
|
||
| private[netty] abstract class AsyncBodyReader extends SimpleChannelInboundHandler[HttpContent](true) { | ||
| private[netty] abstract class AsyncBodyReader(timeoutMillis: Option[Long]) | ||
| extends SimpleChannelInboundHandler[HttpContent](true) { | ||
| import zio.http.netty.AsyncBodyReader._ | ||
|
|
||
| private var state: State = State.Buffering | ||
| private val buffer = new mutable.ArrayBuilder.ofByte() | ||
| private var previousAutoRead: Boolean = false | ||
| private var readingDone: Boolean = false | ||
| private var ctx: ChannelHandlerContext = _ | ||
| private var state: State = State.Buffering | ||
| private val buffer = new mutable.ArrayBuilder.ofByte() | ||
| private var previousAutoRead: Boolean = false | ||
| private var readingDone: Boolean = false | ||
| private var ctx: ChannelHandlerContext = _ | ||
| private val timeoutTask: AtomicReference[ScheduledFuture[_]] = new AtomicReference(null) | ||
|
|
||
| private def result(buffer: mutable.ArrayBuilder.ofByte): Chunk[Byte] = { | ||
| val arr = buffer.result() | ||
|
|
@@ -57,12 +62,50 @@ private[netty] abstract class AsyncBodyReader extends SimpleChannelInboundHandle | |
| case UnsafeAsync.Aggregating(bufSize) => buffer.sizeHint(bufSize) | ||
| case cb => cb(result(buffer0), isLast = false) | ||
| } | ||
|
|
||
| // Schedule timeout task if configured | ||
| timeoutMillis.foreach { timeoutMillis => | ||
| val task = ctx | ||
| .channel() | ||
| .eventLoop() | ||
| .schedule( | ||
| new Runnable { | ||
| override def run(): Unit = { | ||
| AsyncBodyReader.this.synchronized { | ||
| state match { | ||
| case State.Direct(cb) if !readingDone => | ||
| cb.fail( | ||
| new IOException( | ||
| s"Body read timeout: server stopped sending data after ${timeoutMillis}ms", | ||
| ), | ||
| ) | ||
| // Mark as done to prevent further processing | ||
| readingDone = true | ||
| if (ctx.channel().isOpen) { | ||
| ctx.channel().close(): Unit | ||
| } | ||
| case _ => // Already completed or not connected | ||
| } | ||
| } | ||
| } | ||
| }, | ||
| timeoutMillis, | ||
| TimeUnit.MILLISECONDS, | ||
| ) | ||
| timeoutTask.set(task) | ||
| } | ||
|
|
||
| ctx.read(): Unit | ||
| } else { | ||
| throw new IllegalStateException("Attempting to read from a closed channel, which will never finish") | ||
| // Channel is already closed - fail immediately with appropriate error | ||
| callback.fail( | ||
| new IOException( | ||
| "Server closed connection before sending complete response body", | ||
| ), | ||
| ) | ||
| } | ||
| case _ => | ||
| throw new IllegalStateException("Cannot connect twice") | ||
| callback.fail(new IllegalStateException("Cannot connect twice")) | ||
| } | ||
| } | ||
| } | ||
|
|
@@ -74,7 +117,13 @@ private[netty] abstract class AsyncBodyReader extends SimpleChannelInboundHandle | |
| } | ||
|
|
||
| override def handlerRemoved(ctx: ChannelHandlerContext): Unit = { | ||
| val _ = ctx.channel().config().setAutoRead(previousAutoRead) | ||
| val _ = ctx.channel().config().setAutoRead(previousAutoRead) | ||
| // Cancel any pending timeout task | ||
| val currentTask = timeoutTask.get() | ||
| if (currentTask != null) { | ||
| currentTask.cancel(false) | ||
| timeoutTask.set(null) | ||
| } | ||
| } | ||
|
|
||
| protected def onLastMessage(): Unit = () | ||
|
|
@@ -89,6 +138,13 @@ private[netty] abstract class AsyncBodyReader extends SimpleChannelInboundHandle | |
| val isLast = msg.isInstanceOf[LastHttpContent] | ||
| val content = ByteBufUtil.getBytes(msg.content()) | ||
|
|
||
| // Cancel timeout task since we received data | ||
| val currentTask = timeoutTask.get() | ||
| if (currentTask != null) { | ||
| currentTask.cancel(false) | ||
| timeoutTask.set(null) | ||
| } | ||
|
|
||
| if (isLast) { | ||
| readingDone = true | ||
| ctx.channel().pipeline().remove(this) | ||
|
|
@@ -121,12 +177,52 @@ private[netty] abstract class AsyncBodyReader extends SimpleChannelInboundHandle | |
| !isLast | ||
| } | ||
|
|
||
| // Reschedule timeout for next chunk if not the last message | ||
| if (readMore && !isLast) { | ||
| timeoutMillis.foreach { timeoutMillis => | ||
| val task = ctx | ||
| .channel() | ||
| .eventLoop() | ||
| .schedule( | ||
| new Runnable { | ||
| override def run(): Unit = { | ||
| AsyncBodyReader.this.synchronized { | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is a bit confusing (I've actually never seen this syntax before). Maybe just use |
||
| state match { | ||
| case State.Direct(cb) if !readingDone => | ||
| cb.fail( | ||
| new IOException( | ||
| s"Body read timeout: server stopped sending data after ${timeoutMillis}ms", | ||
| ), | ||
| ) | ||
| readingDone = true | ||
| if (ctx.channel().isOpen) { | ||
| ctx.channel().close(): Unit | ||
| } | ||
| case _ => // Already completed or not connected | ||
| } | ||
| } | ||
| } | ||
| }, | ||
| timeoutMillis, | ||
| TimeUnit.MILLISECONDS, | ||
| ) | ||
| timeoutTask.set(task) | ||
| } | ||
| } | ||
|
Comment on lines
+182
to
+211
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Isn't this the same code as above? Maybe extract it all into a method if that's the case? |
||
|
|
||
| if (readMore) ctx.read(): Unit | ||
| } | ||
| } | ||
|
|
||
| override def exceptionCaught(ctx: ChannelHandlerContext, cause: Throwable): Unit = { | ||
| this.synchronized { | ||
| // Cancel timeout task | ||
| val currentTask = timeoutTask.get() | ||
| if (currentTask != null) { | ||
| currentTask.cancel(false) | ||
| timeoutTask.set(null) | ||
| } | ||
|
|
||
| state match { | ||
| case State.Buffering => | ||
| case State.Direct(callback) => | ||
|
|
@@ -138,10 +234,27 @@ private[netty] abstract class AsyncBodyReader extends SimpleChannelInboundHandle | |
|
|
||
| override def channelInactive(ctx: ChannelHandlerContext): Unit = { | ||
| this.synchronized { | ||
| // Cancel timeout task | ||
| val currentTask = timeoutTask.get() | ||
| if (currentTask != null) { | ||
| currentTask.cancel(false) | ||
| timeoutTask.set(null) | ||
| } | ||
|
Comment on lines
+238
to
+242
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is being used everywhere and doesn't have any dependency on the local vals. Maybe extract it into a method to keep things DRY |
||
|
|
||
| state match { | ||
| case State.Buffering => | ||
| case State.Direct(callback) => | ||
| callback.fail(new IOException("Channel closed unexpectedly")) | ||
| case State.Buffering => | ||
| case State.Direct(callback) if !readingDone => | ||
| // Step 4: Premature channel closure detection | ||
| // This is the core issue from #2383 - server sent headers but closed before body completed | ||
| // Provide a clear, actionable error message | ||
| callback.fail( | ||
| new IOException( | ||
| "Server closed connection before sending complete response body. " + | ||
| "This may indicate a broken server, network issue, or server-side timeout.", | ||
| ), | ||
| ) | ||
| case _ => | ||
| // Reading already done - this is a normal close after completion | ||
| } | ||
| } | ||
| ctx.fireChannelInactive(): Unit | ||
|
|
||
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.
If you wrap this in
synchronizedyou can simply use a var fortimeoutTaskinstead of anAtomicReference.