fix: time out wsl hook commands (#3827)
This commit is contained in:
@@ -749,6 +749,52 @@ describe('runHook', () => {
|
||||
})
|
||||
}
|
||||
})
|
||||
|
||||
it('settles WSL hooks when wsl.exe never reports completion', async () => {
|
||||
vi.useFakeTimers()
|
||||
execMock.mockReset()
|
||||
execFileMock.mockReset()
|
||||
const killMock = vi.fn()
|
||||
execFileMock.mockImplementation(() => ({ kill: killMock }) as never)
|
||||
|
||||
const fs = await import('fs')
|
||||
vi.mocked(fs.existsSync).mockReturnValue(true)
|
||||
vi.mocked(fs.readFileSync).mockReturnValue('scripts:\n setup: |\n echo hello\n')
|
||||
|
||||
const originalPlatform = process.platform
|
||||
Object.defineProperty(process, 'platform', {
|
||||
configurable: true,
|
||||
value: 'win32'
|
||||
})
|
||||
|
||||
try {
|
||||
const { runHook } = await import('./hooks')
|
||||
const promise = runHook('setup', '\\\\wsl.localhost\\Ubuntu\\home\\jin\\feature', {
|
||||
...makeRepo(),
|
||||
path: 'C:\\Users\\jinwo\\git\\orca'
|
||||
})
|
||||
let settled = false
|
||||
void promise.finally(() => {
|
||||
settled = true
|
||||
})
|
||||
|
||||
await vi.advanceTimersByTimeAsync(120_000)
|
||||
await Promise.resolve()
|
||||
|
||||
expect(settled).toBe(true)
|
||||
await expect(promise).resolves.toMatchObject({
|
||||
success: false,
|
||||
output: expect.stringContaining('Hook timed out')
|
||||
})
|
||||
expect(killMock).toHaveBeenCalled()
|
||||
} finally {
|
||||
vi.useRealTimers()
|
||||
Object.defineProperty(process, 'platform', {
|
||||
configurable: true,
|
||||
value: originalPlatform
|
||||
})
|
||||
}
|
||||
})
|
||||
})
|
||||
|
||||
describe('shouldRunSetupForCreate', () => {
|
||||
|
||||
+46
-23
@@ -525,30 +525,53 @@ export function runHook(
|
||||
}
|
||||
|
||||
return new Promise((resolve) => {
|
||||
execFile(
|
||||
'wsl.exe',
|
||||
['-d', wslInfo.distro, '--', 'bash', '-c', bashCmd],
|
||||
{
|
||||
timeout: HOOK_TIMEOUT,
|
||||
encoding: 'utf-8',
|
||||
env: { ...process.env, ...wslEnv }
|
||||
},
|
||||
(error, stdout, stderr) => {
|
||||
if (error) {
|
||||
console.error(`[hooks] ${hookName} hook failed in ${cwd}:`, error.message)
|
||||
resolve({
|
||||
success: false,
|
||||
output: `${stdout}\n${stderr}\n${error.message}`.trim()
|
||||
})
|
||||
} else {
|
||||
console.log(`[hooks] ${hookName} hook completed in ${cwd}`)
|
||||
resolve({
|
||||
success: true,
|
||||
output: `${stdout}\n${stderr}`.trim()
|
||||
})
|
||||
}
|
||||
let child: ReturnType<typeof execFile> | null = null
|
||||
let settled = false
|
||||
|
||||
const finish = (error: Error | null, stdout = '', stderr = ''): void => {
|
||||
if (settled) {
|
||||
return
|
||||
}
|
||||
)
|
||||
settled = true
|
||||
clearTimeout(timeout)
|
||||
if (error) {
|
||||
console.error(`[hooks] ${hookName} hook failed in ${cwd}:`, error.message)
|
||||
resolve({
|
||||
success: false,
|
||||
output: `${stdout}\n${stderr}\n${error.message}`.trim()
|
||||
})
|
||||
} else {
|
||||
console.log(`[hooks] ${hookName} hook completed in ${cwd}`)
|
||||
resolve({
|
||||
success: true,
|
||||
output: `${stdout}\n${stderr}`.trim()
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
// Why: Node's execFile timeout only signals wsl.exe; if no callback
|
||||
// arrives, hook setup/archive must still unblock after HOOK_TIMEOUT.
|
||||
const timeout = setTimeout(() => {
|
||||
child?.kill()
|
||||
finish(new Error(`Hook timed out after ${HOOK_TIMEOUT}ms.`))
|
||||
}, HOOK_TIMEOUT)
|
||||
|
||||
try {
|
||||
child = execFile(
|
||||
'wsl.exe',
|
||||
['-d', wslInfo.distro, '--', 'bash', '-c', bashCmd],
|
||||
{
|
||||
timeout: HOOK_TIMEOUT,
|
||||
encoding: 'utf-8',
|
||||
env: { ...process.env, ...wslEnv }
|
||||
},
|
||||
(error, stdout, stderr) => {
|
||||
finish(error ?? null, stdout, stderr)
|
||||
}
|
||||
)
|
||||
} catch (error) {
|
||||
finish(error instanceof Error ? error : new Error(String(error)))
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user