Skip to content

test: convert context_aware_server to in-process threads for coverage#2342

Open
perhapzz wants to merge 6 commits intomodelcontextprotocol:mainfrom
perhapzz:fix/1678-context-aware-server-coverage
Open

test: convert context_aware_server to in-process threads for coverage#2342
perhapzz wants to merge 6 commits intomodelcontextprotocol:mainfrom
perhapzz:fix/1678-context-aware-server-coverage

Conversation

@perhapzz
Copy link

@perhapzz perhapzz commented Mar 25, 2026

Motivation

Part of #1678

The context_aware_server test fixture launches a server via multiprocessing.Process, so coverage.py cannot track its execution. This PR applies the same in-process threading approach to this fixture.

Changes

  • Replace multiprocessing.Process with threading.Thread + uvicorn.Server for the context_aware_server fixture
  • Graceful shutdown via server.should_exit = True instead of proc.kill()
  • Remove 5 # pragma: no cover markers: _handle_context_list_tools, _handle_context_call_tool, run_context_aware_server, context_aware_server fixture, proc.is_alive() guard
  • All 61 tests pass, pyright 0 errors, ruff check + ruff format clean

@perhapzz perhapzz force-pushed the fix/1678-context-aware-server-coverage branch from 9b80c04 to 40bfd49 Compare March 25, 2026 10:34
… threads

Replace multiprocessing.Process with threading.Thread for the
context_aware_server fixture so coverage.py can track server-side code.

- Remove 5 pragma: no cover markers now reachable by coverage
- Graceful shutdown via server.should_exit instead of proc.kill()
- All 61 tests pass, pyright/ruff clean

Part of modelcontextprotocol#1678
@perhapzz perhapzz force-pushed the fix/1678-context-aware-server-coverage branch from 40bfd49 to f53793d Compare March 25, 2026 10:47
@perhapzz perhapzz marked this pull request as draft March 25, 2026 13:45
@perhapzz perhapzz marked this pull request as ready for review March 25, 2026 16:02
…_aware handler

- Add pragma: no branch for ctx.request guards (always truthy in test env)
- Add pragma: no cover for unknown tool fallback return (never reached in tests)
- Fixes coverage failure: these branches are defensive code paths that
  cannot be exercised through the test fixtures
@perhapzz perhapzz marked this pull request as draft March 25, 2026 17:29
@perhapzz perhapzz marked this pull request as ready for review March 25, 2026 17:29
Python 3.14 deprecates asyncio.iscoroutinefunction(), which uvicorn
calls internally. With pytest's filterwarnings=['error'], this
DeprecationWarning becomes an exception that kills the server thread
before it can start listening.

In multiprocessing mode this was hidden because child processes don't
inherit pytest's warning filters. Threading shares the same process,
so we need to explicitly suppress DeprecationWarnings in the server
thread.
@perhapzz perhapzz force-pushed the fix/1678-context-aware-server-coverage branch from 6cba79c to ff4239d Compare March 26, 2026 03:13
On Windows Python 3.13, the ProactorBasePipeTransport.__del__ fires
during GC after the threaded uvicorn server shuts down, raising a
PytestUnraisableExceptionWarning due to filterwarnings=['error'].

Force a GC collection with warnings suppressed so the transport
finalizer runs before pytest's unraisable-exception hook can catch it.
On Windows Python 3.13, the ProactorBasePipeTransport finalizer fires
during GC after the threaded uvicorn server shuts down, raising
PytestUnraisableExceptionWarning. Add a filterwarnings marker to all
tests that use the threaded context_aware_server fixture.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant