Skip to content

fix: clean up JSON streams replaced by null - #2014

Open
Aurantium-47 wants to merge 1 commit into
koajs:masterfrom
Aurantium-47:fix/json-null-stream-cleanup
Open

Aurantium-47 wants to merge 1 commit into
koajs:masterfrom
Aurantium-47:fix/json-null-stream-cleanup

Conversation

@Aurantium-47

@Aurantium-47 Aurantium-47 commented Oct 12, 2026 •

Copy link
Copy Markdown

When a Node.js response stream has Content-Type: application/json, replacing it with null or undefined serializes the JSON body as 'null' and returns before cleaning up the old stream. The abandoned stream stays alive until the response finishes, unlike other non-stream body replacements.

Call the existing cleanup helper before this return. Regression tests cover both values and check that the JSON body, status and content type are preserved.

Validation on Windows with Node.js 24.15.0 / npm 11.12.1:

  • Both new regression tests fail before the fix and pass afterward.
  • node --test __tests__/response/body.test.js: 36 passed.
  • npm run test:coverage: 468 passed, 0 failed or skipped.
  • npm run lint and git diff --check: passed.

AI assistance: implemented and tested with OpenAI GPT-6.

Checklist

  • I have ensured my pull request is not behind the main or master branch of the original repository.
  • I have rebased all commits where necessary so that reviewing this pull request can be done without having to merge it first.
  • I have written a commit message that passes commitlint linting.
  • I have ensured that my code changes pass linting tests.
  • I have ensured that my code changes pass unit tests.
  • I have described my pull request and the reasons for code changes along with context if necessary.

No commitlint command or configuration is present in this checkout; the commit uses the existing fix: convention.

Summary by Sourcery

Ensure JSON stream replacements with null or undefined clean up the previous stream before returning.

Bug Fixes:

  • Clean up replaced response streams when JSON bodies are assigned null or undefined, preventing abandoned streams from remaining alive until the response finishes.

Tests:

  • Add regression coverage verifying stream cleanup while preserving the JSON body, status, and content type for null and undefined replacements.

Summary by CodeRabbit

  • Bug Fixes
    • Replacing a streamed body with null or undefined in a non-empty JSON response now serializes the body as null and properly closes the original stream. The response retains its status and JSON content type. This prevents the previous stream from being left open when the JSON body is replaced.

Call the existing stream cleanup before returning the serialized JSON null body.

Assisted-by: OpenAI GPT-6 <noreply@openai.com>
@coderabbitai

coderabbitai Bot commented Oct 12, 2026 •

Copy link
Copy Markdown

Review in Change Stack →

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration
  • Configuration used: Organization UI
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: d06f2efa-3c8a-4132-8ace-62fde4f17667

📥 Commits

Reviewing files that changed from the base of the PR and between 2fecd02 and 08b0ebb.


📒 Files selected for processing (2)
  • __tests__/response/body.test.js
  • lib/response.js

Included review availability: This review used your included allowance. Your plan provides up to 10 included reviews per hour; 9 remain after this review.



📝 Walkthrough

Walkthrough

When a response has JSON content type and a non-empty status, setting its body to null now cleans up the previous stream before returning the JSON body "null". Tests cover assigning null or undefined after a stream body and verify the body, status, content type, and stream cleanup.


Priority: ⬇️ Low

Merge Risk: ⚪ Minimal · up to 08b0e

The change cleans up the replaced stream while preserving the JSON null response. No merge-blocking risk remains in the reviewed change.

Pre-merge checks | Passed 5
✅ Passed checks (5 passed)
Check name Status Explanation
Description Check Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check Passed The title clearly and concisely describes the main change: cleaning up an existing JSON stream when the response body is replaced with null.
Docstring Coverage Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check. Docstring coverage is scoped to functions touched by this diff. Analyzed 0 functions across 2…
Linked Issues check Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check Passed Check skipped because no linked issues were found for this pull request.

  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

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.

@sourcery-ai

sourcery-ai Bot commented Oct 12, 2026

Copy link
Copy Markdown

Reviewer's Guide

The fix updates JSON body replacement to clean up an existing response stream before returning, while preserving the established null serialization and response metadata. New regression tests verify cleanup for both null and undefined replacements and confirm the resulting body, status, and content type.

Sequence diagram for JSON response body replacement cleanup

sequenceDiagram
    participant Caller
    participant Response
    participant PreviousStream

    Caller->>Response: body replacement with null or undefined
    Response->>Response: cleanupPreviousStream()
    Response->>Response: set _body to null
    Response-->>Caller: return
    PreviousStream-->>Response: stream is cleaned up
Loading

File-Level Changes

Change Details Files
Ensure replacing a JSON response stream with nullish values cleans up the previous stream before returning.
  • Invoke the existing previous-stream cleanup helper after serializing null.
  • Preserve the existing JSON body, status, and content type behavior.
lib/response.js
Add regression coverage for stream cleanup and JSON response invariants.
  • Cover replacement with null.
  • Cover replacement with undefined.
  • Assert stream destruction, serialized body, status, and content type.
__tests__/response/body.test.js

Tips and commands

Interacting with Sourcery

  • Trigger a new review: Comment @sourcery-ai review on the pull request.
  • Continue discussions: Reply directly to Sourcery's review comments.
  • Generate a GitHub issue from a review comment: Ask Sourcery to create an
    issue from a review comment by replying to it. You can also reply to a
    review comment with @sourcery-ai issue to create an issue from it.
  • Generate a pull request title: Write @sourcery-ai anywhere in the pull
    request title to generate a title at any time. You can also comment
    @sourcery-ai title on the pull request to (re-)generate the title at any time.
  • Generate a pull request summary: Write @sourcery-ai summary anywhere in
    the pull request body to generate a PR summary at any time exactly where you
    want it. You can also comment @sourcery-ai summary on the pull request to
    (re-)generate the summary at any time.
  • Generate reviewer's guide: Comment @sourcery-ai guide on the pull
    request to (re-)generate the reviewer's guide at any time.
  • Resolve all Sourcery comments: Comment @sourcery-ai resolve on the
    pull request to resolve all Sourcery comments. Useful if you've already
    addressed all the comments and don't want to see them anymore.
  • Dismiss all Sourcery reviews: Comment @sourcery-ai dismiss on the pull
    request to dismiss all existing Sourcery reviews. Especially useful if you
    want to start fresh with a new review - don't forget to comment
    @sourcery-ai review to trigger a new review!

Customizing Your Experience

Access your dashboard to:

  • Enable or disable review features such as the Sourcery-generated pull request
    summary, the reviewer's guide, and others.
  • Change the review language.
  • Add, remove or edit custom review instructions.
  • Adjust other review settings.

Getting Help

@sourcery-ai sourcery-ai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Hey - I've reviewed your changes and they look great!

Sourcery assessment

Approved.


Sourcery is free for open source - if you like our reviews please consider sharing them ✨

@codecov

codecov Bot commented Oct 12, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 99.90%. Comparing base (2fecd02) to head (08b0ebb).

Additional details and impacted files
@@           Coverage Diff           @@
##           master    #2014   +/-   ##
=======================================
  Coverage   99.90%   99.90%           
=======================================
  Files           9        9           
  Lines        2144     2145    +1     
=======================================
+ Hits         2142     2143    +1     
  Misses          2        2           

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

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.

1 participant