Skip to content

Commit 6d7f0a9

Browse files
authored
fix: remove onCancel when worker is terminated (#9033)
1 parent 62fab24 commit 6d7f0a9

File tree

3 files changed

+17
-8
lines changed

3 files changed

+17
-8
lines changed

docs/api/advanced/vitest.md

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -458,11 +458,15 @@ Register a handler that will be called when the server is restarted due to a con
458458
## onCancel
459459

460460
```ts
461-
function onCancel(fn: (reason: CancelReason) => Awaitable<void>): void
461+
function onCancel(fn: (reason: CancelReason) => Awaitable<void>): () => void
462462
```
463463

464464
Register a handler that will be called when the test run is cancelled with [`vitest.cancelCurrentRun`](#cancelcurrentrun).
465465

466+
::: warning EXPERIMENTAL
467+
Since 4.0.10, `onCancel` returns a teardown function that will remove the listener.
468+
:::
469+
466470
## onClose
467471

468472
```ts

packages/vitest/src/node/core.ts

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,7 @@ export class Vitest {
139139
private _onRestartListeners: OnServerRestartHandler[] = []
140140
private _onClose: (() => Awaitable<void>)[] = []
141141
private _onSetServer: OnServerRestartHandler[] = []
142-
private _onCancelListeners: ((reason: CancelReason) => Awaitable<void>)[] = []
142+
private _onCancelListeners = new Set<(reason: CancelReason) => Awaitable<void>>()
143143
private _onUserTestsRerun: OnTestsRerunHandler[] = []
144144
private _onFilterWatchedSpecification: ((spec: TestSpecification) => boolean)[] = []
145145

@@ -706,7 +706,7 @@ export class Vitest {
706706

707707
// previous run
708708
await this.runningPromise
709-
this._onCancelListeners = []
709+
this._onCancelListeners.clear()
710710
this.isCancelling = false
711711

712712
// schedule the new run
@@ -807,7 +807,7 @@ export class Vitest {
807807

808808
// previous run
809809
await this.runningPromise
810-
this._onCancelListeners = []
810+
this._onCancelListeners.clear()
811811
this.isCancelling = false
812812

813813
// schedule the new run
@@ -859,7 +859,7 @@ export class Vitest {
859859
*/
860860
async cancelCurrentRun(reason: CancelReason): Promise<void> {
861861
this.isCancelling = true
862-
await Promise.all(this._onCancelListeners.splice(0).map(listener => listener(reason)))
862+
await Promise.all([...this._onCancelListeners].map(listener => listener(reason)))
863863
await this.runningPromise
864864
}
865865

@@ -1257,8 +1257,11 @@ export class Vitest {
12571257
/**
12581258
* Register a handler that will be called when the test run is cancelled with `vitest.cancelCurrentRun`.
12591259
*/
1260-
onCancel(fn: (reason: CancelReason) => Awaitable<void>): void {
1261-
this._onCancelListeners.push(fn)
1260+
onCancel(fn: (reason: CancelReason) => Awaitable<void>): () => void {
1261+
this._onCancelListeners.add(fn)
1262+
return () => {
1263+
this._onCancelListeners.delete(fn)
1264+
}
12621265
}
12631266

12641267
/**

packages/vitest/src/node/pools/poolRunner.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ export class PoolRunner {
3737
rpc: [unknown]
3838
}> = new EventEmitter()
3939

40+
private _offCancel: () => void
4041
private _rpc: BirpcReturn<RunnerRPC, RuntimeRPC>
4142

4243
public get isTerminated(): boolean {
@@ -63,7 +64,7 @@ export class PoolRunner {
6364
},
6465
)
6566

66-
this.project.vitest.onCancel(reason => this._rpc.onCancel(reason))
67+
this._offCancel = this.project.vitest.onCancel(reason => this._rpc.onCancel(reason))
6768
}
6869

6970
postMessage(message: WorkerRequest): void {
@@ -181,6 +182,7 @@ export class PoolRunner {
181182
)
182183

183184
this._eventEmitter.removeAllListeners()
185+
this._offCancel()
184186
this._rpc.$close(new Error('[vitest-pool-runner]: Pending methods while closing rpc'))
185187

186188
// Stop the worker process (this sets _fork/_thread to undefined)

0 commit comments

Comments
 (0)