Skip to content

Commit

Permalink
fix: close http connection when request throws exception (#23)
Browse files Browse the repository at this point in the history
  • Loading branch information
bgiori authored Jun 9, 2023
1 parent 8733729 commit 40c4dbb
Showing 1 changed file with 8 additions and 3 deletions.
11 changes: 8 additions & 3 deletions src/amplitude_experiment/connection_pool.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import threading
import time
import logging
from typing import Any

from http.client import HTTPConnection, HTTPResponse, HTTPSConnection
Expand Down Expand Up @@ -28,9 +29,13 @@ def __exit__(self, *exit_info: Any) -> None:
self.pool.release(self)

def request(self, *args: Any, **kwargs: Any) -> HTTPResponse:
self.conn.request(*args, **kwargs)
self.response = self.conn.getresponse()
return self.response
try:
self.conn.request(*args, **kwargs)
self.response = self.conn.getresponse()
return self.response
except Exception as e:
self.close()
raise e

def close(self) -> None:
self.conn.close()
Expand Down

0 comments on commit 40c4dbb

Please # to comment.