Skip to content

Commit 6ec44b1

Browse files
Hans WeltarKarsten Verelst
Hans Weltar
authored and
Karsten Verelst
committed
Add close method to app
Fixes Issue #370 Session/socket never closed, warnings shown The msal application does not close its resources like eg: self.http_client. As a result a 'ResourceWarning unclosed <ssl.SSLSocket' is shown when the app goes out of scope (when warnings are enabled) This fix adds and explicit close method so developers can cleanly close the app if they so desire. In __init__ we trap and close the exception explicitely as users cannot call close when __init__ fails. Finally we don't close the http_client if it's externally managed.
1 parent cb88462 commit 6ec44b1

File tree

1 file changed

+26
-11
lines changed

1 file changed

+26
-11
lines changed

msal/application.py

+26-11
Original file line numberDiff line numberDiff line change
@@ -281,7 +281,9 @@ def __init__(
281281

282282
if http_client:
283283
self.http_client = http_client
284+
self.internal_client = False
284285
else:
286+
self.internal_client = True
285287
self.http_client = requests.Session()
286288
self.http_client.verify = verify
287289
self.http_client.proxies = proxies
@@ -301,7 +303,24 @@ def __init__(
301303

302304
# Here the self.authority will not be the same type as authority in input
303305
try:
304-
self.authority = Authority(
306+
self.authority = self._build_authority(
307+
authority, validate_authority, azure_region)
308+
except Exception:
309+
self.close()
310+
raise
311+
312+
self.token_cache = token_cache or TokenCache()
313+
self._region_configured = azure_region
314+
self._region_detected = None
315+
self.client, self._regional_client = self._build_client(
316+
client_credential, self.authority)
317+
self.authority_groups = None
318+
self._telemetry_buffer = {}
319+
self._telemetry_lock = Lock()
320+
321+
def _build_authority(authority, validate_authority, azure_region):
322+
try:
323+
return Authority(
305324
authority or "https://login.microsoftonline.com/common/",
306325
self.http_client, validate_authority=validate_authority)
307326
except ValueError: # Those are explicit authority validation errors
@@ -310,21 +329,12 @@ def __init__(
310329
if validate_authority and azure_region:
311330
# Since caller opts in to use region, here we tolerate connection
312331
# errors happened during authority validation at non-region endpoint
313-
self.authority = Authority(
332+
return Authority(
314333
authority or "https://login.microsoftonline.com/common/",
315334
self.http_client, validate_authority=False)
316335
else:
317336
raise
318337

319-
self.token_cache = token_cache or TokenCache()
320-
self._region_configured = azure_region
321-
self._region_detected = None
322-
self.client, self._regional_client = self._build_client(
323-
client_credential, self.authority)
324-
self.authority_groups = None
325-
self._telemetry_buffer = {}
326-
self._telemetry_lock = Lock()
327-
328338
def _decorate_scope(
329339
self, scopes,
330340
reserved_scope=frozenset(['openid', 'profile', 'offline_access'])):
@@ -1297,6 +1307,11 @@ def _acquire_token_by_username_password_federated(
12971307
)),
12981308
**kwargs)
12991309

1310+
def close(self):
1311+
"""Close the app and any open sockets"""
1312+
if self.internal_client:
1313+
self.http_client.close()
1314+
13001315

13011316
class PublicClientApplication(ClientApplication): # browser app or mobile app
13021317

0 commit comments

Comments
 (0)