Skip to content

Commit 11c2bfb

Browse files
committed
Fix socket leak when exception is raised in request.
Added __enter__ and __exit__ to Response for as with context manager.
1 parent 86c2984 commit 11c2bfb

File tree

1 file changed

+7
-1
lines changed

1 file changed

+7
-1
lines changed

adafruit_esp32spi/adafruit_esp32spi_requests.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,12 @@ def __init__(self, sock):
6969
self._read_so_far = 0
7070
self.headers = {}
7171

72+
def __enter__(self):
73+
return self
74+
75+
def __exit__(self, exc_type, exc_value, traceback):
76+
self.close()
77+
7278
def close(self):
7379
"""Close, delete and collect the response data"""
7480
if self.socket:
@@ -216,7 +222,7 @@ def request(method, url, data=None, json=None, headers=None, stream=False):
216222
elif line.startswith(b"Location:") and not 200 <= status <= 299:
217223
raise NotImplementedError("Redirects not yet supported")
218224

219-
except OSError:
225+
except:
220226
sock.close()
221227
raise
222228

0 commit comments

Comments
 (0)