fix(ssh): fix flaky test sync and add error code safety check

Test was using tick-based Promise.resolve() loops which don't guarantee
the async operation has started. Replace with signal-based synchronization
that waits for the actual lease flush. Also add nullish-coalescing to
error code check to prevent crashes if error is null or undefined.
This commit is contained in:
Jinjing
2026-08-03 22:59:51 -07:00
parent f72dc6b008
commit 2c911c3695
2 changed files with 13 additions and 6 deletions
+11 -5
View File
@@ -618,13 +618,19 @@ describe('SSH IPC handlers', () => {
const detachFlush = new Promise<void>((resolve) => {
releaseDetach = resolve
})
mockStore.markSshRemotePtyLeasesAsync.mockImplementationOnce(() => detachFlush)
let signalEnteredDetach = (): void => {}
const enteredDetach = new Promise<void>((resolve) => {
signalEnteredDetach = resolve
})
mockStore.markSshRemotePtyLeasesAsync.mockImplementationOnce(() => {
signalEnteredDetach()
return detachFlush
})
const replacement = handlers.get('ssh:connect')!(null, { targetId: 'ssh-1' })
// Why: let the replacement connect reach the flush before shutdown snapshots what to drain.
for (let tick = 0; tick < 10; tick++) {
await Promise.resolve()
}
// Why await the flush entry and not ticks: shutdown must not snapshot what to drain until the
// replacement connect is parked in the lease flush.
await enteredDetach
expect(mockStore.markSshRemotePtyLeasesAsync).toHaveBeenCalledWith('ssh-1', 'detached')
mockConnectionManager.disconnectAll.mockClear()
+2 -1
View File
@@ -46,7 +46,8 @@ export async function retrySshOwnerRecoveryWhilePublicationPending<T>(
return await attempt()
} catch (error) {
if (
(error as { code?: unknown }).code !== PTY_CONSUMER_OWNER_RECOVERY_PENDING_ERROR ||
(error as { code?: unknown } | null | undefined)?.code !==
PTY_CONSUMER_OWNER_RECOVERY_PENDING_ERROR ||
!gate.isCurrent()
) {
throw error