@@ -931,13 +931,15 @@ async def remove_stale_sockets(self, reference_generation: int) -> None:
931931                return 
932932
933933        if  self .opts .max_idle_time_seconds  is  not   None :
934+             close_conns  =  []
934935            async  with  self .lock :
935936                while  (
936937                    self .conns 
937938                    and  self .conns [- 1 ].idle_time_seconds () >  self .opts .max_idle_time_seconds 
938939                ):
939-                     conn  =  self .conns .pop ()
940-                     await  conn .close_conn (ConnectionClosedReason .IDLE )
940+                     close_conns .append (self .conns .pop ())
941+             for  conn  in  close_conns :
942+                 await  conn .close_conn (ConnectionClosedReason .IDLE )
941943
942944        while  True :
943945            async  with  self .size_cond :
@@ -957,14 +959,18 @@ async def remove_stale_sockets(self, reference_generation: int) -> None:
957959                    self ._pending  +=  1 
958960                    incremented  =  True 
959961                conn  =  await  self .connect ()
962+                 close_conn  =  False 
960963                async  with  self .lock :
961964                    # Close connection and return if the pool was reset during 
962965                    # socket creation or while acquiring the pool lock. 
963966                    if  self .gen .get_overall () !=  reference_generation :
964-                         await  conn .close_conn (ConnectionClosedReason .STALE )
965-                         return 
966-                     self .conns .appendleft (conn )
967-                     self .active_contexts .discard (conn .cancel_context )
967+                         close_conn  =  True 
968+                     if  not  close_conn :
969+                         self .conns .appendleft (conn )
970+                         self .active_contexts .discard (conn .cancel_context )
971+                 if  close_conn :
972+                     await  conn .close_conn (ConnectionClosedReason .STALE )
973+                     return 
968974            finally :
969975                if  incremented :
970976                    # Notify after adding the socket to the pool. 
@@ -1343,17 +1349,20 @@ async def checkin(self, conn: AsyncConnection) -> None:
13431349                        error = ConnectionClosedReason .ERROR ,
13441350                    )
13451351            else :
1352+                 close_conn  =  False 
13461353                async  with  self .lock :
13471354                    # Hold the lock to ensure this section does not race with 
13481355                    # Pool.reset(). 
13491356                    if  self .stale_generation (conn .generation , conn .service_id ):
1350-                         await   conn . close_conn ( ConnectionClosedReason . STALE ) 
1357+                         close_conn   =   True 
13511358                    else :
13521359                        conn .update_last_checkin_time ()
13531360                        conn .update_is_writable (bool (self .is_writable ))
13541361                        self .conns .appendleft (conn )
13551362                        # Notify any threads waiting to create a connection. 
13561363                        self ._max_connecting_cond .notify ()
1364+                 if  close_conn :
1365+                     await  conn .close_conn (ConnectionClosedReason .STALE )
13571366
13581367        async  with  self .size_cond :
13591368            if  txn :
0 commit comments