Skip to content

cacheable-request - fix: keyv error listener leak on every request#1687

Merged
jaredwray merged 2 commits into
mainfrom
claude/cacheable-issue-1685-33dija
Jul 15, 2026
Merged

cacheable-request - fix: keyv error listener leak on every request#1687
jaredwray merged 2 commits into
mainfrom
claude/cacheable-issue-1685-33dija

Conversation

@jaredwray

Copy link
Copy Markdown
Owner

Please check if the PR fulfills these requirements

  • Followed the Contributing guidelines and Code of Conduct
  • Tests for the changes have been added (for bug fixes/features) with 100% code coverage.

What kind of change does this PR introduce? (Bug fix, feature, docs update, ...)

Bug fix — fixes #1685.

cacheable-request registered its per-request Keyv error handler with cachek.once("error", errorHandler) and later tried to clean it up with cachek.removeListener("error", errorHandler). Keyv v5's custom event manager wraps once() listeners in an anonymous function and removeListener() matches by exact reference, so the wrapper was never removed — one error listener leaked per request, triggering MaxListenersExceededWarning: Possible event memory leak detected. 101 error listeners added. after 100 requests. This is a regression of the original leak fix (jaredwray/cacheable-request#222 / #223) after the Keyv 5 migration.

Changes in packages/cacheable-request/src/index.ts:

  • Register the handler with cachek.on("error", errorHandler) so the shared removeListener cleanup removes the exact same reference. Effective once-semantics are preserved: forwarding a Keyv error emits error on the request emitter, which itself removes the Keyv listener.
  • Also remove the handler on the underlying request's error, abort, and close events, so aborted or destroyed requests that never emit response/error on the emitter no longer leak the listener.

Tests (packages/cacheable-request/test/cacheable-request-instance.test.ts):

  • New regression test asserting the Keyv error listener count returns to baseline after sequential requests across both the network and cached-response paths (fails with 6 leaked listeners before the fix).
  • New regression test asserting the listener is removed when the request is aborted before a response (fails with 1 leaked listener before the fix).
  • Verified the exact reproduction script from cacheable-request leaks Keyv error listeners on every request with Keyv v5 #1685 against the built package: after 110 sequential requests there are 0 residual listeners and no MaxListenersExceededWarning.

pnpm test for the package passes (67 tests; statements/lines remain at 100%, branch coverage improves 93.97% → 96.38%). The only failure in this environment is the pre-existing return with url and port test, which requires outbound network access to mockhttp.org and fails identically on unmodified main here.

🤖 Generated with Claude Code

https://claude.ai/code/session_011tF3VefnzwcH6TQo48uJHQ


Generated by Claude Code

Keyv v5's event manager wraps once() listeners in an anonymous function,
so the existing removeListener(error, errorHandler) cleanup never matched
the registered wrapper and one error listener leaked per request, hitting
MaxListenersExceededWarning after 100 requests.

Register the handler with on() so the cleanup can remove the exact same
reference, and also remove it on the underlying request's error/abort/
close events so aborted or destroyed requests that never emit response
do not leak either.

Fixes #1685

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_011tF3VefnzwcH6TQo48uJHQ

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Code Review

This pull request addresses a memory leak where Keyv error listeners were not being properly unregistered upon request completion or abortion. It introduces a unified cleanup handler registered across multiple termination events and adds corresponding unit tests. The reviewer identified a potential issue where registering once listeners on the underlying request object without cleaning them up when the request succeeds could still lead to dangling listeners and a memory leak, and provided a code suggestion to clean up those listeners on any termination path.

Comment thread packages/cacheable-request/src/index.ts
@codecov

codecov Bot commented Jul 15, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 100.00%. Comparing base (d93554f) to head (43aec20).

Additional details and impacted files
@@            Coverage Diff            @@
##              main     #1687   +/-   ##
=========================================
  Coverage   100.00%   100.00%           
=========================================
  Files           29        29           
  Lines         3504      3513    +9     
  Branches       794       809   +15     
=========================================
+ Hits          3504      3513    +9     

☔ 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.

…up runs

Track the in-flight request and remove its error/abort/close cleanup
listeners inside removeErrorHandler, so the closure over the emitter is
released deterministically once the request settles instead of waiting
for the request object to be garbage collected.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_011tF3VefnzwcH6TQo48uJHQ
@jaredwray jaredwray merged commit f50b770 into main Jul 15, 2026
12 checks passed
@jaredwray jaredwray deleted the claude/cacheable-issue-1685-33dija branch July 15, 2026 18:26
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.

cacheable-request leaks Keyv error listeners on every request with Keyv v5

2 participants