jersey icon indicating copy to clipboard operation
jersey copied to clipboard

ApacheConnector could throw ConnectionClosedException when using httpclient: 4.5.1+ with chunked transfer encoding

Open patriziomunzi opened this issue 8 years ago • 10 comments

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 avatar Jul 28 '17 09:07 patriziomunzi

@patriziomunzi Have you found any workaround for this issue? Do you know why this isn't being prioritized?

buchireddy avatar Feb 05 '18 20:02 buchireddy

@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()...

mpotociar avatar Feb 06 '18 22:02 mpotociar

There is a plan to release Jersey 2.27 soon. We could be able to switch the two lines before that.

jansupol avatar Feb 07 '18 12:02 jansupol

@jansupol Supercool!! :)

mpotociar avatar Feb 07 '18 17:02 mpotociar

@jansupol that's great news. Is there any approximate estimate for the release of 2.27?

steve-todorov avatar Feb 11 '18 03:02 steve-todorov

We do not have any estimate, but my assumption is during March.

jansupol avatar Feb 12 '18 20:02 jansupol

Thanks @jansupol! I'll be keeping an eye for when it's out. :)

steve-todorov avatar Feb 12 '18 21:02 steve-todorov

Hi,

It's almost mid-March, so I'd like to check, what the status is...?

carlspring avatar Mar 10 '18 23:03 carlspring

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

jansupol avatar Mar 21 '18 10:03 jansupol

Are there any other workarounds (or working fixes) available for this problem? I'm also running into this..

seeebiii avatar Apr 11 '18 12:04 seeebiii