Skip to content

fix(mock): guard aborted requests in async reply callback rejection - #5764

Open
pacocartones wants to merge 1 commit into
nodejs:mainfrom
pacocartones:fix/mock-options-callback-abort-guard-v2
Open

fix(mock): guard aborted requests in async reply callback rejection#5764
pacocartones wants to merge 1 commit into
nodejs:mainfrom
pacocartones:fix/mock-options-callback-abort-guard-v2

Conversation

@pacocartones

Copy link
Copy Markdown
Contributor

Problem

In lib/mock/mock-utils.js, sendReply() handles an async reply options callback (the .reply(() => ...) / response.callback path from #5534). Its resolve arm goes through handleReply() (guarded by if (aborted) return), but the reject arm called handler.onResponseError(null, err) with no abort guard.

When a request with a body is aborted while that options-callback promise is still pending, controller.abort() already settles the request via onResponseError (first terminal). The callback promise then rejects and calls onResponseError again → a double-settle of an already-aborted request.

This is distinct from #5762: that one guards the data/body function promise in handleReply(); this guards the reply options callback promise in sendReply() — different function, different call site, different feature.

Fix

Guard the reject arm with if (aborted) return, mirroring the guard handleReply() already applies.

Test

test/mock-delayed-abort.js: a CountingHandler counts onResponseError; a POST with a body and an options callback that rejects at 50ms, aborted at 10ms, asserts onResponseError fires exactly once.

  • Before: 2 !== 1 (double-settle).
  • After: exactly once.

node --test test/mock-delayed-abort.js (4 pass), whole mock suite via borp (340 pass), npm run lint — all green.

When a reply options callback returns a promise, the request can be
aborted while that promise is still pending. controller.abort() then
settles the request through onResponseError. If the callback promise
later rejects, the rejection arm called handler.onResponseError(null,
err) again, double-settling an already-aborted request.

Add the same `if (aborted) return` guard that handleReply() already uses
so the late rejection is dropped once the request has been aborted.

Signed-off-by: Paco Cartones <pacocartones@users.noreply.github.com>
@codecov-commenter

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 93.52%. Comparing base (17117f6) to head (60b61f6).

Additional details and impacted files
@@            Coverage Diff             @@
##             main    #5764      +/-   ##
==========================================
+ Coverage   93.50%   93.52%   +0.01%     
==========================================
  Files         110      110              
  Lines       39072    39080       +8     
==========================================
+ Hits        36534    36549      +15     
+ Misses       2538     2531       -7     

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

@pacocartones

Copy link
Copy Markdown
Contributor Author

The only non-green checks are two cancelled CI jobs (Node.js 24 Ubuntu coverage and Node.js 22 Ubuntu). I tried the Actions rerun endpoint, but contributors receive HTTP 403. Could a maintainer rerun the cancelled jobs when convenient?

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