@@ -86,7 +86,7 @@ def cache_and_return_session(
86
86
f"{ evicted_session } " ,
87
87
)
88
88
threading .Timer (
89
- request_timeout + 0.1 ,
89
+ request_timeout or 0 + 0.1 ,
90
90
self ._close_evicted_sessions ,
91
91
args = [evicted_sessions ],
92
92
).start ()
@@ -200,23 +200,24 @@ async def async_cache_and_return_session(
200
200
# At this point the evicted sessions are already popped out of the cache and
201
201
# just stored in the `evicted_sessions` dict. So we can kick off a future
202
202
# 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 () )
204
204
for evicted_session in evicted_sessions :
205
205
self .logger .debug (
206
206
"Async session cache full. Session evicted from cache: "
207
207
f"{ evicted_session } " ,
208
208
)
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
+ )
220
221
221
222
return cached_session
222
223
@@ -258,18 +259,17 @@ async def async_make_post_request(
258
259
response .raise_for_status ()
259
260
return await response .read ()
260
261
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 ]
263
264
) -> None :
264
- loop = asyncio .new_event_loop ( )
265
+ await asyncio .sleep ( timeout )
265
266
266
267
for evicted_session in evicted_sessions :
267
- loop . run_until_complete ( evicted_session .close () )
268
+ await evicted_session .close ()
268
269
self .logger .debug (f"Closed evicted async session: { evicted_session } " )
269
270
270
271
if any (not evicted_session .closed for evicted_session in evicted_sessions ):
271
272
self .logger .warning (
272
273
"Some evicted async sessions were not properly closed: "
273
274
f"{ evicted_sessions } "
274
275
)
275
- loop .close ()
0 commit comments