Skip to content

Commit e1d712d

Browse files
committed
refactor: unify respond() call after if/else to avoid 3.14 phantom trace
Python 3.14's compiler attributes the async trampoline's CLEANUP_THROW instructions (for the try-body's await) to the next physical line of code, which was the else body. coverage.py traced a phantom line event there, tripping strict-no-cover even though the else never runs. Moving the try/respond after the if/else avoids the misattribution and also deduplicates the two respond() calls.
1 parent 36a974a commit e1d712d

File tree

1 file changed

+11
-10
lines changed

1 file changed

+11
-10
lines changed

src/mcp/server/lowlevel/server.py

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -489,17 +489,18 @@ async def _handle_request(
489489
if raise_exceptions: # pragma: no cover
490490
raise err
491491
response = types.ErrorData(code=0, message=str(err))
492-
493-
try:
494-
await message.respond(response)
495-
except anyio.ClosedResourceError:
496-
# Transport closed between handler unblocking and respond. Happens
497-
# when _receive_loop's finally wakes a handler blocked on
498-
# send_request: the handler runs to respond() before run()'s TG
499-
# cancel fires, but after _receive_loop closed _write_stream.
500-
logger.debug("Response for %s dropped - transport closed", message.request_id)
501492
else: # pragma: no cover
502-
await message.respond(types.ErrorData(code=types.METHOD_NOT_FOUND, message="Method not found"))
493+
response = types.ErrorData(code=types.METHOD_NOT_FOUND, message="Method not found")
494+
495+
try:
496+
await message.respond(response)
497+
except anyio.ClosedResourceError:
498+
# Transport closed between handler unblocking and respond. Happens
499+
# when _receive_loop's finally wakes a handler blocked on
500+
# send_request: the handler runs to respond() before run()'s TG
501+
# cancel fires, but after _receive_loop closed _write_stream.
502+
logger.debug("Response for %s dropped - transport closed", message.request_id)
503+
return
503504

504505
logger.debug("Response sent")
505506

0 commit comments

Comments
 (0)