perf_hooks: fix timerify() inconsistency for rejected thenables - #65120
Open
batuhan-bas wants to merge 1 commit into
Open
perf_hooks: fix timerify() inconsistency for rejected thenables#65120batuhan-bas wants to merge 1 commit into
batuhan-bas wants to merge 1 commit into
Conversation
timerify() used Promise.prototype.finally() to record a function's duration and histogram entry once its returned thenable settled, regardless of whether it fulfilled or rejected. This meant a timerified async function that throws would still be recorded, while a timerified sync function that throws never reaches the recording step at all, since it throws before returning. Switch to then() with only an onFulfilled handler so a rejected thenable behaves like a synchronous throw: no performance entry and no histogram record, and the rejection still propagates unchanged. Fixes: nodejs#42743
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
timerify()behaves inconsistently depending on whether the wrappedfunction throws synchronously or returns a rejected thenable:
g1()throws before the code ever reaches the recording step, sonothing is recorded.
g2()returns a rejected promise, andtimerify()records viaPromise.prototype.finally(), which runs onboth fulfillment and rejection — so the async throw is recorded.
Fix
Use
.then()with only anonFulfilledhandler instead of.finally(). This records only on fulfillment, matching thesynchronous case, while the rejection still propagates through the
returned promise unchanged. It also avoids assuming the returned
thenable implements
finally(), which isn't part of the minimalthenable contract (only
then()is required).Test plan
test/parallel/test-perf-hooks-timerify-async-error.js,asserting a rejected thenable produces no histogram record and no
'function'performance entry.test-perf-hooks-timerify-*suite locally,all passing.
Fixes: #42743