ApacheConnector could throw ConnectionClosedException when using httpclient: 4.5.1+ with chunked transfer encoding
As per the comment here: https://github.com/jersey/jersey/commit/0d1326dee69d9c693014184bea3e4a44b323daee#diff-91ece5335d0cdc9321a099df1caec54fR640
This change does not work with the org.apache.httpcomponents: httpclient: 4.5.1+ when using chunked transfer encoding. This is because they have changed the way they close the connection. In this case when response.close() is called the connection is closed and the input stream's buffer is cleared, then super.close() function is not able to close the ChunkedInputStream and results in org.apache.http.ConnectionClosedException: Premature end of chunk coded message body: closing chunk expected. The reason of the exception occurring is that upon close of ChunkedInputStream, the close() function reads the remainder of the chunked message and it fails because the buffer of the stream has been cleared when the connection has been closed in response.close() call. Reference: https://hc.apache.org/httpclient-3.x/apidocs/org/apache/commons/httpclient/ChunkedInputStream.html#close() The fix to this problem is putting super.close() line of code before the response.close(). This allows for the stream to be closed correctly before its buffer has been cleared.
The ApacheConnector could throw ConnectionClosedException: Premature end of chunk coded message body: closing chunk expected when using httpclient: 4.5.1+ with chunked transfer encoding.
@patriziomunzi Have you found any workaround for this issue? Do you know why this isn't being prioritized?
@jansupol is this something you can fix for us guys? If not, would you be able to release new Jersey if we provide a patch?
FWIW, the suggested change that should fix the issue seems minimal - just switch the lines 658 & 659 in ApacheConnector, so that super.close() is called before response.close()...
There is a plan to release Jersey 2.27 soon. We could be able to switch the two lines before that.
@jansupol Supercool!! :)
@jansupol that's great news. Is there any approximate estimate for the release of 2.27?
We do not have any estimate, but my assumption is during March.
Thanks @jansupol! I'll be keeping an eye for when it's out. :)
Hi,
It's almost mid-March, so I'd like to check, what the status is...?
Switching the lines makes the tests to hang, jstack output:
at java.net.SocketInputStream.socketRead0(Native Method) at java.net.SocketInputStream.socketRead(SocketInputStream.java:116) at java.net.SocketInputStream.read(SocketInputStream.java:171) at java.net.SocketInputStream.read(SocketInputStream.java:141) at org.apache.http.impl.io.SessionInputBufferImpl.streamRead(SessionInputBufferImpl.java:139)
It might be a bug in the tests, but not sure we have time to spend on this before the release
Are there any other workarounds (or working fixes) available for this problem? I'm also running into this..