http-client icon indicating copy to clipboard operation
http-client copied to clipboard

Flush the connectionRead buffer before throwing connectionClosed.

Open junjihashimoto opened this issue 6 months ago • 4 comments

When the response-data has already been read into the buffer, the connection is closed, and the data already read is sufficient, an exception is thrown and the data cannot be read.

In my case, I am using http-client in haskell/servant. The program can not read response data using getResponse and makeLengthReader. I think https://github.com/haskell-servant/servant/issues/994 might be related, but it's closed for some reason and I'm not sure how to fix it.

This PR flushes the connectionRead buffer before throwing connectionClosed.

junjihashimoto avatar May 25 '25 06:05 junjihashimoto

When the response-data has already been read into the buffer, the connection is closed

Are you saying that after

  1. connectionRead
  2. connectionUnread
  3. connectionClose

you expect a subsequent connectionRead to succeed?

If yes, then I'm not convinced that this is "reasonable" to expect.

I think a better solution might be to identify where and when connectionClose was called prematurely, and addressed this.

sol avatar Jun 03 '25 08:06 sol

you expect a subsequent connectionRead to succeed?

Yes.

In my case, the HTTP response has an HTTP header and body. After parsing the header, connectionUnread is called and the body is returned to the stack. After that, the body needs to be read with connectionRead, but since the connection has already been closed, an exception is thrown and the body remains unread. This series of processes cannot be controlled from the application.

junjihashimoto avatar Jun 03 '25 08:06 junjihashimoto

but since the connection has already been closed, an exception is thrown and the body remains unread

Do you know where the connection is closed?

sol avatar Jun 03 '25 09:06 sol

After rerunning it with +RTS -xc, share the stack trace.

junjihashimoto avatar Jun 03 '25 09:06 junjihashimoto