MCPcopy
hub / github.com/modelcontextprotocol/python-sdk / aclose_shielded

Function aclose_shielded

src/mcp/server/runner.py:160–178  ·  view source on GitHub ↗

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)

Source from the content-addressed store, hash-verified

158
159
160async 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
181def _apply_middleware(

Callers 4

_driveFunction · 0.90
serve_connectionFunction · 0.85
serve_oneFunction · 0.85

Calls 2

warningMethod · 0.80
acloseMethod · 0.45