Skip to content

Commit d524425

Browse files
fix(server): only a not-found lookup gets the cached-answer message
EAI_AGAIN means the local resolver did not answer, so a public record says nothing about a stale cache there. Only ENOTFOUND, EAI_NONAME and EBUSY are checked against public DNS; other getaddrinfo failures keep the DNS/VPN advice. AI-assisted; verified with the tests and tools listed above.
1 parent 62b6b6b commit d524425

2 files changed

Lines changed: 28 additions & 23 deletions

File tree

‎apps/server/src/cloud/ConnectReachability.test.ts‎

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -415,15 +415,17 @@ describe("ConnectReachability DNS failures", () => {
415415
it.effect("says whether the name exists in public DNS", () =>
416416
Effect.gen(function* () {
417417
using _connect = mockEdgeConnect();
418-
for (const [dnsStatus, reason] of [
419-
[0, "old cached answer"],
420-
[3, "missing from public DNS"],
418+
for (const [lookupCode, dnsStatus, reason] of [
419+
["ENOTFOUND", 0, "old cached answer"],
420+
["ENOTFOUND", 3, "missing from public DNS"],
421+
// The local resolver did not answer, so nothing it cached is to blame.
422+
["EAI_AGAIN", 0, "does not resolve from this machine"],
421423
] as const) {
422424
const layer = reachabilityLayer(
423425
(request) =>
424426
request.url.startsWith("https://1.1.1.1/dns-query")
425427
? Effect.succeed({ status: 200, body: JSON.stringify({ Status: dnsStatus }) })
426-
: lookupFailed("ENOTFOUND"),
428+
: lookupFailed(lookupCode),
427429
() => Effect.succeed(false),
428430
);
429431
const result = yield* ConnectReachability.ConnectReachability.use(

‎apps/server/src/cloud/ConnectReachability.ts‎

Lines changed: 22 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -203,22 +203,23 @@ export function formatReachabilityFailure(result: Unreachable): string {
203203
].join("\n");
204204
}
205205

206-
/** Any failed name lookup. getaddrinfo reports more than the DNS codes, such as EBUSY. */
207-
function isDnsLookupError(error: unknown): boolean {
206+
/** The code of a failed name lookup, or null. getaddrinfo reports more than the DNS codes, such as EBUSY. */
207+
function dnsLookupErrorCode(error: unknown): string | null {
208208
let current = error;
209209
for (let depth = 0; depth < 6 && typeof current === "object" && current !== null; depth++) {
210-
if (
211-
("syscall" in current && current.syscall === "getaddrinfo") ||
212-
("code" in current && typeof current.code === "string" && DNS_ERROR_CODES.has(current.code))
213-
) {
214-
return true;
210+
const code = "code" in current && typeof current.code === "string" ? current.code : "";
211+
if (("syscall" in current && current.syscall === "getaddrinfo") || DNS_ERROR_CODES.has(code)) {
212+
return code;
215213
}
216214
current = "cause" in current ? current.cause : undefined;
217215
}
218-
return false;
216+
return null;
219217
}
220218

221219
const DNS_ERROR_CODES = new Set(["ENOTFOUND", "EAI_AGAIN", "EAI_NONAME"]);
220+
// Answers that the name does not exist, which a resolver may have cached. EAI_AGAIN means
221+
// the resolver did not answer. glibc behind systemd-resolved can report NXDOMAIN as EBUSY.
222+
const NOT_FOUND_CODES = new Set(["ENOTFOUND", "EAI_NONAME", "EBUSY"]);
222223

223224
const DnsJsonAnswer = Schema.Struct({ Status: Schema.Number });
224225

@@ -316,17 +317,19 @@ const make = Effect.gen(function* () {
316317
Effect.scoped,
317318
(effect, hostname) =>
318319
effect.pipe(
319-
Effect.catch((error) =>
320-
isDnsLookupError(error)
321-
? existsInPublicDns(hostname).pipe(
322-
Effect.map((existsPublicly): EndpointProbe =>
323-
existsPublicly === null
324-
? { kind: "dns_failed" }
325-
: { kind: "dns_failed", existsPublicly },
326-
),
327-
)
328-
: Effect.succeed<EndpointProbe>({ kind: "network_error" }),
329-
),
320+
Effect.catch((error) => {
321+
const code = dnsLookupErrorCode(error);
322+
if (code === null) return Effect.succeed<EndpointProbe>({ kind: "network_error" });
323+
if (!NOT_FOUND_CODES.has(code))
324+
return Effect.succeed<EndpointProbe>({ kind: "dns_failed" });
325+
return existsInPublicDns(hostname).pipe(
326+
Effect.map((existsPublicly): EndpointProbe =>
327+
existsPublicly === null
328+
? { kind: "dns_failed" }
329+
: { kind: "dns_failed", existsPublicly },
330+
),
331+
);
332+
}),
330333
),
331334
);
332335

0 commit comments

Comments
 (0)