Repository navigation
test(orcad): wait for killed terminal daemons to exit before removing their temp profiles #24871
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
Merged
OrcaWin
merged 1 commit into
OrcaWin/node-rt-phase3
from
OrcaWin/node-rt-p3-orcad-test-cleanup
Oct 2, 2026
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,92 @@ | ||
| /** | ||
| * Teardown for integration tests whose orcad started a real terminal daemon in a temp profile. | ||
| * | ||
| * The daemon outlives orcad by design and only its pid record names it. Killing it is not | ||
| * enough: until it has exited it can still remove its socket, pid and token files and append | ||
| * to its log, so a recursive delete racing it fails with ENOTEMPTY (seen on macOS). | ||
| */ | ||
| import { existsSync, readdirSync } from 'node:fs' | ||
| import { join } from 'node:path' | ||
| import { removeTreeSync } from '../../shared/windows-transient-lock-removal' | ||
| import { readDaemonPidRecord } from '../daemon/daemon-endpoint-incarnation' | ||
| import type { spawnProcess } from '../../shared/child-process/run-process' | ||
|
|
||
| const EXIT_WAIT_MS = 10_000 | ||
| const POLL_MS = 50 | ||
| const REMOVE_ATTEMPTS = 8 | ||
| const REMOVE_RETRY_MS = 150 | ||
|
|
||
| const sleep = (ms: number): Promise<void> => new Promise((settle) => setTimeout(settle, ms)) | ||
|
|
||
| function errorCode(error: unknown): string | undefined { | ||
| return typeof error === 'object' && | ||
| error !== null && | ||
| 'code' in error && | ||
| typeof error.code === 'string' | ||
| ? error.code | ||
| : undefined | ||
| } | ||
|
|
||
| function hasExited(pid: number): boolean { | ||
| try { | ||
| process.kill(pid, 0) | ||
| return false | ||
| } catch (error) { | ||
| // EPERM still proves the process exists. | ||
| return errorCode(error) === 'ESRCH' | ||
| } | ||
| } | ||
|
|
||
| /** SIGKILLs each pid and waits, bounded, until the OS no longer knows it. */ | ||
| export async function killAndAwaitExit(pids: Iterable<number>): Promise<void> { | ||
| const pending = [...pids] | ||
| for (const pid of pending) { | ||
| try { | ||
| process.kill(pid, 'SIGKILL') | ||
| } catch {} | ||
| } | ||
| const deadline = Date.now() + EXIT_WAIT_MS | ||
| while (pending.some((pid) => !hasExited(pid)) && Date.now() < deadline) { | ||
| await sleep(POLL_MS) | ||
| } | ||
| } | ||
|
|
||
| /** Kills a spawned child and waits for its exit event, so nothing it spawned races teardown. */ | ||
| export async function killChildAndWait(child: ReturnType<typeof spawnProcess>): Promise<void> { | ||
| if (child.exitCode !== null || child.signalCode !== null) { | ||
| return | ||
| } | ||
| const exited = new Promise((settle) => child.once('exit', settle)) | ||
| child.kill('SIGKILL') | ||
| await exited | ||
| } | ||
|
|
||
| /** Kills every terminal daemon `<userData>/daemon` names and waits until each has exited. */ | ||
| export async function killProfileDaemons(userData: string): Promise<void> { | ||
| const daemonDir = join(userData, 'daemon') | ||
| const pids = (existsSync(daemonDir) ? readdirSync(daemonDir) : []).flatMap((name) => { | ||
| const pid = /^daemon-v\d+\.pid$/u.test(name) | ||
| ? readDaemonPidRecord(join(daemonDir, name))?.pid | ||
| : undefined | ||
| return pid ? [pid] : [] | ||
| }) | ||
| await killAndAwaitExit(pids) | ||
| } | ||
|
|
||
| /** Removes a test root, retrying a late writer's ENOTEMPTY/EBUSY on every platform. */ | ||
| export async function removeTestRoot(root: string): Promise<void> { | ||
| for (let attempt = 1; ; attempt += 1) { | ||
| try { | ||
| removeTreeSync(root) | ||
| return | ||
| } catch (error) { | ||
| if ( | ||
| attempt >= REMOVE_ATTEMPTS || | ||
| !['ENOTEMPTY', 'EBUSY', 'EPERM'].includes(errorCode(error) ?? '') | ||
| ) { | ||
| throw error | ||
| } | ||
| await sleep(REMOVE_RETRY_MS) | ||
| } | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
REMOVE_ATTEMPTS = 8/REMOVE_RETRY_MS = 150restate the retry policy already owned bysrc/shared/windows-transient-lock-removal.ts(WINDOWS_RM_MAX_RETRIES/WINDOWS_RM_RETRY_DELAY_MS, same values). That module's ratchet test exists precisely to stop a second copy of this policy drifting — and because it only matches the exactWINDOWS_RM_*names, this copy slips past it. Reuse the exported constants (or give them platform-neutral aliases) instead of restating the numbers.Technical details