Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(vitest): close inspector immediately if run is canceled #5519

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
25 changes: 19 additions & 6 deletions packages/vitest/src/runtime/inspector.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { createRequire } from 'node:module'

import type { ContextRPC } from '../types'
import type { ContextRPC, ResolvedConfig } from '../types'

const __require = createRequire(import.meta.url)
let inspector: typeof import('node:inspector')
Expand Down Expand Up @@ -44,11 +44,7 @@ export function setupInspect(ctx: ContextRPC) {
}
}

// In watch mode the inspector can persist re-runs if isolation is disabled and a single worker is used
const isIsolatedSingleThread = config.pool === 'threads' && config.poolOptions?.threads?.isolate === false && config.poolOptions?.threads?.singleThread
const isIsolatedSingleFork = config.pool === 'forks' && config.poolOptions?.forks?.isolate === false && config.poolOptions?.forks?.singleFork

const keepOpen = config.watch && (isIsolatedSingleFork || isIsolatedSingleThread)
const keepOpen = shouldKeepOpen(config)

return function cleanup() {
if (isEnabled && !keepOpen && inspector) {
Expand All @@ -57,3 +53,20 @@ export function setupInspect(ctx: ContextRPC) {
}
}
}

export function closeInspector(config: ResolvedConfig) {
const keepOpen = shouldKeepOpen(config)

if (inspector && !keepOpen) {
inspector.close()
session?.disconnect()
}
}

function shouldKeepOpen(config: ResolvedConfig) {
// In watch mode the inspector can persist re-runs if isolation is disabled and a single worker is used
const isIsolatedSingleThread = config.pool === 'threads' && config.poolOptions?.threads?.isolate === false && config.poolOptions?.threads?.singleThread
const isIsolatedSingleFork = config.pool === 'forks' && config.poolOptions?.forks?.isolate === false && config.poolOptions?.forks?.singleFork

return config.watch && (isIsolatedSingleFork || isIsolatedSingleThread)
}
6 changes: 5 additions & 1 deletion packages/vitest/src/runtime/runBaseTests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import { setupChaiConfig } from '../integrations/chai/config'
import { setupGlobalEnv, withEnv } from './setup-node'
import type { VitestExecutor } from './execute'
import { resolveTestRunner } from './runners'
import { closeInspector } from './inspector'

// browser shouldn't call this!
export async function run(files: string[], config: ResolvedConfig, environment: ResolvedTestEnvironment, executor: VitestExecutor): Promise<void> {
Expand All @@ -21,7 +22,10 @@ export async function run(files: string[], config: ResolvedConfig, environment:

const runner = await resolveTestRunner(config, executor)

workerState.onCancel.then(reason => runner.onCancel?.(reason))
workerState.onCancel.then((reason) => {
closeInspector(config)
runner.onCancel?.(reason)
})

workerState.durations.prepare = performance.now() - workerState.durations.prepare
workerState.durations.environment = performance.now()
Expand Down
6 changes: 6 additions & 0 deletions packages/vitest/src/runtime/runVmTests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import * as VitestIndex from '../index'
import type { VitestExecutor } from './execute'
import { resolveTestRunner } from './runners'
import { setupCommonEnv } from './setup-common'
import { closeInspector } from './inspector'

export async function run(files: string[], config: ResolvedConfig, executor: VitestExecutor): Promise<void> {
const workerState = getWorkerState()
Expand Down Expand Up @@ -56,6 +57,11 @@ export async function run(files: string[], config: ResolvedConfig, executor: Vit

const runner = await resolveTestRunner(config, executor)

workerState.onCancel.then((reason) => {
closeInspector(config)
runner.onCancel?.(reason)
})

workerState.durations.prepare = performance.now() - workerState.durations.prepare

const { vi } = VitestIndex
Expand Down