Skip to content

Own MCP response work shutdown - #141

Closed
lynnswap wants to merge 4 commits into
mainfrom
codex/mcp-response-work-owner-pr
Closed

Own MCP response work shutdown#141
lynnswap wants to merge 4 commits into
mainfrom
codex/mcp-response-work-owner-pr

Conversation

@lynnswap

@lynnswap lynnswap commented Aug 22, 2026

Copy link
Copy Markdown
Owner

Purpose

Prevent an MCP listener generation from shutting down its SwiftNIO event loop while admitted connection, request, or response work is still active, without violating HTTP response ordering or connection lifetime semantics.

Changes

  • Extend each admitted request operation with fixed finite or open source, writer, and response-end ownership.
  • Make each connection admission-order queue the sole HTTP response writer authority and disable NIO pipelining assistance.
  • Start response-source consumption only after the exact FIFO writer turn, preventing queued responses from buffering source output before they can write.
  • Use a one-body rendezvous between source and writer so each source read waits for the prior physical write acknowledgement; heartbeat delivery is coalesced and remains fair.
  • Run SSE source consumption and heartbeat delivery as joined response work, with source and framing failures terminating the connection.
  • Handle Expect: 100-continue through the existing connection FIFO: HTTP/1.1 receives a head-only interim response before its body, HTTP/1.0 ignores the expectation, and unsupported expectations receive an owned 417 close.
  • Preserve direct handleHTTPRequest stream completion tracking for finite completion and cancelled open streams.
  • Close HTTP/1.0 non-keepalive and Connection: close requests after the acknowledged response end, while retaining owner state through physical channel-close acknowledgement.
  • Preserve the first acknowledged response end across concurrent stop while still joining all physical response work.
  • Add deterministic coverage for FIFO ordering and source admission, direct stream completion, non-persistent EOF, finite disconnects, writer and end shutdown races, accepted child acknowledgements, and late event-loop cleanup.

Testing

  • Strict MCP suite (51 tests)
  • SwiftNIO shutdown warning absent in 5 strict suite runs
  • Host tests (48 tests)
  • swift test --build-system swiftbuild --no-parallel
  • Compatibility consumer, API, and MCP golden gates
  • ReviewMonitor xcodebuild test (15 tests)
  • Post-main-merge strict MCP suite and diff check
  • Concurrency and scope/API/test audits

Screenshots

Not applicable.

@chatgpt-codex-connector

Copy link
Copy Markdown

You have reached your Codex usage limits for security reviews. Please try again later.

@lynnswap

Copy link
Copy Markdown
Owner Author

@codex review

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 0a36671a18

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment thread Sources/CodexReviewMCPServer/CodexReviewMCPHTTPServer.swift Outdated
Comment thread Sources/CodexReviewMCPServer/CodexReviewMCPHTTPServer.swift
Comment thread Sources/CodexReviewMCPServer/CodexReviewMCPHTTPServer.swift

@lynnswap lynnswap left a comment

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@codex review

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 2744bc3d5c

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment thread Sources/CodexReviewMCPServer/CodexReviewMCPHTTPServer.swift Outdated
Comment thread Sources/CodexReviewMCPServer/CodexReviewMCPHTTPServer.swift

@lynnswap lynnswap left a comment

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@codex review

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 84e3f6c020

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment on lines +1404 to +1407
} else if heartbeatPending {
heartbeatPending = false
immediate = .heartbeat
} else if let pendingBody {

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Prioritize response bodies over pending heartbeats

When a heartbeat write takes at least one heartbeat interval (for example, with a backpressured client), the timer sets heartbeatPending while a source body is already pending; this ordering then selects another heartbeat first, allowing the timer to re-arm on every slow write and starving the actual MCP response body indefinitely. Check pendingBody before heartbeatPending so keep-alives cannot overtake application data.

Useful? React with 👍 / 👎.

Comment on lines +1733 to +1736
guard let admittedRequest = connection.admitRequest() else {
context.close(promise: nil)
return
}

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Close request admission at non-keepalive heads

When a client pipelines another request after one carrying Connection: close (or an HTTP/1.0 non-keepalive request), every subsequent .head is still admitted here and its handler can execute handleTrackedHTTPRequest before the first response reaches the later closeAfterResponse() call. Thus a side-effecting MCP request can run even though its response is subsequently cancelled when the connection closes. The fresh evidence beyond the earlier physical-close issue is this new head-time concurrent admission; close admission as soon as the non-keepalive head is accepted while still deferring the physical close until its response ends.

Useful? React with 👍 / 👎.

Comment on lines 655 to 657
let lease = operation.makeLease()
requests[operation.id] = operation
requests.append(operation)
lock.unlock()

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P1 Badge Bound the per-connection request queue

When the first response is slow or an open SSE stream, a client can pipeline arbitrarily many small requests and every one is appended here while channelReadComplete continues issuing context.read(); each admission also creates a task/body receipt, and completed handlers retain their prepared response while awaiting the FIFO writer turn. Unlike the previously fixed response-source buffering, this request-level queue remains unbounded, so a single connection can drive memory and task counts until the server is exhausted. Cap outstanding requests or stop reading from the channel until queue capacity becomes available.

Useful? React with 👍 / 👎.

@lynnswap

Copy link
Copy Markdown
Owner Author

Superseded by #143 and #144. #143 restored NIO as the sole inbound FIFO and bounded request admission/body handling. #144 rebuilds response lifecycle and bounded writer ownership without the custom FIFO that caused the repeated review findings.

@lynnswap

Copy link
Copy Markdown
Owner Author

Closing without merge. The corrected request-admission prerequisite is merged in #143 and the response-lifecycle successor continues in #144.

@lynnswap lynnswap closed this Aug 22, 2026
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