GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — fix: respect bench abort signals in task concurrency (#738) · tinylibs/tinybench@1e5e66a

File tree

Expand file treeCollapse file tree
Open diff view settings
Filter options
  • src
    • concurrency.ts
    • task.ts
  • test
    • task-level-abort.test.ts
    • with-concurrency.test.ts
Expand file treeCollapse file tree
Open diff view settings
Collapse file

‎src/concurrency.ts‎

Copy file name to clipboardExpand all lines: src/concurrency.ts
+9-5Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,10 @@ interface WithConcurrencyOptions<R> {
1818
*/
1919
limit: number
2020
/**
21-
* An optional AbortSignal to cancel the execution.
21+
* An AbortSignal or a list of signals; aborting any of them cancels the
22+
* execution.
2223
*/
23-
signal?: AbortSignal | undefined
24+
signal?: AbortSignal | AbortSignal[] | undefined
2425
/**
2526
* Time budget for starting iterations, in milliseconds. Already-started
2627
* calls are awaited without a completion deadline. Zero disables this
@@ -88,9 +89,10 @@ export const withConcurrency = async <R>(
8889
}
8990

9091
const onAbort = () => (isRunning = false)
92+
const signals = Array.isArray(signal) ? signal : signal ? [signal] : []
9193

92-
if (signal) {
93-
if (signal.aborted) return []
94+
if (signals.some(signal => signal.aborted)) return []
95+
for (const signal of signals) {
9496
signal.addEventListener('abort', onAbort)
9597
}
9698

@@ -114,7 +116,9 @@ export const withConcurrency = async <R>(
114116
const promises = Array.from({ length: maxWorkers }, () => worker())
115117
await Promise.allSettled(promises)
116118
} finally {
117-
signal?.removeEventListener('abort', onAbort)
119+
for (const signal of signals) {
120+
signal.removeEventListener('abort', onAbort)
121+
}
118122
}
119123

120124
if (errors.length === 0) return results
Collapse file

‎src/task.ts‎

Copy file name to clipboardExpand all lines: src/task.ts
+3-1Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -414,7 +414,9 @@ export class Task extends EventTarget {
414414
fn: benchmarkTask,
415415
iterations,
416416
limit: Math.max(1, Math.floor(this.#bench.threshold)),
417-
signal: this.#signal ?? this.#bench.signal,
417+
signal: [this.#signal, this.#bench.signal].filter(
418+
signal => signal != null
419+
),
418420
time,
419421
timestampProvider: this.#timestampProvider,
420422
})
Collapse file

‎test/task-level-abort.test.ts‎

Copy file name to clipboardExpand all lines: test/task-level-abort.test.ts
+36Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,42 @@ import { expect, test } from 'vitest'
33
import { Bench } from '../src'
44
import { sleep } from './utils'
55

6+
test.each([
7+
['bench', 'run'],
8+
['task', 'run'],
9+
['bench', 'warmup'],
10+
['task', 'warmup'],
11+
])('stops concurrent scheduling on %s abort during %s', async (source, phase) => {
12+
const benchController = new AbortController()
13+
const taskController = new AbortController()
14+
let clockCalls = 0
15+
let taskCalls = 0
16+
const bench = new Bench({
17+
concurrency: 'task',
18+
iterations: 0,
19+
now: () => ++clockCalls,
20+
signal: benchController.signal,
21+
threshold: 2,
22+
time: 10000,
23+
warmup: phase === 'warmup',
24+
warmupIterations: 0,
25+
warmupTime: 10000,
26+
})
27+
28+
bench.add('task', async () => {
29+
taskCalls++
30+
const controller = source === 'bench' ? benchController : taskController
31+
controller.abort()
32+
await Promise.resolve()
33+
}, { signal: taskController.signal })
34+
35+
await bench.run()
36+
37+
expect(taskCalls).toBe(1)
38+
expect(clockCalls).toBeLessThan(20)
39+
expect(bench.getTask('task')?.result.state).toMatch(/^aborted/)
40+
})
41+
642
test('abort individual task (async)', async () => {
743
const controller = new AbortController()
844
const bench = new Bench({ iterations: 16, time: 100 })
Collapse file

‎test/with-concurrency.test.ts‎

Copy file name to clipboardExpand all lines: test/with-concurrency.test.ts
+47Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,53 @@ import { performanceNowTimestampProvider } from '../src/timestamp'
55
import { hrtimeNow, withConcurrency } from '../src/utils'
66
import { asyncSleep } from './utils'
77

8+
test.each(['completed', 'errored', 'aborted'])(
9+
'removes all abort listeners after a %s run',
10+
async state => {
11+
const first = new AbortController()
12+
const second = new AbortController()
13+
const failure = new Error('failure')
14+
const run = withConcurrency({
15+
fn: async () => {
16+
await Promise.resolve()
17+
if (state === 'errored') throw failure
18+
if (state === 'aborted') second.abort()
19+
return 1
20+
},
21+
iterations: 1,
22+
limit: 1,
23+
signal: [first.signal, second.signal],
24+
})
25+
26+
if (state === 'errored') await expect(run).rejects.toBe(failure)
27+
else expect(await run).toEqual(state === 'aborted' ? [] : [1])
28+
29+
expect(getEventListeners(first.signal, 'abort')).toEqual([])
30+
expect(getEventListeners(second.signal, 'abort')).toEqual([])
31+
}
32+
)
33+
34+
test.each([0, 1])('does not add listeners when signal %i was aborted', async index => {
35+
const controllers = [new AbortController(), new AbortController()]
36+
controllers[index]?.abort()
37+
let calls = 0
38+
const results = await withConcurrency({
39+
fn: async () => {
40+
calls++
41+
await Promise.resolve()
42+
},
43+
iterations: 1,
44+
limit: 1,
45+
signal: controllers.map(controller => controller.signal),
46+
})
47+
48+
expect(results).toEqual([])
49+
expect(calls).toBe(0)
50+
for (const controller of controllers) {
51+
expect(getEventListeners(controller.signal, 'abort')).toEqual([])
52+
}
53+
})
54+
855
test.each(['completed', 'errored', 'aborted'])(
956
'removes its abort listener after a %s run',
1057
async state => {

0 commit comments

Comments
0 (0)
You are visitor 01402775 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.