-
Notifications
You must be signed in to change notification settings - Fork 412
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
invoke NettyBody callback in a separate fiber to avoid deadlocks #2402
invoke NettyBody callback in a separate fiber to avoid deadlocks #2402
Conversation
7768ff7
to
c99c6b8
Compare
Codecov ReportPatch coverage:
❗ Your organization is not using the GitHub App Integration. As a result you may experience degraded service beginning May 15th. Please install the Github App Integration for your organization. Read more. Additional details and impacted files@@ Coverage Diff @@
## main #2402 +/- ##
==========================================
- Coverage 63.41% 63.33% -0.09%
==========================================
Files 137 137
Lines 7113 7113
Branches 1259 1259
==========================================
- Hits 4511 4505 -6
- Misses 2602 2608 +6
☔ View full report in Codecov by Sentry. |
@lackhoa nope 🙁 |
Rather than doing ZIO stuff inside a separate fiber, we should consider another mechanism that could be higher-performance. Will take a look at the code and suggest something. |
c99c6b8
to
ffca507
Compare
@jdegoes another easy option would be to break abstraction a little bit and handle it in buffer.result().foreach { case (chunk, isLast) =>
callback(chunk, isLast)
} We can merge these chunks in place and submit one big chunk val (buffered, isLast) =
buffer
.result()
.foldLeft((Chunk.empty[Byte], false)) { case ((acc, _), (chunk, isLast)) =>
(acc ++ chunk, isLast)
}
callback(buffered, isLast) I haven't done any benchmarks yet, but I guess it should be faster than both current version and one proposed in the MR. private val buffer: ChunkBuilder[(Chunk[Byte], Boolean)] = ChunkBuilder.make[(Chunk[Byte], Boolean)]()
...
buffer.result().foreach { case (chunk, isLast) =>
callback(chunk, isLast)
} with private val buffer: ChunkBuilder[Byte] = ChunkBuilder.make[Byte]()
private var isLast: Boolean = false
...
callback(buffer.result(), isLast) What do you think? |
Looks like |
The purpose of the async body reader is to be able to process the body in a streaming way - when you don't want to buffer it in memory. So changing that would basically disable the streaming feature. |
@vigoo not sure how it disables streaming feature. This buffer is used only until we connect some consumer to a channel. Once consumer is connected, this buffer will not be used and chunks received from netty will be passed directly to it. |
Ah ok sorry, misunderstood. |
@myazinn Let's do this in the most performant way possible, please! 🙏 |
@myazinn Master is fixed, btw. 👍 |
@myazinn could you update your branch? |
@kyri-petrou Do you have any thoughts on how to do this better? |
@jdegoes I've given this some thought; I think the main issue here is that we're using a bounded queue in AFAICT there are 2 possible solutions:
Let me know if you're happy with (2) and I'll start working on it; should be relatively easy to implement |
When reading a request from a super-fast producer, it is possible that
zio.http.netty.AsyncBodyReader
accumulates more data in buffer than can fit into Stream buffer inzio.http.netty.NettyBody.AsyncBody#asStream
. In that case, the request will hang because we can't consume messages until callback pushes all the data, which it can't do until we start consuming messages.Minimized example of an issue can be found here
https://scastie.scala-lang.org/sZ8DrRKxR2qAKxrAOV52hg
For a reproduction step on a full server, you can use steps from this ticket
#2297
Sometimes server is able to read data from
yes hello | http --chunked POST localhost:8080/forever
, sometimes it's just silent.I did try to write a test for, but couldn't find a way to reproduce it in tests. So I'd really appreciate if someone gave me a hint on how to do it. Also, callbacks sometimes blow my mind, so I hope nothing goes wrong if we move callback invocation to a separate fiber 🤔