@@ -1219,35 +1219,23 @@ def test_shutdown_close_idle_keepalive(
12191219 sock = unused_port_socket
12201220 port = sock .getsockname ()[1 ]
12211221 t = None
1222- sess_holder : list [ClientSession ] = []
12231222
12241223 async def test () -> None :
12251224 await asyncio .sleep (1 )
1226- sess = ClientSession ()
1227- sess_holder .append (sess )
1228- async with sess :
1225+ async with ClientSession () as sess :
12291226 async with sess .get (f"http://127.0.0.1:{ port } /stop" ):
12301227 pass
1228+
12311229 # Hold on to keep-alive connection.
12321230 await asyncio .sleep (5 )
12331231
12341232 async def run_test (app : web .Application ) -> AsyncIterator [None ]:
12351233 nonlocal t
12361234 t = asyncio .create_task (test ())
12371235 yield
1238- try :
1239- t .cancel ()
1240- with contextlib .suppress (asyncio .CancelledError ):
1241- await t
1242- finally :
1243- if sys .platform == "win32" :
1244- # On Windows, explicitly close the session and yield to the
1245- # event loop to mitigate resource warnings related to unclosed
1246- # sockets, which are more common due to ProactorEventLoop
1247- # timing issues.
1248- for sess in sess_holder :
1249- await sess .close ()
1250- await asyncio .sleep (0.1 )
1236+ t .cancel ()
1237+ with contextlib .suppress (asyncio .CancelledError ):
1238+ await t
12511239
12521240 app = web .Application ()
12531241 app .cleanup_ctx .append (run_test )
@@ -1280,13 +1268,9 @@ async def close_websockets(app: web.Application) -> None:
12801268 for ws in app [WS ]:
12811269 await ws .close (code = WSCloseCode .GOING_AWAY )
12821270
1283- sess_holder : list [ClientSession ] = []
1284-
12851271 async def test () -> None :
12861272 await asyncio .sleep (1 )
1287- sess = ClientSession ()
1288- sess_holder .append (sess )
1289- async with sess :
1273+ async with ClientSession () as sess :
12901274 async with sess .ws_connect (f"http://127.0.0.1:{ port } /ws" ) as ws :
12911275 async with sess .get (f"http://127.0.0.1:{ port } /stop" ):
12921276 pass
@@ -1301,18 +1285,9 @@ async def run_test(app: web.Application) -> AsyncIterator[None]:
13011285 t = asyncio .create_task (test ())
13021286 yield
13031287 await asyncio .sleep (0 ) # In case test() hasn't resumed yet.
1304- try :
1305- try :
1306- await asyncio .wait_for (t , timeout = 3.0 )
1307- except asyncio .TimeoutError :
1308- t .cancel ()
1309- with contextlib .suppress (asyncio .CancelledError ):
1310- await t
1311- finally :
1312- if sys .platform == "win32" :
1313- for sess in sess_holder :
1314- await sess .close ()
1315- await asyncio .sleep (0.1 )
1288+ t .cancel ()
1289+ with contextlib .suppress (asyncio .CancelledError ):
1290+ await t
13161291
13171292 app = web .Application ()
13181293 app [WS ] = set ()
0 commit comments