From 3155bc99362a8c6ab136b6a3bb999732617cd2e5 Mon Sep 17 00:00:00 2001 From: Ian Cordasco Date: Fri, 7 Nov 2014 08:55:17 -0600 Subject: [PATCH] Close sessions created in the functional API This is related to #1882 and #1685. By calling close on the session, we clear the PoolManager operated by the Session and close all sockets. Fixes #1882 Partially-fixes #1685 --- requests/api.py | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/requests/api.py b/requests/api.py index 4eaaf9e6..1469b05c 100644 --- a/requests/api.py +++ b/requests/api.py @@ -46,7 +46,12 @@ def request(method, url, **kwargs): """ session = sessions.Session() - return session.request(method=method, url=url, **kwargs) + response = session.request(method=method, url=url, **kwargs) + # By explicitly closing the session, we avoid leaving sockets open which + # can trigger a ResourceWarning in some cases, and look like a memory leak + # in others. + session.close() + return response def get(url, **kwargs):