fix(server): racePeering normalizes handshake rejections and only warns on timeout win

Addresses code review on b22a7bd: (1) a rejected handshake now returns
{ status: 'failed', error } instead of throwing, keeping the structured
contract; (2) the "background handshake" warn only fires when the
timeout arm wins — not when the handshake is itself the race winner by
rejection. Timing tests migrated to vi.useFakeTimers for determinism.
Regression test added for the handshake-wins-by-rejection case.
This commit is contained in:
Jannis Braun
2026-04-21 13:41:44 +02:00
parent b22a7bd0c6
commit 4ddb09edf1
2 changed files with 44 additions and 14 deletions
+17 -9
View File
@@ -227,21 +227,29 @@ export async function racePeering(
): Promise<EnsurePeeredResult | { status: 'timeout' }> {
const handshake = ensurePeeredFn(origin);
// Attach a warn-logged catch so a background arm that rejects AFTER the
// race loses does not trigger unhandledRejection. This runs regardless
// of which arm wins.
handshake.catch(err => {
console.warn('[federation] background handshake after call-relay race:', origin, err);
});
let timeoutHandle: ReturnType<typeof setTimeout> | undefined;
const timeoutPromise = new Promise<{ status: 'timeout' }>(resolve => {
timeoutHandle = setTimeout(() => resolve({ status: 'timeout' }), timeoutMs);
});
let raceResult: EnsurePeeredResult | { status: 'timeout' };
try {
return await Promise.race([handshake, timeoutPromise]);
} finally {
raceResult = await Promise.race([handshake, timeoutPromise]);
} catch (err) {
// ensurePeeredFn rejected as the race winner. Normalize to failed.
if (timeoutHandle) clearTimeout(timeoutHandle);
const message = err instanceof Error ? err.message : 'Unknown handshake error';
return { status: 'failed', error: message };
}
if (timeoutHandle) clearTimeout(timeoutHandle);
// Only when the timeout arm won is the background handshake still running.
// Guard its eventual rejection so we don't emit unhandledRejection.
if (raceResult.status === 'timeout') {
handshake.catch(err => {
console.warn('[federation] background handshake after call-relay race:', origin, err);
});
}
return raceResult;
}