requests: socket.timeout exception

I think that exception should be handled/wrapped internally and be reraised as a RequestException-based exception.

Traceback (most recent call last):
  File "X:\dev\rotaryboard\daemon\testdaemon.py", line 117, in <module>
    res = foobar_cmd()
  File "X:\dev\rotaryboard\daemon\testdaemon.py", line 53, in foobar_cmd
    return requests.get(url, params=params, auth=('foobar', 'meow'), timeout=0.1).json()
  File "F:\Python27\lib\site-packages\requests\api.py", line 55, in get
    return request('get', url, **kwargs)
  File "F:\Python27\lib\site-packages\requests\api.py", line 44, in request
    return session.request(method=method, url=url, **kwargs)
  File "F:\Python27\lib\site-packages\requests\sessions.py", line 382, in request
    resp = self.send(prep, **send_kwargs)
  File "F:\Python27\lib\site-packages\requests\sessions.py", line 485, in send
    r = adapter.send(request, **kwargs)
  File "F:\Python27\lib\site-packages\requests\adapters.py", line 388, in send
    r.content
  File "F:\Python27\lib\site-packages\requests\models.py", line 676, in content
    self._content = bytes().join(self.iter_content(CONTENT_CHUNK_SIZE)) or bytes()
  File "F:\Python27\lib\site-packages\requests\models.py", line 615, in generate
    decode_content=True):
  File "F:\Python27\lib\site-packages\requests\packages\urllib3\response.py", line 236, in stream
    data = self.read(amt=amt, decode_content=decode_content)
  File "F:\Python27\lib\site-packages\requests\packages\urllib3\response.py", line 183, in read
    data = self._fp.read(amt)
  File "F:\Python27\lib\httplib.py", line 552, in read
    s = self.fp.read(amt)
  File "F:\Python27\lib\httplib.py", line 1288, in read
    return s + self._file.read(amt - len(s))
  File "F:\Python27\lib\socket.py", line 378, in read
    data = self._sock.recv(left)
socket.timeout: timed out

About this issue

  • Original URL
  • State: closed
  • Created 11 years ago
  • Comments: 31 (15 by maintainers)

Most upvoted comments

Okay, so what I can tell from a cursory look at the code:

  • This socket.timeout exception will only happen when the connection was successful, but it times out while reading the response from the server, ie. the server went away inbetween the original request and finishing the response retrieval. HTTP requests are usually fairly short-lived, thus this would be an uncommon scenario - that’d explain why it’s hard to reproduce.
  • The issue manifests itself in urllib3’s response.py.
  • httplib does not wrap this in a custom kind of exception, and just leaves it propagate as socket.exception. It is unclear to me whether this is intended behaviour or not.
  • To fix this, urllib3 would have to capture socket.exceptions on .read()/.recv() calls of the socket (via httplib), and re-raise them as a urllib3 exception of some sort (I’m unfamiliar with the internals of urllib3).

I will also cross-post this to shazow/urllib3#297.