fix(services): let slow handshakes reach the TREK services

Node gives each address of a dual-stack name 250 ms before it moves on.
From Australia that is shorter than one round trip to Europe, so on a
network without IPv6 the IPv4 handshake to places.liketrek.com was cut
off, the IPv6 attempt failed at once and every request ended in
ETIMEDOUT. The installation never got its services key, sharing place
details stayed unavailable and searches fell back to OpenStreetMap.

Raise the per-address attempt timeout to 2 s at startup, keeping a
larger value an operator already set, and log the reason behind
undici's bare "fetch failed" for each address tried.
This commit is contained in:
Maurice
2026-11-02 12:12:48 +01:00
parent 327ebc354f
commit 8659262e59
6 changed files with 138 additions and 2 deletions
@@ -1,4 +1,5 @@
import {
describeFailure,
isAllowlisted,
keyFingerprint,
readQuotaHeaders,
@@ -103,3 +104,51 @@ describe('keyFingerprint', () => {
expect(keyFingerprint(token)).not.toBe(keyFingerprint(`${token}x`));
});
});
describe('describeFailure', () => {
const attempt = (code: string, address: string, port = 443) =>
Object.assign(new Error(`connect ${code} ${address}:${port}`), { code, address, port });
it('spells out every address undici tried behind "fetch failed"', () => {
const cause = new AggregateError(
[attempt('ETIMEDOUT', '185.255.115.203'), attempt('ENETUNREACH', '2a0d:5940:140:bf4::2')],
'',
);
const err = new TypeError('fetch failed', { cause });
expect(describeFailure(err)).toBe(
'fetch failed (ETIMEDOUT 185.255.115.203:443, ENETUNREACH [2a0d:5940:140:bf4::2]:443)',
);
});
it('names a single cause by its code', () => {
const cause = Object.assign(new Error('getaddrinfo ENOTFOUND places.liketrek.com'), { code: 'ENOTFOUND' });
expect(describeFailure(new TypeError('fetch failed', { cause }))).toBe('fetch failed (ENOTFOUND)');
});
it('falls back to the cause message without a code', () => {
const cause = new Error('unable to verify the first certificate');
expect(describeFailure(new TypeError('fetch failed', { cause }))).toBe(
'fetch failed (unable to verify the first certificate)',
);
});
it('handles a cause list without usable entries and an address without a port', () => {
const portless = Object.assign(new Error('connect ECONNREFUSED'), { code: 'ECONNREFUSED', address: '192.0.2.1' });
expect(describeFailure(new TypeError('fetch failed', { cause: new AggregateError([], '') }))).toBe('fetch failed');
expect(describeFailure(new TypeError('fetch failed', { cause: new AggregateError(['odd'], '') }))).toBe(
'fetch failed (odd)',
);
expect(describeFailure(new TypeError('fetch failed', { cause: portless }))).toBe(
'fetch failed (ECONNREFUSED 192.0.2.1)',
);
});
it('keeps a plain error or value as it is', () => {
expect(describeFailure(new Error('the issuer answered 503'))).toBe('the issuer answered 503');
expect(describeFailure('boom')).toBe('boom');
});
});
@@ -0,0 +1,30 @@
import { CONNECT_ATTEMPT_TIMEOUT_MS, widenConnectAttemptTimeout } from '../../../src/utils/connectAttempt';
import net from 'node:net';
import { afterEach, beforeEach, describe, expect, it } from 'vitest';
describe('widenConnectAttemptTimeout', () => {
let original: number;
beforeEach(() => {
original = net.getDefaultAutoSelectFamilyAttemptTimeout();
});
afterEach(() => {
net.setDefaultAutoSelectFamilyAttemptTimeout(original);
});
it('raises Node’s 250 ms default so a slow but working handshake is not cut off', () => {
net.setDefaultAutoSelectFamilyAttemptTimeout(250);
expect(widenConnectAttemptTimeout()).toBe(CONNECT_ATTEMPT_TIMEOUT_MS);
expect(net.getDefaultAutoSelectFamilyAttemptTimeout()).toBe(CONNECT_ATTEMPT_TIMEOUT_MS);
});
it('keeps a larger value an operator already set', () => {
net.setDefaultAutoSelectFamilyAttemptTimeout(5_000);
expect(widenConnectAttemptTimeout()).toBe(5_000);
expect(net.getDefaultAutoSelectFamilyAttemptTimeout()).toBe(5_000);
});
});