address connection leak issue from #520

* prefetch now defaults to True, ensuring that by default, sockets
  are returned to the urllib3 connection pool on request end
* sessions now have a close() method, notifying urllib3 to close pooled
  connections
* the module-level API, e.g., `requests.get('http://www.google.com')`,
  explicitly closes its session when finished

When prefetch is False, the open socket becomes part of the state of the
Response object, and it's the client's responsibility to read the whole
body, at which point the socket will be returned to the pool.
This commit is contained in:
Shivaram Lingamneni
2012-07-17 19:47:13 -07:00
parent 9abc9ad1b3
commit 3c0b94047c
5 changed files with 59 additions and 14 deletions
+26
View File
@@ -0,0 +1,26 @@
"""
This is an informal test originally written by Bluehorn;
it verifies that Requests does not leak connections when
the body of the request is not read.
"""
import gc, os, subprocess, requests, sys
def main():
gc.disable()
for x in range(20):
requests.head("http://www.google.com/")
print("Open sockets after 20 head requests:")
pid = os.getpid()
subprocess.call("lsof -p%d -a -iTCP" % (pid,), shell=True)
gcresult = gc.collect()
print("Garbage collection result: %s" % (gcresult,))
print("Open sockets after garbage collection:")
subprocess.call("lsof -p%d -a -iTCP" % (pid,), shell=True)
if __name__ == '__main__':
sys.exit(main())
+4 -4
View File
@@ -807,9 +807,9 @@ class RequestsTestSuite(TestSetup, TestBaseMixin, unittest.TestCase):
assert 'k' in c
ds1 = pickle.loads(pickle.dumps(requests.session()))
ds2 = pickle.loads(pickle.dumps(requests.session(prefetch=True)))
assert not ds1.prefetch
assert ds2.prefetch
ds2 = pickle.loads(pickle.dumps(requests.session(prefetch=False)))
assert ds1.prefetch
assert not ds2.prefetch
# def test_invalid_content(self):
# # WARNING: if you're using a terrible DNS provider (comcast),
@@ -858,7 +858,7 @@ class RequestsTestSuite(TestSetup, TestBaseMixin, unittest.TestCase):
)
# Make a request and monkey-patch its contents
r = get(httpbin('get'))
r = get(httpbin('get'), prefetch=False)
r.raw = StringIO(quote)
lines = list(r.iter_lines())