Skip to content

Cancel non-streaming chat inference when clients disconnect - #1188

Open
Baiju Meswani (baijumeswani) wants to merge 3 commits into
mainfrom
copilot/nonstream-disconnect-cancellation
Open

Baiju Meswani (baijumeswani) wants to merge 3 commits into
mainfrom
copilot/nonstream-disconnect-cancellation

Conversation

@baijumeswani

Copy link
Copy Markdown
Collaborator

Summary

  • Monitor the non-streaming Chat Completions TCP connection during synchronous inference without reading request bytes, and cancel the active request on peer disconnect.
  • Release request-scoped session and Engine state after cancellation and classify disconnected route telemetry as canceled rather than a successful completion or inference failure. Streaming behavior and other endpoints are unchanged.
  • Cover buffered-input/keep-alive safety, stop-on-completion, an actual HTTP disconnect with canceled telemetry and a successful follow-up request.

Verification

  • Built via python sdk_v2/cpp/build.py on Linux.
  • Focused monitor, HTTP disconnect, and pre-existing streaming-disconnect tests passed.

Normal TCP peer disconnects are in scope; silent network blackholes without FIN/RST require a separately specified request deadline. Windows socket handling is included but was not exercised on a Windows runner.

Watch the non-streaming request's TCP peer without consuming input, cancel the active request when the client closes, and classify the disconnected route as canceled. Exercise the socket monitor and real HTTP inference cleanup, including a successful follow-up request.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>

Copilot-Session: 92506982-4b86-4eca-aa81-4f0abfcb5a8a
Copilot AI balanced review requested due to automatic review settings October 13, 2026 06:27
@vercel

vercel Bot commented Oct 13, 2026 •

Copy link
Copy Markdown

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Actions Updated
foundry-local Ready Ready Preview Oct 13, 2026 7:06am UTC

Request Review

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

🟡 Changes recommended

Windows disconnect detection fails when unread input precedes the FIN, and the telemetry assertion is race-prone.

2 open findings
What changed in this PR

Adds client-disconnect cancellation for synchronous Chat Completions inference.

Changes:

  • Adds a TCP disconnect monitor that cancels active requests.
  • Reports disconnected requests as canceled telemetry.
  • Adds monitor and end-to-end disconnect tests.
File Description
sdk_v2/​cpp/​src/​service/​non_streaming_disconnect_monitor.h Implements cross-platform peer monitoring.
sdk_v2/​cpp/​src/​service/​chat_completions_handler.h Extends non-streaming handler inputs.
sdk_v2/​cpp/​src/​service/​chat_completions_handler.cc Integrates cancellation and telemetry classification.
sdk_v2/​cpp/​test/​internal_api/​web_service_test.cc Adds disconnect, recovery, and telemetry tests.

🧠 Review effort: Balanced


💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.

Comment thread sdk_v2/cpp/src/service/non_streaming_disconnect_monitor.h Outdated
Comment thread sdk_v2/cpp/test/internal_api/web_service_test.cc Outdated
Read TCP state when a peek finds unread bytes, so a peer FIN cannot be masked by pipelined data. Exercise the Windows socket case and wait for the asynchronous route telemetry action before asserting cancellation.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>

Copilot-Session: 92506982-4b86-4eca-aa81-4f0abfcb5a8a

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

🔵 Needs a closer look

Monitor shutdown can miss a disconnect occurring after its final poll, incorrectly recording success.

0 open findings

2 resolved since last review
Previously missed (1)

In code that hasn't changed since last review

Medium severity Stop can miss disconnect before reporting successful route

sdk_v2/​cpp/​src/​service/​non_streaming_disconnect_monitor.h:64

Stop() sets the stop token before guaranteeing one final socket check. A FIN/RST can therefore arrive after the worker's last poll (or before the new thread first runs), then Stop() wakes/joins the worker and returns false; the handler records a successful route even though the peer disconnected during inference. Please synchronize shutdown with a final nonblocking PeerDisconnected check (while retaining the connection/request safely), and add a regression test for disconnect-immediately-before-stop.

🧠 Review effort: Balanced

Retain the TCP connection through monitor shutdown and perform a final nonblocking peer check after joining its worker, including a regression for an immediate disconnect before Stop. Keep repeated Stop calls from rechecking a completed request.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>

Copilot-Session: 92506982-4b86-4eca-aa81-4f0abfcb5a8a
@baijumeswani

Copy link
Copy Markdown
Collaborator Author

Addressed the new Copilot review finding in 2b47789. Stop() now joins the worker and performs one final nonblocking peer check while the monitor still owns the TCP connection and request; repeated Stop calls do not recheck a completed request. Added an immediate-disconnect-before-Stop regression (and made the Windows buffered-input case exercise immediate Stop). Focused disconnect and existing streaming tests passed in 10 repeated runs.

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

🔵 Needs a closer look

Low-level cross-platform socket and concurrency behavior warrants human review, particularly because the Windows path was not executed.

0 open findings

🧠 Review effort: Balanced

This branch was successfully deployed

1 active deployment
Preview — 2b477894 Deployed Oct 13, 2026 by vercel[bot]
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.

2 participants