| 241 | ) |
| 242 | |
| 243 | async def __aenter__(self) -> Self: |
| 244 | self._task_group = anyio.create_task_group() |
| 245 | await self._task_group.__aenter__() |
| 246 | try: |
| 247 | await self._task_group.start(self._dispatcher.run, self._on_request, self._on_notify) |
| 248 | except BaseException: |
| 249 | # Unwind the entered task group before propagating: a cancellation |
| 250 | # landing here (e.g. `move_on_after` around connect) would abandon |
| 251 | # it and anyio would later raise "exited non-innermost cancel scope". |
| 252 | task_group = self._task_group |
| 253 | self._task_group = None |
| 254 | task_group.cancel_scope.cancel() |
| 255 | # Shield the group's own scope (a new one would break LIFO exit) |
| 256 | # so a pending outer cancellation cannot re-fire inside __aexit__. |
| 257 | task_group.cancel_scope.shield = True |
| 258 | await task_group.__aexit__(None, None, None) |
| 259 | raise |
| 260 | return self |
| 261 | |
| 262 | async def __aexit__( |
| 263 | self, |