Skip to content

Fix handling of timed out connections kept alive in connection pool under Unix#762

Merged
ras0219-msft merged 5 commits intomicrosoft:masterfrom
vadz:fix-asio-conn-timeout
Aug 3, 2018
Merged

Fix handling of timed out connections kept alive in connection pool under Unix#762
ras0219-msft merged 5 commits intomicrosoft:masterfrom
vadz:fix-asio-conn-timeout

Conversation

@vadz
Copy link
Copy Markdown
Contributor

@vadz vadz commented May 16, 2018

This fixes #592 by ensuring that we actually reopen the connection if the existing one, obtained from the connection pool, timed out in the meanwhile.

Note that the existing code already tried to do it, but failed due to 2 bugs in it: first, it didn't detect the timeouts properly for HTTPS connections and, second, it didn't reinitialize the request input stream, meaning that even if the connection was reopened, the correct request wasn't sent over it.

Other improvements remain possible (and desirable), but this PR at least makes things work, albeit not in the most efficient way, whereas they just don't currently, so I think it should already be applied and the rest done later.

vadz and others added 5 commits May 8, 2018 17:05
Reduce code duplication between ssl_proxy_tunnel::handle_status_line()
and the method with the same name in asio_context itself by moving the
common handling of connections closed by server into a new function.

No real changes, this is a pure refactoring.
When an SSL connection times out due to being closed by server, a
different error code is returned, so we need to check for it too in
was_closed_by_server().

Without this, losing connection was not detected at all when using
HTTPS, resulting in "Failed to read HTTP status line" errors whenever
the same http_client was reused after more than the server keep alive
timeout of inactivity.

See microsoft#592.
Creating a new request when the existing connection being used was
closed by the server didn't work correctly because the associated input
stream was already exhausted, as its contents had been already "sent" to
the server using the now discarded connection, so starting a new request
using the same body stream never worked.

Fix this by explicitly rewinding the stream to the beginning before
using it again.

Note that even with this fix using a custom body stream which is not
positioned at the beginning initially (or which doesn't support
rewinding) still wouldn't work, but at least it fixes the most common
use case.

See microsoft#592.
…ad_status_line.

Avoid increasing public surface with rewind function.
@ras0219-msft ras0219-msft merged commit 5021303 into microsoft:master Aug 3, 2018
@ras0219-msft
Copy link
Copy Markdown
Contributor

Agreed. Thanks for the PR and sorry for the long delay!

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.

http_client does not gracefully handle remotely-closed keepalive connections (Linux/ASIO)

2 participants