Skip to content

Drain MCP NIO response tasks before event-loop shutdown #120

Description

@lynnswap

Problem

The MCP HTTP suite intermittently logs:

ERROR: Cannot schedule tasks on an EventLoop that has already shut down. This will be upgraded to a forced crash in future SwiftNIO versions.

The warning appears around stopDrainsAdmittedHandlerBeforeClosingItsSession. It does not fail the current test, but SwiftNIO explicitly says it will become a crash.

Evidence

  • Clean detached PR checkpoint 22874f3: reproduced in 1 of 3 full CodexReviewMCPHTTPServerTests runs; all 27 tests otherwise passed.
  • Current Wave 3B replacement worktree: reproduced in 2 of 3 full MCP runs; all tests otherwise passed.
  • Five isolated runs of stopDrainsAdmittedHandlerBeforeClosingItsSession did not reproduce, so this is suite-order/lifecycle timing rather than a deterministic assertion failure.

Required investigation

Trace NIO handler response/stream completion tasks and event-loop callbacks relative to listener/session close and shutdownGracefully(). The MCP lifecycle owner must await every Task that can call eventLoop.execute/write after shutdown. Do not suppress the warning or add delays.

Acceptance

  • No scheduling occurs after event-loop shutdown.
  • Repeated full MCP suite and full package runs emit no warning.
  • Admission close -> handler/response flush drain -> session/channel close -> event-loop shutdown remains explicit.
  • Deterministic task/flush ownership tests; no sleeps, polling, or warning filtering.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions