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

Cannot read twice a response body when backed by Netty #3184

Open
thierry-st opened this issue Sep 29, 2024 · 2 comments
Open

Cannot read twice a response body when backed by Netty #3184

thierry-st opened this issue Sep 29, 2024 · 2 comments
Labels
bug Something isn't working

Comments

@thierry-st
Copy link

Describe the bug
When provided with the default Client, the following effect fails with an "IllegalStateException: Cannot connect twice":

val requestBodyAsChunk: RIO[Client, Chunk[Byte]] = ZIO.scoped {
  for {
    client <- ZIO.service[Client]
    response <- client(Request.get("http://jsonplaceholder.typicode.com/todos"))
    _ <- response.body.asChunk
    bodyAsChunk <- response.body.asChunk
  } yield bodyAsChunk
}

This is due to the zio.http.netty.AsyncBodyReader.connect method being called twice, the second time with an illegal state.

To Reproduce
Run this Scastie

Expected behaviour
The fact that the response body uses zio.http.netty.AsyncBodyReader should be an implementation detail, and calling zio.http.Body.asChunk should not be stateful.

@thierry-st thierry-st added the bug Something isn't working label Sep 29, 2024
@987Nabil
Copy link
Contributor

@kyri-petrou I think would could for the aggregation operations (asChunk/asArray) in the netty body cache the result internally? But I don't think we can do this for streaming, since it would defy the purpose of streaming it (not keeping all in memory). wdyt?

@kyri-petrou
Copy link
Collaborator

@987Nabil that's one option. Not exactly a fix, but this can also be achieved by using the batched API. The following works as expected:

val requestBodyAsChunk: RIO[Client, Chunk[Byte]] = {
  for {
    client <- ZIO.service[Client]
    response <- client.batched(Request.get("http://jsonplaceholder.typicode.com/todos"))
    _ <- response.body.asChunk
    bodyAsChunk <- response.body.asChunk
  } yield bodyAsChunk
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

3 participants