Skip to content

fix(dev-overrides): Add automatic response cleanup via onClose callback #952

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 4 commits into
base: main
Choose a base branch
from

Conversation

sommeeeer
Copy link
Contributor

@sommeeeer sommeeeer commented Aug 7, 2025

To make request.signal.onabort work in route handlers. Currently only works for node and express-dev. It will close the OpenNextNodeResponse we pass into NextServer's request handler when the original response in the wrapper is closed. This will ensure that the AbortSignal associated with request.signal is properly triggered when a client disconnects during a streaming response.

I did try to get it to work with awsLambda.streamifyResponse. It looks like we get no event emitted on responseStream when a client disconnects. I even tried with a barebone Lambda like this:

exports.handler = awslambda.streamifyResponse(
    async (event, responseStream, context) => {
        responseStream.setContentType('text/event-stream')
        responseStream.on("close", ()=> {
          // this never gets emitted at any point of time
          console.log("responseStream closed");
        });
        for (let i = 0; i < 20; i++) {
            await sleep(1000);
            responseStream.write(`event: ping\n`);
            responseStream.write(`data: ${i}\n\n`);
        }
        responseStream.end();
    }
);

Copy link

changeset-bot bot commented Aug 7, 2025

🦋 Changeset detected

Latest commit: d3e4b48

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 3 packages
Name Type
@opennextjs/aws Patch
app-pages-router Patch
app-router Patch

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

Copy link

pkg-pr-new bot commented Aug 7, 2025

Open in StackBlitz

pnpm add https://pkg.pr.new/@opennextjs/aws@952

commit: d3e4b48

Copy link
Contributor

@conico974 conico974 left a comment

Choose a reason for hiding this comment

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

This and the other one in cloudflare for the same thing makes me think that we should use a single interface in aws.

What make the most sense IMO is probably to add an AbortSignal in the StreamCreator. When it aborts, we just destroy the stream.
It's basically what you did, but using AbortSignal instead of our own implementation

@sommeeeer
Copy link
Contributor Author

sommeeeer commented Aug 12, 2025

It's basically what you did, but using AbortSignal instead of our own implementation

Thanks for the review Nico! This was an excellent idea.

I am now using an AbortSignal instead of a callback. However, to get this to work in cloudflare I had to add the initial request.signal you retrieve from fetch to the ALS store. The request.signal you retrieve in the handler here will not be === to the one you retrieve in your workers fetch. It doesn't trigger any event when the client disconnects/aborts either. Im not sure why thats the case, but im gonna look into it later. For now this PR would require us to put the signal in opennextjs-cloudflare here

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