-
Notifications
You must be signed in to change notification settings - Fork 1k
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
Don't block the event loop when reading inputstream. #6120
Conversation
@@ -77,59 +93,61 @@ public InputStreamBodyBinder(HttpContentProcessorResolver processorResolver) { | |||
PipedOutputStream outputStream = new PipedOutputStream(); | |||
try { | |||
PipedInputStream inputStream = new PipedInputStream(outputStream); | |||
processor.subscribe(new CompletionAwareSubscriber<ByteBufHolder>() { | |||
Flux.from(processor) | |||
.onBackpressureBuffer() |
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.
Wouldn't this lead to OOM?
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.
InputStream is not thread safe, so I don't see an alternative
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.
Then I don't think this is an improvement
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.
Provide a reproducer for your OOM theory in that case. "I don't think" is not a valid argument opposing a bug fix
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.
I will try to create the scenario. In the meantime, relax with your tone
Basically my thoughts are that if the stream is not read, etc.. we shouldn't buffer the data as that will lead to OOM exceptions. It's my understanding that in order to not buffer the event loop thread has to wait for the consumer to read the data. |
Feel free to amend the PR if you find a solution that is both thread safe and doesn't need buffering. Right now the current implementation is blocking the event loop |
It's only blocking the event loop if the data isn't read. If the data isn't read with this change it could lead to OOM. I see this as a documentation issue |
Also note that we are already buffering the data in |
@jameskleeh that is not true, the test reads the data and it is blocking the event loop |
Yes it is read after it requires a thread from the event loop. If it is read before that it would not be an issue |
Set a break point on the code that writes to the output stream and you will note that all writes to the output stream (a blocking API) occur on the event loop thread without my change. After my change they occur on the I/O thread. If that isn't blocking the event loop I don't know what is 🤷♂️ |
I'm confused by what you are saying here, looking at the code prior to my change, the Lines 79 to 80 in 8da7f72
At this point the |
When you write to a PipedOutputStream, it blocks the current thread to wait for the reader to read the data. See the |
Well exactly, so why are we writing to it on the event loop thread? |
Perhaps I'm misunderstanding something here so please tell me if so If we should not buffer data then the event loop thread has to wait for the data to be read before requesting more. If the server is continuously reading data from the client and the data isn't being read then it has to go somewhere (buffered), or block. |
Indeed you have to block somewhere, but that somewhere should not be the event loop which is what is currently being blocked. |
I was able to reproduce the OOM in my example app before the latest changes The request is hung with the latest changes (JDK 11). Could be some daemon/cache/etc issue on my machine https://github.com/jameskleeh/delay-read-stream Changing version back to 3.0.1 makes it work again |
29d2ec0
to
dcf80f3
Compare
Fixes #6100