-
Notifications
You must be signed in to change notification settings - Fork 517
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
Streaming body issue with a synchronous ring handler #491
Comments
Initially I thought this was a bug, but now that I've investigated it more fully, I don't believe it is. The reason this doesn't work with synchronous handlers is because the adapter expects the This seems like reasonable behavior to have, as synchronous functions have guarantees that asynchronous ones do not. A thread is assigned to the handler, and then to write the body stream, and then given back up to the pool. There's no instance where the body stream could be left open without also blocking the request thread. Going back to the handler function you gave as an example, we could rewrite this to work correctly just by removing the My current thinking is that it's better to keep the guarantees that synchronous, blocking I/O give us. Particularly if Java's virtual threads allow us to park a thread for relatively little cost in future. |
I think I understand your point, this seems simpler and safer. I'm re-reading the doc string of the protocol now: (write-body-to-stream [body response output-stream]
"Write a value representing a response body to an output stream. The stream
will be closed after the value had been written. The stream may be written
asynchronously.") In retrospect, maybe "after the value has been written" means "after |
Yes, I think we want to update the protocol docstring to explain the use-case more clearly. |
Back to servlet spec, it does not allow async thread to write to |
There is an issue with streaming responses when using a synchronous ring handler. Consider the following bare-bone SSE handler:
(Tested with Clojure 1.12.0-alpha5 and Java 21)
Calling the async server with curl yields the expected result, streaming a message every 100ms and finally closing:
But calling the synchronous server returns instantly without displaying any of the "data: x" messages.
The response should behave the same way whether the
:async true
setting is present or not.(This issue was originally encountered while reporting another issue in the ring-jetty9-adapter project)
The text was updated successfully, but these errors were encountered: