hackney icon indicating copy to clipboard operation
hackney copied to clipboard

Fix intermittent chunked response hang

Open danielfinke opened this issue 1 year ago • 4 comments

When streaming a chunked response, it is possible to cause a TCP receive hang under particular circumstances. If at one point the parser buffer doesn't have the whole chunk, at a later point the buffer ends up empty <<>>, and subsequently hackney_response:stream_body/1 is called, hackney_response:recv/2 will hang if the expected remaining size exceeds the remainder of the response. That expected size is actually stale, from the earlier point when the parser did not have the whole chunk. This issue slipped in with benoitc/hackney#710.

This was identified when using https://github.com/benoitc/couchbeam and sending several chunked requests. If the last non-terminating chunk completed the response JSON object, hackney_response:skip_body/1 is called to discard the remaining body, but is told to receive a number of bytes equal to the expected remaining size which will frequently exceed the small terminating chunk and trailers. As a result, the recv operation hangs waiting for bytes that will never arrive.

Now, the transfer state (BufSize/ExpectedSize) are reset after each successful chunk. The speed benefit of benoitc/hackney#710 is retained (tested with the same approach as in that PR).

  • correct some related typespecs

danielfinke avatar Sep 06 '24 03:09 danielfinke

@benoitc this has been working well for us lately. Curious if you have a chance to review?

jamesaimonetti avatar Sep 17 '24 18:09 jamesaimonetti

on it sorry for the delay.

benoitc avatar Oct 01 '24 13:10 benoitc

@benoitc hey there, anything needed from our end? I double-checked and I see the same EUnit failures on master as on this branch, so I don't believe they are a result of my changes.

danielfinke avatar Oct 29 '24 22:10 danielfinke

@benoitc any news on this fix? This one together with #744 would be good content for a new release :)

NelsonVides avatar Aug 21 '25 11:08 NelsonVides