Skip to content

Conversation

@yowainwright
Copy link
Member

@yowainwright yowainwright commented Jul 13, 2025

Description

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.

Checklist for 1834 (before re-pinging team)

@codecov
Copy link

codecov bot commented Jul 13, 2025

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 99.90%. Comparing base (ffd497a) to head (954a474).
⚠️ Report is 1 commits behind head on master.

Additional details and impacted files
@@           Coverage Diff           @@
##           master    #1893   +/-   ##
=======================================
  Coverage   99.90%   99.90%           
=======================================
  Files           9        9           
  Lines        2060     2074   +14     
=======================================
+ Hits         2058     2072   +14     
  Misses          2        2           

☔ View full report in Codecov by Sentry.
📢 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.

@yowainwright yowainwright force-pushed the ISSUE-1834 branch 7 times, most recently from f809804 to 7c93f8d Compare July 13, 2025 22:41
@yowainwright yowainwright marked this pull request as ready for review July 13, 2025 22:44
@yowainwright yowainwright requested a review from jonathanong July 13, 2025 22:44
@yowainwright yowainwright requested a review from fengmk2 August 23, 2025 04:06
@yowainwright yowainwright force-pushed the ISSUE-1834 branch 3 times, most recently from 777a840 to 8dc41ab Compare October 19, 2025 07:41
@yowainwright
Copy link
Member Author

@fengmk2 could you review this as you have time? 🙏 🙇
happy to help elsewhere! 👍
Thanks for all of your efforts!

@fengmk2
Copy link
Member

fengmk2 commented Oct 19, 2025

Is it the same issue fixed by #1889?

@yowainwright
Copy link
Member Author

@fengmk2 thanks for your thoughtfulness and time!
Will review #1889 and your comments and update accordingly. 🙇

@yowainwright yowainwright force-pushed the ISSUE-1834 branch 2 times, most recently from 60cb0ce to 4670ea1 Compare October 20, 2025 05:48
@yowainwright
Copy link
Member Author

@fengmk2 updated.

@fengmk2 fengmk2 changed the title chore: fixes mem leak relating to issue-1834 fix: fixes mem leak relating to issue-1834 Oct 22, 2025
@fengmk2
Copy link
Member

fengmk2 commented Oct 22, 2025

@jonathanong Do you want to take another look?

@fengmk2 fengmk2 changed the title fix: fixes mem leak relating to issue-1834 feat: fixes mem leak relating to issue-1834 Oct 22, 2025
@fengmk2
Copy link
Member

fengmk2 commented Oct 22, 2025

I will merge this fix as a better stream handler and release it as a minor version.

It's a feature better than a bug :)🤣

@fengmk2 fengmk2 self-assigned this Oct 22, 2025
@fengmk2
Copy link
Member

fengmk2 commented Oct 22, 2025

@yowainwright Thank you for your excellent contribution!

@fengmk2 fengmk2 mentioned this pull request Oct 22, 2025
6 tasks
@fengmk2 fengmk2 requested a review from Copilot October 22, 2025 17:08
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull Request Overview

This PR addresses memory leaks related to stream handling in Koa by replacing the .pipe() method with Stream.pipeline() and improving stream cleanup. The changes ensure proper stream destruction and error handling to prevent resource leaks when streams are replaced or responses are aborted.

Key changes:

  • Replaced .pipe() with Stream.pipeline() for automatic stream cleanup and error handling
  • Added explicit cleanup of original streams when the response body is set to null
  • Removed manual error listeners on streams since pipeline handles errors automatically

Reviewed Changes

Copilot reviewed 4 out of 4 changed files in this pull request and generated 2 comments.

File Description
lib/response.js Adds cleanup logic to destroy original streams when body is nullified, removes manual error listener attachment
lib/application.js Refactors stream handling to use Stream.pipeline() instead of .pipe() for proper cleanup and error propagation
tests/response/body.test.js Updates test expectations to reflect removal of manual error listeners on streams
tests/application/respond.test.js Adds comprehensive test coverage for ReadableStream, Response objects, and pipeline error handling scenarios

Tip: Customize your code reviews with copilot-instructions.md. Create the file or learn how to get started.

@yowainwright yowainwright merged commit 49a3207 into master Oct 22, 2025
6 checks passed
@yowainwright yowainwright deleted the ISSUE-1834 branch October 22, 2025 18:50
@fengmk2
Copy link
Member

fengmk2 commented Oct 23, 2025

@yowainwright If no one gets feedback this week, I'll release this change on Sunday.

if (isStream(val)) {
onFinish(this.res, destroy.bind(null, val))
if (original !== val) {
val.once('error', err => this.ctx.onerror(err))
Copy link
Member

Choose a reason for hiding this comment

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

@yowainwright I consecutively set 2 streams that will cause exceptions, deleting here is problematic.

ctx.body = fs.createReadStream('does not exist');
ctx.body = fs.createReadStream('does not exist');

will throw unhandled error: Error: ENOENT: no such file or directory, open 'does not exist'

Copy link
Member Author

@yowainwright yowainwright Oct 23, 2025

Choose a reason for hiding this comment

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

😳 Looking! Do you have anymore initial insights? Will dig into this shortly.
Update here @fengmk2 .

Copy link
Member Author

Choose a reason for hiding this comment

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

@fengmk2
Copy link
Member

fengmk2 commented Oct 26, 2025

v3.1.0 released https://github.com/koajs/koa/releases/tag/v3.1.0

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

Projects

None yet

3 participants