overall timeout
We already make great use of the timeout parameter which allows setting per TCP transaction timeouts. This is very helpful! However, we also need to support an overall timeout across the connection. Reading the docs on timeouts I see this isn't currently supported, and searching through the issues at least a bit back I didn't see another request for this feature -- excuse me if there is.
I realize we can set timers in our library to accomplish this, but I'm concerned about the additional overhead (one per thread, and we may have many) as well as any adverse effects to connection pooling if we end up needing to abort a request. Is there a good way to abort a request in the first place? I didn't see anything obvious in the docs.
So: Long term, it would be great if we could add overall timeout to the requests library. Short term, is there a recommended way of implementing this on my end?
Hi @emgerner-msft,
For reference, the following are all variations on this theme if not this exact feature request:
- https://github.com/kennethreitz/requests/issues/2327
- https://github.com/kennethreitz/requests/issues/2685
- https://github.com/kennethreitz/requests/issues/1928
- (and I'm sure there are more)
We've also discussed this over on https://github.com/sigmavirus24/requests-toolbelt/issues/51
You'll notice the last link discusses this package which should handle this for you without adding it to requests. The reality, is that there's no need for requests to do this when another package already does it very well.
The package you reference does it by forking a separate process to run the web request. That is a very heavyweight way of achieving the simple goal of a timeout, and in my view is not in any way a substitute for requests itself having a native timeout feature.
@jribbens If you can come up with a way that uses neither threads nor processes, that would be amazing. Until then, if you want a wall clock timeout your best bet is that package as it's the most reliable way of achieving that at the moment.
I don't think @jribbens is saying no threads nor processes. Just that a process per web request is excessive. Many languages have a way of multiple timers sharing a single additional thread or process. I'm just not aware of how to do that best in Python.
It seems like #1928 has the most discussion of alternatives, but most come with a lot of caveats (this won't work for your use case, etc). I'm fine with having some custom code in my library and writing my own custom solution if this really doesn't belong in requests but I think I need a little more information on what that would look like. The whole reason we use requests is to get away from the low level TCP connection pooling logic but it seems like reading that thread that in order to write this custom code I need to know that logic, and that's what I'm having some trouble with.
@emgerner-msft is correct. I am bit confused by @sigmavirus24's comment, having a "total timeout" without using threads or processes seems quite pedestrian and not at all "amazing". Just calculate the deadline at the start of the whole process (e.g. deadline = time.time() + total_timeout) and then on any individual operation set the timeout to be deadline - time.time().
having a "total timeout" without using threads or processes seems quite pedestrian and not at all "amazing".
And your solution is rather primitive. The reason most people want a total (or wall clock) timeout is to prevent a read from "hanging", in other words a case like the following:
r = requests.get(url, stream=True)
for chunk in r.iter_content(chunksize):
process_data(chunk)
Where each read takes a long time in the middle of iter_content but it's less than the read timeout (I'm assuming we apply that when streaming, but it still may be the case that we don't) they specified. Certainly it would seem like this should be simply handled by your solution @jribbens until you remember how clocks drift and daylight savings time works and them time.time() is woefully insufficient.
Finally, it's important to keep in mind that Requests' API is frozen. There is no good or consistent API for specifying a total timeout. And if we implemented a timeout like you suggest, we would have countless bugs that they specified a one minute long total timeout but it took longer because the last time we checked we were under a minute but their configured read timeout was long enough that their timeout error was raised around a minute and a half. That's a very rough wall timeout that would be slightly better for people looking for this, but no different from the person implementing this themselves.
Apologies if I was unclear @sigmavirus24 , you seem to have critiqued my pseudocode illustration of principle as if you thought it was a literal patch. I should point out though that time.time() does not work the way you apparently think - daylight savings time is not relevant, and neither is clock skew on the timescales we're talking about here. Also you have misunderstood the suggestion if you think the bug you describe would occur. Finally I am not sure what you mean by the Requests API being "frozen" as the API was changed as recently as version 2.9.0 so clearly whatever you mean it's not what I would normally understand by the word.
Just to separate my discussion: I'm actually not arguing this is easy. If it were totally simple, I would just write it and stop bugging you. :)
My problems are:
- Everything on the threads you listed was monkey patches. That's fine, but I'm using this in a production quality library and can't take the caveat of internal changes breaking everything.
- The timeout decorator in the link you gave is great, but I'm not clear on how that affects the connection. Even if we accept that the only good way of doing timeouts is with a bunch of threads, how does this library enforce that the socket gets shut down, the connection dropped, etc. We're doing a lot of connections and this seems potentially quite leak prone. requests doesn't have an 'abort' method that I can find (correct me if I'm wrong) so how is the shutdown of the connection happening?
All I'm looking for is a clear 'blessed' version of how to solve this problem on my own, or if there's not a perfect solution, a couple solutions with the caveats discussed. Does that make sense?
@emgerner-msft Assuming you're using CPython, connection shutdown will happen when the request is no longer continuing. At that point all references to the underlying connection will be lost and the socket will be closed and disposed of.
@Lukasa Okay, thanks! How does the library determine the request is no longer continuing? For example, if I used the timeout decorator route and cut off in the middle of the download, when would the download actually stop? Do I need to do anything special with the streaming options?
If you use the timeout decorator, the download will stop when the timeout fires. This is because signals interrupt syscalls, which means that there will be no further calls into the socket. Once the request is no longer in scope (e.g. the stack has unwound to outside of your requests.* function), that's in: CPython will clean up the connection object and tear the connection down. No special streaming options are required there.
Perfect. I'm good to close the thread then, unless others have more to say.
Actually, sorry, one more concern. Was looking at the timeout decorator code more closely since you said that it uses signals was relevant, as opposed to something like Python Timers (presumably). It looks like it calls signal with SIGALRM which is documented in Python Signal not to work on Windows. I need this to work in both Unix and Windows environments, as well as in Python 2.7 and 3.3+ (much like requests itself). I'll poke around a bit more and see if this will actually work given that.
@emgerner-msft That's frustrating. =(
@Lukasa Yup, tried the basic usage snippet and it doesn't work on Windows. I read some more of the code/examples and fiddled and it looks like if we don't use signals the package might work, but everything has to be pickable which is not the case for my application. So as far as I can tell, timeout decorator won't solve my problem. Any other ideas?
@emgerner-msft Are you confident that none of the Windows-specific signals are suitable?
@Lukasa To be blunt, I simply don't know. I haven't used signals before, and much like I didn't realize until you told me that they'd interrupt the request I'm not sure what's appropriate. I'm also not trying to get this just to work on Windows. I need full crossplat support (Windows and Unix) and both Python 2 and Python 3 support. So much of signals looks platform specific it's throwing me. Timer was one of the solutions I was looking at that looked less low level and thus might take care of my constraints, but I'm not sure then how I could close the connection. I can do more reading, but this is why I was hoping to get additional guidance from you guys. :)
So this is a really tricky place to be.
The reality is that there is more-or-less no cross-platform way to kill a thread except by interrupting it, which is basically what a signal is. That means, I think, that signals are the only route you really have to making this work across platforms. I'm inclined to try to ping in a Windowsy Pythony expert: @brettcannon, do you have a good suggestion here?
Out of interest, is there a reason to not implement "total timeout" in Requests other than that implementing and testing it requires work? I mean, if a patch to implement it magically appeared today would it in theory be rejected or accepted? I appreciate and agree with the "eliminate unnecessary complexity" point of view, but "you can do it by forking a separate process" does not make this feature unnecessary in my opinion.
@jribbens There are a few problems with this.
Part 1 is that the complexity of such a patch is very high. To get it to behave correctly you need to repeatedly change timeouts at the socket level. This means that the patch needs to be passed pervasively though httplib, which we've already patched more than we'd like to. Essentially, we'd need to be reaching into httplib and reimplementing about 50% of its more complex methods in order to achieve this functional change.
Part 2 is that the maintenance of such a patch is relatively burdensome. We'd likely need to start maintaining what amounts to a parallel fork of httplib (more properly http.client at this time) in order to successfully do it. Alternatively, we'd need to take on the maintenance burden of a different HTTP stack that is more amenable to this kind of change. This part is, I suspect, commonly missed by those who wish to have such a feature: the cost of implementing it is high, but that is nothing compared to the ongoing maintenance costs of supporting such a feature on all platforms.
Part 3 is that the advantage of such a patch is unclear. It has been my experience that most people who want a total timeout patch are not thinking entirely clearly about what they want. In most cases, total timeout parameters end up having the effect of killing perfectly good requests for no reason.
For example, suppose you've designed a bit of code that downloads files, and you'd like to handle hangs. While it's initially tempting to want to set a flat total timeout ("no request may take more than 30 seconds!"), such a timeout misses the point. For example, if a file changes from being 30MB to being 30GB in size, such a file can never download in that kind of time interval, even though the download may be entirely healthy.
Put another way, total timeouts are an attractive nuisance: they appear to solve a problem, but they don't do it effectively. A more useful approach, in my opinion, is to take advantage of the per-socket-action timeout, combined with stream=True and iter_content, and assign yourself timeouts for chunks of data. The way iter_content works, flow of control will be returned to your code in a somewhat regular interval. That means that you can set yourself socket-level timeouts (e.g. 5s) and then iter_content over fairly small chunks (e.g. 1KB of data) and be relatively confident that unless you're being actively attacked, no denial of service is possible here. If you're really worried about denial of service, set your socket-level timeout much lower and your chunk size smaller (0.5s and 512 bytes) to ensure that you're regularly having control flow handed back to you.
The upshot of all this is that I believe that total timeouts are a misfeature in a library like this one. The best kind of timeout is one that is tuned to allow large responses enough time to download in peace, and such a timeout is best served by socket-level timeouts and iter_content.