GeezerVison is showing github.com — fix: consume the promise returned by a thenable's then call (#743) · tinylibs/tinybench@15a2408
File treeExpand file treeCollapse file tree Open diff view settings Expand file treeCollapse file tree Open diff view settings Collapse file Copy file name to clipboardExpand all lines: src/async.ts +20-5Lines changed: 20 additions & 5 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -15,6 +15,25 @@ export const isPromiseLike = <T>( | 15 | 15 | typeof maybePromiseLike === 'function') && | 16 | 16 | typeof (maybePromiseLike as PromiseLike<T>).then === 'function' | 17 | 17 |
| | 18 | +/** | | 19 | + * Suppresses rejections of a promise-like value, and of the promise its own | | 20 | + * `then` call returns, so neither surfaces as an unhandled rejection. | | 21 | + * @param value - the promise-like value to mark handled | | 22 | + */ | | 23 | +export const markHandled = (value: unknown): void => { | | 24 | + try { | | 25 | + const returned = (value as PromiseLike<unknown>).then( | | 26 | + emptyFunction, | | 27 | + emptyFunction | | 28 | + ) | | 29 | + if (isPromiseLike(returned)) { | | 30 | + Promise.resolve(returned).catch(emptyFunction) | | 31 | + } | | 32 | + } catch { | | 33 | + // Treat a throwing `then` as handled | | 34 | + } | | 35 | +} | | 36 | + | 18 | 37 | type AsyncFunctionType<A extends unknown[], R> = (...args: A) => PromiseLike<R> | 19 | 38 |
| 20 | 39 | const AsyncFunctionConstructor = (async () => { | @@ -48,11 +67,7 @@ export const isFnAsyncResource = (fn: Fn | null | undefined): boolean => { | 48 | 67 | const promiseLike = isPromiseLike(fnCall) | 49 | 68 | if (promiseLike) { | 50 | 69 | // silence promise rejection | 51 | | - try { | 52 | | - (fnCall.then(emptyFunction) as Promise<unknown>).catch(emptyFunction) | 53 | | - } catch { | 54 | | - // ignore | 55 | | - } | | 70 | + markHandled(fnCall) | 56 | 71 | } | 57 | 72 | return promiseLike | 58 | 73 | } catch { | |
Collapse file Copy file name to clipboardExpand all lines: src/task.ts +14-8Lines changed: 14 additions & 8 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -18,7 +18,6 @@ import type { | 18 | 18 | TimestampValue, | 19 | 19 | } from './types' | 20 | 20 |
| 21 | | -import { emptyFunction } from './constants' | 22 | 21 | import { BenchEvent } from './event' | 23 | 22 | import { | 24 | 23 | assert, | @@ -28,6 +27,7 @@ import { | 28 | 27 | isFnAsyncResource, | 29 | 28 | isPromiseLike, | 30 | 29 | isValidSamples, | | 30 | + markHandled, | 31 | 31 | medianAbsoluteDeviation, | 32 | 32 | sortSamples, | 33 | 33 | toError, | @@ -50,7 +50,7 @@ const hookNames = ['afterAll', 'beforeAll', 'beforeEach', 'afterEach'] as const | 50 | 50 | const assertSyncResult = (result: unknown, message: string): void => { | 51 | 51 | const isAsync = isPromiseLike(result) | 52 | 52 | if (isAsync) { | 53 | | - Promise.resolve(result).catch(emptyFunction) | | 53 | + markHandled(result) | 54 | 54 | } | 55 | 55 | assert(!isAsync, message) | 56 | 56 | } | @@ -785,6 +785,14 @@ export class Task extends EventTarget { | 785 | 785 | } | 786 | 786 | } | 787 | 787 |
| | 788 | +/** | | 789 | + * Whether a value is a finite, non-negative duration in milliseconds. | | 790 | + * @param value - the value to check | | 791 | + * @returns whether the value can be used as an overridden duration or cost | | 792 | + */ | | 793 | +const isValidDuration = (value: unknown): value is number => | | 794 | + typeof value === 'number' && Number.isFinite(value) && value >= 0 | | 795 | + | 788 | 796 | /** | 789 | 797 | * Extracts a finite, non-negative `overriddenDuration`, otherwise undefined. | 790 | 798 | * Checks own and inherited properties, reads the value once and propagates | @@ -806,14 +814,14 @@ function getOverriddenDurationFromFnResult ( | 806 | 814 | if (typeof value !== 'number') { | 807 | 815 | try { | 808 | 816 | if (isPromiseLike(value)) { | 809 | | - Promise.resolve(value).catch(emptyFunction) | | 817 | + markHandled(value) | 810 | 818 | } | 811 | 819 | } catch { | 812 | 820 | // Treat access errors as absence | 813 | 821 | } | 814 | 822 | return undefined | 815 | 823 | } | 816 | | - return Number.isFinite(value) && value >= 0 ? value : undefined | | 824 | + return isValidDuration(value) ? value : undefined | 817 | 825 | } | 818 | 826 |
| 819 | 827 | /** | @@ -837,11 +845,9 @@ function getOverriddenIterationCostFromFnResult ( | 837 | 845 | } | 838 | 846 | const value = record.overriddenIterationCost | 839 | 847 | if (typeof value !== 'number' && isPromiseLike(value)) { | 840 | | - Promise.resolve(value).catch(emptyFunction) | | 848 | + markHandled(value) | 841 | 849 | } | 842 | | - return typeof value === 'number' && Number.isFinite(value) && value >= 0 | 843 | | - ? value | 844 | | - : undefined | | 850 | + return isValidDuration(value) ? value : undefined | 845 | 851 | } catch { | 846 | 852 | return undefined | 847 | 853 | } | |
Collapse file Copy file name to clipboardExpand all lines: src/utils.ts +1-1Lines changed: 1 addition & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
| 1 | | -export { isFnAsyncResource, isPromiseLike } from './async' | | 1 | +export { isFnAsyncResource, isPromiseLike, markHandled } from './async' | 2 | 2 | export { withConcurrency } from './concurrency' | 3 | 3 | export { defaultConvertTaskResultForConsoleTable } from './console-table' | 4 | 4 | export { mToMs, mToNs, mToNsBigint, nToMs } from './conversions' | |
Collapse file Copy file name to clipboardExpand all lines: test/run-sync-hook-rejection.test.ts +28Lines changed: 28 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -124,3 +124,31 @@ test.each([ | 124 | 124 | ) | 125 | 125 | } | 126 | 126 | ) | | 127 | + | | 128 | +test('a task returning a thenable whose `then` call rejects leaks no unhandled rejection', async () => { | | 129 | + const thenable = { | | 130 | + then (resolve: (value?: unknown) => void): Promise<void> { | | 131 | + resolve(undefined) | | 132 | + return Promise.reject(new Error('late')) | | 133 | + }, | | 134 | + } | | 135 | + const bench = new Bench({ warmup: false }) | | 136 | + bench.add( | | 137 | + 'task', | | 138 | + () => thenable, | | 139 | + { async: false } | | 140 | + ) | | 141 | + | | 142 | + const { reasons, stop } = listenToUnhandledRejections() | | 143 | + await bench.run() | | 144 | + await flushTurns(3) | | 145 | + stop() | | 146 | + | | 147 | + expect(reasons).toEqual([]) | | 148 | + expect(bench.getTask('task')?.result).toMatchObject({ | | 149 | + error: { | | 150 | + message: 'task function must be sync when using `runSync()`', | | 151 | + }, | | 152 | + state: 'errored', | | 153 | + }) | | 154 | +}) |
|