Unwind ``connection.exit_stack`` under a shielded, bounded scope. Called from a driver's ``finally``: the shield lets per-connection cleanup callbacks run even when the driver itself is being cancelled, the `_EXIT_STACK_CLOSE_TIMEOUT` bound stops a hung callback wedging shutdown, an
(connection: Connection)
| 158 | |
| 159 | |
| 160 | async def aclose_shielded(connection: Connection) -> None: |
| 161 | """Unwind ``connection.exit_stack`` under a shielded, bounded scope. |
| 162 | |
| 163 | Called from a driver's ``finally``: the shield lets per-connection cleanup |
| 164 | callbacks run even when the driver itself is being cancelled, the |
| 165 | `_EXIT_STACK_CLOSE_TIMEOUT` bound stops a hung callback wedging shutdown, |
| 166 | and a raising callback is logged-and-swallowed so it never masks the |
| 167 | driver's own exception. |
| 168 | """ |
| 169 | with anyio.move_on_after(_EXIT_STACK_CLOSE_TIMEOUT, shield=True) as scope: |
| 170 | try: |
| 171 | await connection.exit_stack.aclose() |
| 172 | except Exception: |
| 173 | logger.exception("connection exit_stack cleanup raised") |
| 174 | if scope.cancelled_caught: |
| 175 | logger.warning( |
| 176 | "connection exit_stack cleanup exceeded %s seconds; abandoning remaining callbacks", |
| 177 | _EXIT_STACK_CLOSE_TIMEOUT, |
| 178 | ) |
| 179 | |
| 180 | |
| 181 | def _apply_middleware( |