Skip to content

Commit

Permalink
feat: process timeout to log names of stuck test files (#3031)
Browse files Browse the repository at this point in the history
  • Loading branch information
AriPerkkio authored Mar 20, 2023
1 parent c7f0c86 commit 0ddf722
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 5 deletions.
2 changes: 1 addition & 1 deletion packages/vitest/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@
"std-env": "^3.3.1",
"strip-literal": "^1.0.0",
"tinybench": "^2.3.1",
"tinypool": "^0.3.1",
"tinypool": "^0.4.0",
"tinyspy": "^1.0.2",
"vite": "^3.0.0 || ^4.0.0",
"vite-node": "workspace:*",
Expand Down
1 change: 1 addition & 0 deletions packages/vitest/src/node/core.ts
Original file line number Diff line number Diff line change
Expand Up @@ -597,6 +597,7 @@ export class Vitest {
setTimeout(() => {
this.report('onProcessTimeout').then(() => {
console.warn(`close timed out after ${this.config.teardownTimeout}ms`)
this.state.getProcessTimeoutCauses().forEach(cause => console.warn(cause))
process.exit()
})
}, this.config.teardownTimeout).unref()
Expand Down
9 changes: 9 additions & 0 deletions packages/vitest/src/node/pools/threads.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,8 @@ export function createThreadsPool(ctx: Vitest, { execArgv, env }: PoolProcessOpt

env,
execArgv,

terminateTimeout: ctx.config.teardownTimeout,
}

if (ctx.config.isolate) {
Expand Down Expand Up @@ -87,6 +89,13 @@ export function createThreadsPool(ctx: Vitest, { execArgv, env }: PoolProcessOpt
try {
await pool.run(data, { transferList: [workerPort], name })
}
catch (error) {
// Worker got stuck and won't terminate - this may cause process to hang
if (error instanceof Error && /Failed to terminate worker/.test(error.message))
ctx.state.addProcessTimeoutCause(`Failed to terminate worker while running ${files.join(', ')}.`)
else
throw error
}
finally {
port.close()
workerPort.close()
Expand Down
9 changes: 9 additions & 0 deletions packages/vitest/src/node/state.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ export class StateManager {
idMap = new Map<string, Task>()
taskFileMap = new WeakMap<Task, File>()
errorsSet = new Set<unknown>()
processTimeoutCauses = new Set<string>()

catchError(err: unknown, type: string): void {
if (isAggregateError(err))
Expand All @@ -39,6 +40,14 @@ export class StateManager {
return Array.from(this.errorsSet.values())
}

addProcessTimeoutCause(cause: string) {
this.processTimeoutCauses.add(cause)
}

getProcessTimeoutCauses() {
return Array.from(this.processTimeoutCauses.values())
}

getPaths() {
return Array.from(this.pathsSet)
}
Expand Down
8 changes: 4 additions & 4 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 0ddf722

Please sign in to comment.