1717if TYPE_CHECKING :
1818 import sys
1919 from asyncio .futures import Future
20- from collections .abc import Callable , Coroutine , Generator
20+ from collections .abc import Callable , Coroutine , Generator , Sequence
2121 from contextvars import Context
2222
2323 _T = TypeVar ("_T" )
@@ -46,7 +46,7 @@ def __init__(
4646
4747@dataclass (slots = True )
4848class WaitSet :
49- ops : list [OpFuture ]
49+ added : Sequence [OpFuture ]
5050 timer : int | None
5151 event : asyncio .Event
5252
@@ -74,6 +74,7 @@ class _TaskCtx:
7474
7575class EventLoop (asyncio .AbstractEventLoop ):
7676 __slots__ : tuple [str , ...] = (
77+ "_added" ,
7778 "_closed" ,
7879 "_ctx" ,
7980 "_event" ,
@@ -98,6 +99,7 @@ def __init__(self, host: asyncio.AbstractEventLoop) -> None:
9899 self ._closed : bool = False
99100 self ._event : asyncio .Event = asyncio .Event () # loop = _host
100101 self ._timers : list [asyncio .TimerHandle ] = []
102+ self ._added : list [OpFuture ] = []
101103
102104 def set_key (self , key : bytes ) -> None :
103105 # Derive a fixed-length key from the provided key
@@ -223,12 +225,17 @@ def poll_completion(self, task: Future[_T]) -> WaitSet | None:
223225
224226 if task .done ():
225227 return None
226- return WaitSet (ops = [* self ._ops .values ()], timer = deadline , event = self ._event )
228+
229+ added , self ._added = self ._added , []
230+ return WaitSet (added = added , timer = deadline , event = self ._event )
227231 finally :
228232 events ._set_running_loop (self ._host ) # noqa: SLF001
229233 if prev_task :
230234 tasks ._enter_task (self ._host , prev_task ) # noqa: SLF001
231235
236+ def pending_ops (self ) -> Sequence [OpFuture ]:
237+ return tuple (self ._ops .values ())
238+
232239 def create_op (self , params : object , * , external : bool = False ) -> OpFuture :
233240 if external :
234241 id_ = random_id ()
@@ -237,6 +244,7 @@ def create_op(self, params: object, *, external: bool = False) -> OpFuture:
237244 id_ = self .generate_op_id ()
238245 op_fut = OpFuture (id_ , params , self )
239246 self ._ops [id_ ] = op_fut
247+ self ._added .append (op_fut )
240248 return op_fut
241249
242250 @overload
@@ -276,7 +284,7 @@ def close(self) -> None:
276284 p = self .poll_completion (op )
277285 if p is not None :
278286 logger .warning (
279- "Event loop closed with pending operations: %r" , p . ops
287+ "Event loop closed with pending operations: %r" , self . _ops
280288 )
281289 while self ._timers :
282290 th = heappop (self ._timers )
0 commit comments