Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

AttributeError: 'NoneType' object has no attribute 'settimeout' when using requests.session and connection gets closed #48

Closed
msabramo opened this issue Nov 26, 2013 · 5 comments

Comments

@msabramo
Copy link
Contributor

The code that VCRpy has for removing and setting sock to None (in order to work around some behavior of requests apparently) causes problems if you:

  1. Open a requests.session
  2. Issue a request that results in the connection being closed
  3. Issue another request

The result of these steps is an exception being raised: AttributeError: 'NoneType' object has no attribute 'settimeout'.

Using the code in this gist: https://gist.github.com/msabramo/7664462

(py27.venv)marca@marca-mac2:~/dev/git-repos/vcrpy$ rm test_vcr.yaml; python vcr_py_issue_48.py
Doing request #1...
Got response #1; headers = CaseInsensitiveDict({'content-length': '381', 'server': 'gunicorn/0.17.4', 'connection': 'Close', 'date': 'Tue, 26 Nov 2013 19:22:12 GMT', 'access-control-allow-origin': '*', 'content-type': 'application/json'})
Doing request #2...
Traceback (most recent call last):
  File "vcr_py_issue_48.py", line 12, in <module>
    resp = session.post('http://httpbin.org/post', data={}, headers={'Connection': 'close'})
  File "/Users/marca/dev/git-repos/vcrpy/py27.venv/lib/python2.7/site-packages/requests/sessions.py", line 403, in post
    return self.request('POST', url, data=data, **kwargs)
  File "/Users/marca/dev/git-repos/vcrpy/py27.venv/lib/python2.7/site-packages/requests/sessions.py", line 361, in request
    resp = self.send(prep, **send_kwargs)
  File "/Users/marca/dev/git-repos/vcrpy/py27.venv/lib/python2.7/site-packages/requests/sessions.py", line 464, in send
    r = adapter.send(request, **kwargs)
  File "/Users/marca/dev/git-repos/vcrpy/py27.venv/lib/python2.7/site-packages/requests/adapters.py", line 321, in send
    timeout=timeout
  File "/Users/marca/dev/git-repos/vcrpy/py27.venv/lib/python2.7/site-packages/requests/packages/urllib3/connectionpool.py", line 471, in urlopen
    body=body, headers=headers)
  File "/Users/marca/dev/git-repos/vcrpy/py27.venv/lib/python2.7/site-packages/requests/packages/urllib3/connectionpool.py", line 308, in _make_request
    conn.sock.settimeout(read_timeout)
AttributeError: 'NoneType' object has no attribute 'settimeout'
@smallcode
Copy link
Contributor

I'm having same trouble.

@msabramo
Copy link
Contributor Author

I think I might have a fix -- I will try to send a PR shortly.

msabramo added a commit to msabramo/vcrpy that referenced this issue Nov 26, 2013
@msabramo
Copy link
Contributor Author

5ba4000 is a test that illustrates the problem

marca@marca-mac2:~/dev/git-repos/vcrpy$ tox -e py26requests
...
            if read_timeout is Timeout.DEFAULT_TIMEOUT:
                conn.sock.settimeout(socket.getdefaulttimeout())
            else: # None or a value
>               conn.sock.settimeout(read_timeout)
E               AttributeError: 'NoneType' object has no attribute 'settimeout'

.tox/py26requests/lib/python2.6/site-packages/requests/packages/urllib3/connectionpool.py:308: AttributeError
=========================================================================================================================================== 2 failed, 70 passed in 23.82 seconds ===========================================================================================================================================
ERROR: InvocationError: '/Users/marca/dev/git-repos/vcrpy/.tox/py26requests/bin/python setup.py test'
_________________________________________________________________________________________________________________________________________________________ summary __________________________________________________________________________________________________________________________________________________________
ERROR:   py26requests: commands failed

@msabramo
Copy link
Contributor Author

#50 has the test and the fix. So you can merge #49 to reproduce the problem and then merge #50 to fix it.

@kevin1024
Copy link
Owner

Awesome, thanks! I'm traveling abroad on vacation right now but I will probably have a chance to check this out this weekend.

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

No branches or pull requests

3 participants