ai icon indicating copy to clipboard operation
ai copied to clipboard

express route isn't receiving abort signal from useChat

Open nikitatome opened this issue 1 year ago • 4 comments

Description

We're observing that when a client executes the stop function from the useChat hook it closes the request, but it doesn't actually communicate an abort signal to the server.

We can do something like this to abort, but we need a way to differentiate user-initiated aborts vs. the request closing for other reasons.

  res.on('close', () => {
    controller.abort();
  });

If I look at the raw request that Express is receiving, it doesn't appear to have the signal populated.

Not sure if the stopping streams documentation is still accurate for v5.

https://ai-sdk.dev/docs/advanced/stopping-streams

AI SDK Version

  • ai: 5.0.39
  • express: 5.1.0

Code of Conduct

  • [x] I agree to follow this project's Code of Conduct

nikitatome avatar Sep 18 '25 04:09 nikitatome

I encountered the same problem, the stop on useChat is not working

konghaiya avatar Sep 19 '25 06:09 konghaiya

Identify issue type

{
  "evidence": [
    "Reporter says calling `stop` from `useChat` closes the request but does not communicate an abort signal to the Express server route.",
    "They state the raw Express request does not appear to have the signal populated and question whether the v5 stopping-streams documentation is accurate.",
    "A commenter reports the same problem: “the stop on useChat is not working.”"
  ],
  "rationale": "The issue describes a concrete current-behavior failure where `useChat` stop/abort handling is not observable by the server as expected, potentially contradicting documented stopping-stream behavior.",
  "type": "bug",
  "confidence": "medium"
}

lgrammel avatar Jul 02 '26 09:07 lgrammel

Bug Reproduction

Issue report appears invalid.

Target branch: main

  • The report assumes a client-side AbortSignal should appear as req.signal on an Express request, but Express uses Node.js http.IncomingMessage, not the Web Request API.
  • The AI SDK stopping-streams example forwards req.signal from a Web API Request; it does not document that property for Express requests.
  • useChat.stop() aborts the Fetch request, which communicates transport termination to Express rather than transmitting an AbortSignal object or its user-initiated reason.
  • Distinguishing an explicit stop from a tab close or network disconnect requires an application-level cancellation message or endpoint.

Relevant Documentation

  • https://ai-sdk.dev/docs/advanced/stopping-streams
  • https://ai-sdk.dev/docs/ai-sdk-ui/chatbot
  • https://expressjs.com/en/5x/guide/overriding-express-api/
  • https://nodejs.org/api/http.html
  • https://fetch.spec.whatwg.org/

lgrammel avatar Jul 14 '26 12:07 lgrammel

Invalid. Closing as not planned.

lgrammel avatar Jul 14 '26 12:07 lgrammel