|
9 | 9 | import sys |
10 | 10 |
|
11 | 11 | import anyio |
| 12 | +import coverage |
12 | 13 |
|
13 | 14 | from mcp.server import Server, ServerRequestContext |
14 | 15 | from mcp.server.stdio import stdio_server |
@@ -54,9 +55,27 @@ async def set_logging_level(ctx: ServerRequestContext, params: SetLevelRequestPa |
54 | 55 | async def main() -> None: |
55 | 56 | async with stdio_server() as (read_stream, write_stream): |
56 | 57 | await server.run(read_stream, write_stream, server.create_initialization_options()) |
| 58 | + # Flush this process's coverage data before the clean-exit line below. Without this, the |
| 59 | + # data is only written by coverage's atexit hook during interpreter teardown -- and on a |
| 60 | + # slow Windows runner that can overrun the transport's termination grace, so the kill |
| 61 | + # silently destroys the data file and the 100% gate trips on this module's subprocess-only |
| 62 | + # lines. Saving here puts the write before the line the test synchronizes on: once the |
| 63 | + # parent has seen "clean exit", the data is durably on disk and the escalation is harmless. |
| 64 | + # Nothing measured may execute after the save (it would be unrecordable by construction), |
| 65 | + # hence the excluded lines below. The branch is pragma'd because under coverage the |
| 66 | + # instance always exists, and without coverage nothing is measured anyway. |
| 67 | + cov = getattr(coverage.process_startup, "coverage", None) |
| 68 | + if cov is not None: # pragma: no branch |
| 69 | + # stop() is load-bearing twice over: it ends tracing, making itself the last |
| 70 | + # recordable line, and it leaves nothing new for coverage's atexit re-save to flush -- |
| 71 | + # so a kill landing during interpreter teardown cannot corrupt the file save() wrote |
| 72 | + # (coverage opens it with sqlite journaling off; a torn rewrite would not roll back). |
| 73 | + cov.stop() |
| 74 | + cov.save() # pragma: lax no cover - untraced: stop() above already ended measurement |
57 | 75 | # Reached only when the run loop exits because stdin closed; if the process were terminated |
58 | | - # the test's stderr capture would not see this line. |
59 | | - print("stdio-echo: clean exit", file=sys.stderr, flush=True) |
| 76 | + # the test's stderr capture would not see this line. lax no cover: runs after the coverage |
| 77 | + # save by design, so it can never appear covered. |
| 78 | + print("stdio-echo: clean exit", file=sys.stderr, flush=True) # pragma: lax no cover |
60 | 79 |
|
61 | 80 |
|
62 | 81 | if __name__ == "__main__": |
|
0 commit comments