Skip to content

Commit 12c4cac

Browse files
committed
In case request_timeout is None, use 0, where relevant.
- Improve the async session closing by utilizing a non-blocking asyncio ``Task``.
1 parent 023c92d commit 12c4cac

File tree

2 files changed

+19
-20
lines changed

2 files changed

+19
-20
lines changed

tests/core/utilities/test_http_session_manager.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ def _simulate_call(http_session_manager, uri):
7575
return _session
7676

7777

78-
@pytest.fixture
78+
@pytest.fixture(scope="function")
7979
def http_session_manager():
8080
return HTTPSessionManager()
8181

@@ -323,7 +323,6 @@ async def test_session_manager_async_cache_does_not_close_session_before_a_call_
323323
# set cache size to 1 + set future session close thread time to 0.01s
324324
http_session_manager.session_cache = SimpleCache(1)
325325
_timeout_for_testing = 0.01
326-
http_session_manager.request_timeout = _timeout_for_testing
327326

328327
async def cache_uri_and_return_session(uri):
329328
_session = await http_session_manager.async_cache_and_return_session(

web3/_utils/http_session_manager.py

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ def cache_and_return_session(
8686
f"{evicted_session}",
8787
)
8888
threading.Timer(
89-
request_timeout + 0.1,
89+
request_timeout or 0 + 0.1,
9090
self._close_evicted_sessions,
9191
args=[evicted_sessions],
9292
).start()
@@ -200,23 +200,24 @@ async def async_cache_and_return_session(
200200
# At this point the evicted sessions are already popped out of the cache and
201201
# just stored in the `evicted_sessions` dict. So we can kick off a future
202202
# task to close them and it should be safe to pop out of the lock here.
203-
evicted_sessions = evicted_items.values()
203+
evicted_sessions = list(evicted_items.values())
204204
for evicted_session in evicted_sessions:
205205
self.logger.debug(
206206
"Async session cache full. Session evicted from cache: "
207207
f"{evicted_session}",
208208
)
209-
# Kick off a future task, in a separate thread, to close the evicted
210-
# sessions. In the case that the cache filled very quickly and some
211-
# sessions have been evicted before their original request has been made,
212-
# we set the timer to a bit more than the `DEFAULT_TIMEOUT` for a call. This
213-
# should make it so that any call from an evicted session can still be made
214-
# before the session is closed.
215-
threading.Timer(
216-
request_timeout.total + 0.1,
217-
self._async_close_evicted_sessions,
218-
args=[evicted_sessions],
219-
).start()
209+
# Kick off an asyncio `Task` to close the evicted sessions. In the case
210+
# that the cache filled very quickly and some sessions have been evicted
211+
# before their original request has been made, we set the timer to a bit
212+
# more than the `request_timeout` for a call. This should make it so that
213+
# any call from an evicted session can still be made before the session
214+
# is closed.
215+
asyncio.create_task(
216+
self._async_close_evicted_sessions(
217+
request_timeout.total or 0 + 0.1,
218+
evicted_sessions,
219+
)
220+
)
220221

221222
return cached_session
222223

@@ -258,18 +259,17 @@ async def async_make_post_request(
258259
response.raise_for_status()
259260
return await response.read()
260261

261-
def _async_close_evicted_sessions(
262-
self, evicted_sessions: List[ClientSession]
262+
async def _async_close_evicted_sessions(
263+
self, timeout: float, evicted_sessions: List[ClientSession]
263264
) -> None:
264-
loop = asyncio.new_event_loop()
265+
await asyncio.sleep(timeout)
265266

266267
for evicted_session in evicted_sessions:
267-
loop.run_until_complete(evicted_session.close())
268+
await evicted_session.close()
268269
self.logger.debug(f"Closed evicted async session: {evicted_session}")
269270

270271
if any(not evicted_session.closed for evicted_session in evicted_sessions):
271272
self.logger.warning(
272273
"Some evicted async sessions were not properly closed: "
273274
f"{evicted_sessions}"
274275
)
275-
loop.close()

0 commit comments

Comments
 (0)