Skip to content

[Feat]: Better DX for Abort events #1753

Closed
Closed

Description

@tri2820

Duplicates

  • I have searched the existing issues

Latest version

  • I have tested the latest version

Current behavior 😯

When the request is aborted, the abort event is not fired within server API route.

Example server API route

import type { APIEvent } from "@solidjs/start/server";
export async function GET(event: APIEvent) {

    event.request.signal.addEventListener("abort", () => {
        console.log('abort detected')
    })

    let i = 0;
    while (i < 20) {
        console.log('looping', i++, event.request.signal.aborted)
        await new Promise(resolve => setTimeout(resolve, 1000))
    }

    return new Response(null, { status: 200 })
}

Example browser triggers

<button
onClick={() => {
  controller = new AbortController();
  fetch("/api/test", {
    method: "GET",
    signal: new AbortController().signal,
  })
    .then((response) => {
      console.log("response", response);
    })
    .catch((error) => {
      console.log("error", error);
    });
}}
>
Call
</button>
<button
onClick={() => {
  console.log("controller", controller);
  controller?.abort("I am aborting this request");
  controller = undefined;
}}
>
Abort
</button>

Expected behavior 🤔

When user cancel the request in browser, the abort event should be fired.

Steps to reproduce 🕹

Steps:

  1. Create an API route
  2. Add abort listener inside the API route
  3. In the browser, fetch & abort
  4. Notice that the abort event is not fired inside the API route

Context 🔦

I'm building an AI chat app that streams the token from the AI provider back to the user.
Currently when the user clicks aborting the request, the api route could not detect that and keep streaming from the AI provider, wasting token.

Not sure if related but Next.js used to have a similar issue vercel/next.js#48682

Your environment 🌎

Linux denpa 6.12.10-arch1-1 #1 SMP PREEMPT_DYNAMIC Sat, 18 Jan 2025 02:26:57 +0000 x86_64 GNU/Linux

bun --version
1.2.0

node --version
v23.4.0

"dependencies": {
    "@solidjs/meta": "^0.29.4",
    "@solidjs/router": "^0.15.0",
    "@solidjs/start": "^1.0.11",
    "solid-js": "^1.9.2",
    "vinxi": "^0.4.3"
  },

Activity

  1. changed the title [-][Bug?]: Abort event does not fire on API route[/-] [+][Bug?]: Abort event is not fired on API route[/+] on Feb 3, 2025
  2. tri2820 commented on Feb 6, 2025

    @tri2820
    Author

    Update: After some digging I found out that we can listen to the close event on nativeEvent for this.

    import { getRequestEvent } from "solid-js/web";
    
    export async function GET() {
        const event = getRequestEvent()!;
        event.nativeEvent.node.req.addListener('close', () => {
            console.log('Client closed connection');
        })
    
        await new Promise(resolve => setTimeout(resolve, 5000));
    
        return new Response(`I'm alive!`, {
            status: 200,
        });
    }

    Nevertheless it would be good if event.request.signal.addEventListener("abort", ...) is supported

  3. atilafassina commented on Feb 12, 2025

    @atilafassina
    Member

    Thanks for this issue and the thorough explanation, @tri2820
    Imho, that's definitely something that's worth having a better DX around (ping @katywings for her 2cents).


    If you have time, it would be great if you could add a reproduction to our /tests workspace.

    You can start the PR with the failing test and then we can have a clear path moving forward.

    Meanwhile, this workaround is worthy of the docs, so I'm pinging @LadyBluenotes in case she wants to create an issue there too :)

    As soon as I can get my hands on trying the workaround, I can help writing the guide (but you're also ofc welcomed to do so if you want)

  4. added
    enhancementNew feature or request
    and removed
    bugSomething isn't working
    on Feb 12, 2025
  5. changed the title [-][Bug?]: Abort event is not fired on API route[/-] [+][Feat]: Better DX for Abort events[/+] on Feb 12, 2025
  6. katywings commented on Feb 12, 2025

    @katywings
    Contributor

    @tri2820 Thank you for bringing this up. I also think that having a better DX around abort sounds like a sensible addition 💯.

    The first question coming to my mind is if event.nativeEvent.node.req.addListener('close', ...) is the proper way to do this from a nitro/h3 perspective. Maybe @pi0 can give us some insight about this 🙂. Touching event.node directly is usually not recommended by h3, so I wonder if there is some "more official" way to do this, or if it already is planned for a future update?

    A similar question was asked in the Nitro Discussions under nitrojs/nitro#1541 but is unanswered 🙈.

  7. lxsmnsyc commented on Oct 13, 2026

    @lxsmnsyc
    Member

    Correction (this comment was wrong, reopening): this issue was filed against SolidStart 1.0, but the behavior below was verified on 2.0. SolidStart 1.0 and 2.0 have different request architectures (1.0 is vinxi + h3 v1; 2.0 is h3 v2 / srvx), so the 2.0 behavior does not show that 1.0 is fixed, and upgrading to 2.0 is not a resolution for this report. I should not have closed it. Leaving the 2.0 notes below for reference only.


    On SolidStart 2.0 (h3 v2 / srvx), event.request is a standard Web Request whose signal aborts when the client disconnects, so the Web-standard API from the original example works without reaching into nativeEvent:

    import type { APIEvent } from "@solidjs/start/server";
    
    export async function GET(event: APIEvent) {
      event.request.signal.addEventListener("abort", () => {
        // fires when the client cancels the request
      });
    
      const upstream = await fetch("https://example.com/stream", {
        signal: event.request.signal,
      });
      return new Response(upstream.body);
    }
  8. lxsmnsyc commented on Oct 16, 2026

    @lxsmnsyc
    Member

    Fixed on the 1.x line by #2340, released in @solidjs/start 1.4.0: on Node, event.request.signal now aborts when the client disconnects, so an API route can stop its work.

    On 2.x, event.request.signal already aborts on client disconnect.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions