Skip to content

Fix implementation of available() in sockets - #694

Merged
flooey merged 1 commit into
google:masterfrom
flooey:engineavailable
Aug 2, 2019
Merged

flooey merged 1 commit into
google:masterfrom
flooey:engineavailable

Conversation

@flooey

@flooey flooey commented Aug 2, 2019

Copy link
Copy Markdown
Contributor

The file descriptor socket used the default implementation of
available(), which just always returns 0. We can at least return the
pending bytes in the plaintext buffer, since those are definitely
available.

The engine socket had two issues. First, it used
fromSocket.remaining(), but fromSocket was in a writing posture, so it
always had a high return from remaining() regardless of whether there
was pending readable data in the buffer. This was the cause of #433:
a BufferedReader would read some data, then check available() to see
if it could freely fill some more data without blocking. The engine
socket would report that it could, so it would try to read that data
and then unexpectedly block.

The second issue is that incoming socket data doesn't necessarily
imply readable plaintext data: it might be a partial TLS record, a
handshake or other management record, etc. So we should only report
plaintext data that's actually been successfully decrypted from
available().

Fixes #433

The file descriptor socket used the default implementation of
available(), which just always returns 0.  We can at least return the
pending bytes in the plaintext buffer, since those are definitely
available.

The engine socket had two issues.  First, it used
fromSocket.remaining(), but fromSocket was in a writing posture, so it
always had a high return from remaining() regardless of whether there
was pending readable data in the buffer.  This was the cause of google#433:
a BufferedReader would read some data, then check available() to see
if it could freely fill some more data without blocking.  The engine
socket would report that it could, so it would try to read that data
and then unexpectedly block.

The second issue is that incoming socket data doesn't necessarily
imply readable plaintext data: it might be a partial TLS record, a
handshake or other management record, etc.  So we should only report
plaintext data that's actually been successfully decrypted from
available().

Fixes google#433
@flooey
flooey requested a review from prbprbprb August 2, 2019 09:12
@flooey
flooey merged commit 7d2a092 into google:master Aug 2, 2019
@flooey
flooey deleted the engineavailable branch August 2, 2019 10:10
@carterkozak

Copy link
Copy Markdown

Great explanation in the PR description, thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

HttpsUrlConnections hang (then respond correctly) when setUseEngineSocket is true

3 participants