Skip to content

fix(server): clean up custom route streams on disconnect#16335

Draft
mbenhamd wants to merge 12 commits into
mainfrom
fix/custom-route-stream-cleanup
Draft

fix(server): clean up custom route streams on disconnect#16335
mbenhamd wants to merge 12 commits into
mainfrom
fix/custom-route-stream-cleanup

Conversation

@mbenhamd
Copy link
Copy Markdown
Contributor

@mbenhamd mbenhamd commented May 8, 2026

What changed

Custom API routes now receive the request abort signal, and Node-based adapters pipe streamed Response bodies through the shared server bridge with backpressure-aware cancellation. Fastify, Express, and Koa now stop reading custom route streams when the client disconnects, while Hono forwards the host runtime's request signal into custom route handlers.

This follows up on #16307 after the built-in Fastify stream cleanup in #16308. It keeps MCP and Bun-specific completed-body behavior out of this PR.

Why

The custom route bridge previously read Web Response.body chunks manually and wrote them to Node responses without backpressure or disconnect cleanup. Long-running custom route streams could keep producing after the client closed the connection.

Validation

  • corepack pnpm turbo build --filter ./packages/core
  • corepack pnpm turbo build --filter ./packages/server
  • corepack pnpm turbo build --filter ./packages/mcp
  • corepack pnpm --dir packages/server exec vitest run src/server/server-adapter/index.test.ts --config vitest.config.ts
  • focused Fastify, Express, Koa, and Hono adapter test files
  • Fastify, Express, Koa, and Hono adapter builds
  • git diff --check

@vercel
Copy link
Copy Markdown

vercel Bot commented May 8, 2026

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

2 Skipped Deployments
Project Deployment Actions Updated (UTC)
mastra-docs-1.x Skipped Skipped May 12, 2026 10:39pm
mastra-playground-ui Skipped Skipped May 12, 2026 10:39pm

Request Review

@coderabbitai
Copy link
Copy Markdown
Contributor

coderabbitai Bot commented May 8, 2026

Important

Review skipped

Draft detected.

Please check the settings in the CodeRabbit UI or the .coderabbit.yaml file in this repository. To trigger a single review, invoke the @coderabbitai review command.

⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Pro

Run ID: a4e75b19-be7d-4fd0-b18d-a6407c47efd7

You can disable this status message by setting the reviews.review_status to false in the CodeRabbit configuration file.

Use the checkbox below for a quick retry:

  • 🔍 Trigger review
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch fix/custom-route-stream-cleanup

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

@changeset-bot
Copy link
Copy Markdown

changeset-bot Bot commented May 8, 2026

🦋 Changeset detected

Latest commit: 9ef054b

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

This PR includes changesets to release 23 packages
Name Type
@mastra/express Patch
@mastra/fastify Patch
@mastra/hono Patch
@mastra/koa Patch
@mastra/server Patch
@mastra/deployer Patch
@mastra/nestjs Patch
mastra Patch
@mastra/deployer-cloud Patch
@mastra/deployer-cloudflare Patch
@mastra/deployer-netlify Patch
@mastra/deployer-vercel Patch
@mastra/temporal Patch
create-mastra Patch
@mastra/core Patch
@internal/playground Patch
mastracode Patch
@mastra/mcp-docs-server Patch
@mastra/client-js Patch
@mastra/opencode Patch
@mastra/longmemeval Patch
@mastra/playground-ui Patch
@mastra/react 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

Use the shared custom route bridge to propagate request abort signals into route handlers and pipe streamed Response bodies through Node pipeline for backpressure and cancellation.

Cover disconnect cleanup and normal POST completion across Fastify, Express, Koa, and Hono, and document how to forward c.req.raw.signal from custom streaming routes.
@vercel vercel Bot temporarily deployed to Preview – mastra-docs-1.x May 8, 2026 14:37 Inactive
@vercel vercel Bot temporarily deployed to Preview – mastra-playground-ui May 8, 2026 14:37 Inactive
Ensure local-registry E2E fixture installs and nested package-manager calls inherit minimum-release-age=0 so freshly published snapshot packages can resolve during CI.
@vercel vercel Bot temporarily deployed to Preview – mastra-playground-ui May 12, 2026 22:05 Inactive
@vercel vercel Bot temporarily deployed to Preview – mastra-docs-1.x May 12, 2026 22:05 Inactive
Use an OS-assigned port for the notification resource test and make teardown tolerate setup failures. This avoids CI flakes when the random port collides with another listener.
@vercel vercel Bot temporarily deployed to Preview – mastra-playground-ui May 12, 2026 22:27 Inactive
@vercel vercel Bot temporarily deployed to Preview – mastra-docs-1.x May 12, 2026 22:27 Inactive
@vercel vercel Bot temporarily deployed to Preview – mastra-playground-ui May 12, 2026 22:29 Inactive
@vercel vercel Bot temporarily deployed to Preview – mastra-docs-1.x May 12, 2026 22:29 Inactive
@vercel vercel Bot temporarily deployed to Preview – mastra-playground-ui May 12, 2026 22:39 Inactive
@vercel vercel Bot temporarily deployed to Preview – mastra-docs-1.x May 12, 2026 22:39 Inactive
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Documentation Improvements or additions to documentation

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant