mirror of
https://github.com/wu736139669/hapi.git
synced 2026-11-24 04:47:03 +00:00
fix(web,cli): clarify archive cleanup progress and bound shutdown waits (#2061)
* fix(web,cli): clarify archive cleanup progress and bound shutdown waits * fix(web): keep archive dismissal and delete waiting states consistent * fix(web): scope archive subscriptions to active consumers
This commit is contained in:
@@ -226,6 +226,50 @@ describe('getHapiRunnerProcessIdentity on Windows', () => {
|
||||
})
|
||||
|
||||
describe('killProcess on Windows (orphanReap / stopSession)', () => {
|
||||
it('includes slow liveness probes in the graceful shutdown deadline', async () => {
|
||||
let graceStarted: number | undefined
|
||||
let alive = true
|
||||
let forcedAt: number | undefined
|
||||
spawnSyncMock.mockImplementation((command: string, args: string[] = []) => {
|
||||
if (command === 'tasklist') {
|
||||
// A synchronous OS query consumes real time even though it is
|
||||
// not part of the timer used to yield between observations.
|
||||
vi.setSystemTime(Date.now() + 400)
|
||||
return completed(alive ? 'hapi.exe 7788 Console' : 'INFO: No tasks')
|
||||
}
|
||||
if (command === 'taskkill' && args.includes('/F')) {
|
||||
forcedAt = Date.now()
|
||||
alive = false
|
||||
} else if (command === 'taskkill') {
|
||||
graceStarted = Date.now()
|
||||
}
|
||||
return completed('')
|
||||
})
|
||||
const stopped = killProcess(7788)
|
||||
await vi.runAllTimersAsync()
|
||||
await expect(stopped).resolves.toBe(true)
|
||||
expect(forcedAt).toBeDefined()
|
||||
expect(graceStarted).toBeDefined()
|
||||
// The two-second wait plus the final observation and taskkill's own
|
||||
// liveness recheck, each taking 400ms in this scenario.
|
||||
expect(forcedAt! - graceStarted!).toBeLessThanOrEqual(2_800)
|
||||
})
|
||||
|
||||
it('bounds slow probes after forced shutdown without treating a live process as stopped', async () => {
|
||||
const started = Date.now()
|
||||
spawnSyncMock.mockImplementation((command: string) => {
|
||||
if (command === 'tasklist') {
|
||||
vi.setSystemTime(Date.now() + 400)
|
||||
return completed('hapi.exe 7788 Console')
|
||||
}
|
||||
return completed('')
|
||||
})
|
||||
const stopped = killProcess(7788, true)
|
||||
await vi.runAllTimersAsync()
|
||||
await expect(stopped).resolves.toBe(false)
|
||||
expect(Date.now() - started).toBeLessThanOrEqual(3_200)
|
||||
})
|
||||
|
||||
beforeAll(() => {
|
||||
if (!originalPlatformDescriptor?.configurable) {
|
||||
throw new Error('process.platform is not configurable in this runtime')
|
||||
|
||||
@@ -573,11 +573,15 @@ async function waitForProcessToDie(pid: number, force: boolean): Promise<void> {
|
||||
// Windows isProcessAlive shells out to tasklist; keep the poll coarse so a
|
||||
// stop cannot burn the runner event loop for seconds (#1911 Overseer).
|
||||
const pollInterval = isWindows() ? 100 : 20;
|
||||
let waited = 0;
|
||||
let deadline = Date.now() + maxWait;
|
||||
|
||||
while (isProcessAlive(pid) && waited < maxWait) {
|
||||
await new Promise(r => setTimeout(r, pollInterval));
|
||||
waited += pollInterval;
|
||||
// OS probes are synchronous on Windows. Count their time against the
|
||||
// grace period too, otherwise twenty slow tasklist calls can turn two
|
||||
// seconds of graceful shutdown into a much longer archive request.
|
||||
while (Date.now() < deadline && isProcessAlive(pid)) {
|
||||
const remaining = deadline - Date.now();
|
||||
if (remaining <= 0) break;
|
||||
await new Promise(r => setTimeout(r, Math.min(pollInterval, remaining)));
|
||||
}
|
||||
|
||||
// Graceful kill didn't finish — escalate (same structure on both platforms).
|
||||
@@ -591,10 +595,11 @@ async function waitForProcessToDie(pid: number, force: boolean): Promise<void> {
|
||||
} catch {
|
||||
return;
|
||||
}
|
||||
waited = 0;
|
||||
while (isProcessAlive(pid) && waited < 1000) {
|
||||
await new Promise(r => setTimeout(r, pollInterval));
|
||||
waited += pollInterval;
|
||||
deadline = Date.now() + 1000;
|
||||
while (Date.now() < deadline && isProcessAlive(pid)) {
|
||||
const remaining = deadline - Date.now();
|
||||
if (remaining <= 0) break;
|
||||
await new Promise(r => setTimeout(r, Math.min(pollInterval, remaining)));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user