Repository navigation
simple_httpclient: Fix timeouts for requests waiting in the queue - #3810
Om-singhaI wants to merge 4 commits into
Conversation
When max_clients is reached, _HTTPConnection started its timers when it was created, so the request_timeout of a queued request restarted when it left the queue and the request could run for up to twice the request_timeout. The queue timer also used connect_timeout, so a queued request could fail before it ever opened a connection. Record the time a request enters the queue in fetch_impl and pass it to _HTTPConnection. The queue timeout now uses only request_timeout, request_timeout is measured from the time the request was queued, and connect_timeout is measured from the start of the connection. This reverses the 3.2 behavior from tornadoweb#885 that applied connect_timeout to queued requests. A queued request with request_timeout=0 now waits in the queue until a connection is free. Fixes tornadoweb#1753.
bdarnell
left a comment
There was a problem hiding this comment.
Thank you for researching and pulling up #885. I no longer believe what I said in that issue, that "The key difference between connect_timeout and request_timeout is that the former allows the request to be aborted before any potentially-expensive server-side work has begun" (I'm not sure I really believed it then, either, since we never had good support for cancellation).
My philosophy about the two timeouts is that request_timeout is about how long the caller is willing to wait, and is all-inclusive. connect_timeout is about a portion of the process that has relatively well-understood time characteristics (the connection phase is a handful of network round trips for the TCP and TLS handshakes without incurring significant CPU or other request-dependent costs).
In #3692 we fixed timeout support for redirects, so we need to make sure the two timeouts interact with redirects in a way that makes sense. I think that for request_timeout, we want to carry the original start time forward through the chain of redirects so that we guarantee the caller a response in no more than approximately request_timeout seconds. Each redirect gets its own fresh connect_timeout.
I think the code looks good but let's add a redirect test and make sure the start time doesn't reset.
A redirect goes through fetch_impl again, which recorded a new start time, so the request_timeout restarted on every redirect. Carry the original start time on the redirected request, so the request_timeout covers the whole chain of redirects, including any time a redirect spends waiting in the queue. Each connection still gets its own connect_timeout. The start time is now a private attribute of the connection, so it is not added to the API of WebSocketClientConnection.
|
@bdarnell Thanks, that makes sense. I merged master in and added a commit that carries the original start time through redirects, so request_timeout counts from the first request for the whole chain, including time a redirect waits in the queue. Each redirect still gets a fresh connect_timeout. New tests cover a slow chain of redirects, a redirect stuck in the queue, and a late redirect that still connects. |
| queue_start_time = self.io_loop.time() | ||
| # A redirect keeps the start time of the original request, so the | ||
| # request_timeout covers the whole chain of redirects. | ||
| queue_start_time: float | None = getattr(request, "_queue_start_time", None) |
There was a problem hiding this comment.
I think we could make this an optional keyword-only argument to fetch_impl instead of smuggling it through an attribute of the request.
There was a problem hiding this comment.
Done. The start time is now an optional keyword only argument of fetch_impl, and the redirect in finish() passes it explicitly, so the attribute on the request is gone. The API snapshot is updated for the new signature.
Instead of setting an attribute on the redirected request, finish() now passes the original start time to fetch_impl as an optional keyword only argument. Behavior is unchanged: the request_timeout still covers the whole chain of redirects, and each connection still gets its own connect_timeout. Update the API snapshot for the new fetch_impl signature.
Fixes #1753.
When
max_clientsis reached,_HTTPConnectionstarts its timers from its own creation time, sorequest_timeoutrestarts when a request leaves the queue and the request can run for up to twice itsrequest_timeout. The queue timer also usesmin(connect_timeout, request_timeout), so a request with a smallconnect_timeoutfails with "Timeout in request queue" before it ever opens a connection.This follows the fix described in the issue: "capture the start time when the request is created (in SimpleAsyncHTTPClient.fetch_impl) and pass it in to the connection class". The queue timer now only uses
request_timeout,request_timeoutis measured from the queue start, andconnect_timeoutfrom the start of the connection.response.request_timestill excludes queue time. The newqueue_start_timeargument is optional, so_HTTPConnectionsubclasses such as the websocket client andtornado_http2keep working.This is a behavior change. Applying
connect_timeoutin the queue was added on purpose in #885 and listed in the 3.2 release notes; this follows the later comment here that queue time should count only againstrequest_timeout. Code that used a shortconnect_timeoutto drop requests stuck in the queue will now wait up torequest_timeout, and a queued request withrequest_timeout=0never times out in the queue. If you would rather keep the 3.2 behavior, I can drop that part and keep only the restart fix.With
max_clients=1and a handler that takes 1 second:request_timeout=1.5connect_timeout=0.2, request_timeout=10I changed
test_queue_timeoutto userequest_timeoutand added two tests toSimpleHTTPClientTestMixin, one for each half. With the fix reverted, all four new test runs (HTTP and HTTPS) fail.simple_httpclient_test(141 tests),httpclient_test(65) andwebsocket_testwithtcpclient_test(114) pass, as do the commands fromtox -e lintandtox -e docs.