11import { createRequire } from 'node:module'
22
3- import type { ContextRPC } from '../types'
3+ import type { ContextRPC , ResolvedConfig } from '../types'
44
55const __require = createRequire ( import . meta. url )
66let inspector : typeof import ( 'node:inspector' )
@@ -44,11 +44,7 @@ export function setupInspect(ctx: ContextRPC) {
4444 }
4545 }
4646
47- // In watch mode the inspector can persist re-runs if isolation is disabled and a single worker is used
48- const isIsolatedSingleThread = config . pool === 'threads' && config . poolOptions ?. threads ?. isolate === false && config . poolOptions ?. threads ?. singleThread
49- const isIsolatedSingleFork = config . pool === 'forks' && config . poolOptions ?. forks ?. isolate === false && config . poolOptions ?. forks ?. singleFork
50-
51- const keepOpen = config . watch && ( isIsolatedSingleFork || isIsolatedSingleThread )
47+ const keepOpen = shouldKeepOpen ( config )
5248
5349 return function cleanup ( ) {
5450 if ( isEnabled && ! keepOpen && inspector ) {
@@ -57,3 +53,20 @@ export function setupInspect(ctx: ContextRPC) {
5753 }
5854 }
5955}
56+
57+ export function closeInspector ( config : ResolvedConfig ) {
58+ const keepOpen = shouldKeepOpen ( config )
59+
60+ if ( inspector && ! keepOpen ) {
61+ inspector . close ( )
62+ session ?. disconnect ( )
63+ }
64+ }
65+
66+ function shouldKeepOpen ( config : ResolvedConfig ) {
67+ // In watch mode the inspector can persist re-runs if isolation is disabled and a single worker is used
68+ const isIsolatedSingleThread = config . pool === 'threads' && config . poolOptions ?. threads ?. isolate === false && config . poolOptions ?. threads ?. singleThread
69+ const isIsolatedSingleFork = config . pool === 'forks' && config . poolOptions ?. forks ?. isolate === false && config . poolOptions ?. forks ?. singleFork
70+
71+ return config . watch && ( isIsolatedSingleFork || isIsolatedSingleThread )
72+ }
0 commit comments