Skip to content

Commit a67742e

Browse files
committed
fix(RestClient): fixed session errors on close
1 parent e43bcc8 commit a67742e

File tree

1 file changed

+17
-5
lines changed

1 file changed

+17
-5
lines changed

ibind/base/rest_client.py

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77

88
import requests
99
from requests import ReadTimeout, Timeout
10+
from requests import ConnectionError as RequestsConnectionError
1011
from requests.exceptions import ChunkedEncodingError
1112

1213
from ibind import var
@@ -226,7 +227,10 @@ def _request(
226227
kwargs = filter_none(kwargs)
227228

228229
# choose which function should be used to make a reqeust based on use_session field
229-
request_function = self._session.request if self.use_session else requests.request
230+
if self.use_session and self._session is not None:
231+
request_function = self._session.request
232+
else:
233+
request_function = requests.request
230234

231235
if request_function is None:
232236
_LOGGER.warning(f'{self}: an attempt was made to create a request with no valid session.')
@@ -253,12 +257,14 @@ def _request(
253257

254258
continue # Continue to the next iteration for a retry
255259

256-
except (ConnectionError, ChunkedEncodingError) as e:
257-
msg = f'{self}: Connection error detected, resetting session and retrying attempt {attempt + 1}/{self._max_retries} :: {str(e)}'
260+
except (ConnectionError, RequestsConnectionError, ChunkedEncodingError) as e:
261+
if attempt >= self._max_retries:
262+
raise ExternalBrokerError(f'{self}: Connection error {str(e)} for {method} {url} {kwargs}') from e
263+
msg = f'{self}: Connection error detected, retrying attempt {attempt + 1}/{self._max_retries} :: {str(e)}'
258264
self.logger.warning(msg)
259265
_LOGGER.warning(msg)
260-
self.close()
261266
if self.use_session:
267+
self.close_session()
262268
self.make_session() # Recreate session automatically
263269
continue # Retry the request with a fresh session
264270

@@ -269,6 +275,8 @@ def _request(
269275
self.logger.exception(e)
270276
raise ExternalBrokerError(f'{self}: request error: {str(e)}') from e
271277

278+
raise ExternalBrokerError(f'{self}: failed to complete request: {method} {url} {kwargs}')
279+
272280
def _process_response(self, response, result: Result) -> Result:
273281
try:
274282
response.raise_for_status()
@@ -287,12 +295,16 @@ def _process_response(self, response, result: Result) -> Result:
287295
f'{self}: response error {result} :: {response.status_code} :: {response.reason} :: {response.text}', status_code=response.status_code
288296
) from e
289297

290-
def close(self):
298+
def close_session(self):
291299
"""Closes the session to release resources."""
292300
if hasattr(self, '_session') and self._session is not None:
293301
self._session.close()
294302
self._session = None
295303

304+
def close(self):
305+
self.close_session()
306+
307+
296308
def register_shutdown_handler(self):
297309
"""
298310
Registers a signal-based and atexit shutdown handler for graceful session termination.

0 commit comments

Comments
 (0)