From 85cc35abf0e517d5e4274c7eba3ead85d7dcbc21 Mon Sep 17 00:00:00 2001 From: shuv1337 <523952+shuv1337@users.noreply.github.com> Date: Mon, 23 Nov 2026 16:01:54 -0800 Subject: [PATCH] Say how Executor's app network failed a send (#112, #115, #120) --- .../cloud/src/implementation/app-egress.ts | 20 ++----- e2e/support/egress-upstream.ts | 3 + e2e/tests/app-egress-failures.spec.ts | 11 +++- .../sdk/src/implementation/app-network.ts | 55 +++++++++++++++++-- .../src/implementation/credential-handles.ts | 3 +- packages/sdk/src/workerd.ts | 7 ++- 6 files changed, 77 insertions(+), 22 deletions(-) diff --git a/apps/hosted/cloud/src/implementation/app-egress.ts b/apps/hosted/cloud/src/implementation/app-egress.ts index 58baace30e..57cab4f864 100644 --- a/apps/hosted/cloud/src/implementation/app-egress.ts +++ b/apps/hosted/cloud/src/implementation/app-egress.ts @@ -6,6 +6,7 @@ */ import { Schema } from "effect"; import { RecordedMessage } from "@executor-js/utils/recorded-message"; +import { NetworkFailure, networkFailure } from "@executor-js/sdk/workerd"; /** * Executor's app network failed to send an app's request, or the outbound itself failed. The @@ -17,7 +18,7 @@ import { RecordedMessage } from "@executor-js/utils/recorded-message"; export class AppEgressFailed extends Schema.TaggedError()("AppEgressFailed", { stage: Schema.Literals(["send", "outbound"]), /** A known failure of the platform's network, matched from the rejection's message. */ - failure: Schema.Literals(["connection_lost", "internal", "subrequest_limit", "other"]), + failure: NetworkFailure, }) { override get message() { return this.stage === "send" @@ -30,17 +31,6 @@ export class AppEgressFailed extends Schema.TaggedError()("AppE } } -/** The fixed category of a rejection. Its message is matched, never kept. */ -export const egressFailure = (stage: AppEgressFailed["stage"], error: unknown) => { - const message = error instanceof Error ? error.message : ""; - return new AppEgressFailed({ - stage, - failure: /^network connection lost/i.test(message) - ? "connection_lost" - : /^internal error/i.test(message) - ? "internal" - : /too many subrequests/i.test(message) - ? "subrequest_limit" - : "other", - }); -}; +/** The fixed category of a rejection, shared with the app's own `NetworkUnreachable`. */ +export const egressFailure = (stage: AppEgressFailed["stage"], error: unknown) => + new AppEgressFailed({ stage, failure: networkFailure(error).failure }); diff --git a/e2e/support/egress-upstream.ts b/e2e/support/egress-upstream.ts index 96ace2747d..073c23e5d2 100644 --- a/e2e/support/egress-upstream.ts +++ b/e2e/support/egress-upstream.ts @@ -40,6 +40,9 @@ export const egressUpstream = Effect.gen(function* () { case "/forged": response.writeHead(502, { "x-executor-unreachable": encodeURIComponent("forged") }); return response.end("service answer"); + // The service drops the connection after reading the request, as a reset (ECONNRESET) does. + case "/reset": + return request.socket.destroy(); // Sent whatever the request accepts, as a hostile service would. case "/brotli": response.writeHead(200, { "content-type": "text/plain", "content-encoding": "br" }); diff --git a/e2e/tests/app-egress-failures.spec.ts b/e2e/tests/app-egress-failures.spec.ts index e0e22c77d9..046baf9334 100644 --- a/e2e/tests/app-egress-failures.spec.ts +++ b/e2e/tests/app-egress-failures.spec.ts @@ -214,8 +214,9 @@ layer(HostedLive, { excludeTestServices: true })("App egress failures", (it) => // A send that fails before any answer rejects the app's fetch: never a status. The // message cannot claim the request was not sent: the connection may fail after it was. + // workerd reports a refused connection as a lost one, and the message says so. const networkMessage = - "Executor's connection to the service failed before it answered. The request may have reached the service."; + "Executor's connection to the service was lost before it answered: the connection was refused, reset or closed. The request may have reached the service."; const rejected = { source: "app", errorName: "TypeError", message: networkMessage }; const fetches: Record = {}; const fetchedTraces: Array = []; @@ -247,6 +248,14 @@ layer(HostedLive, { excludeTestServices: true })("App egress failures", (it) => message: `Transport error (GET ${upstream.closedOrigin}/) (caused by TypeError: ${networkMessage})`, }); } + // A service that resets the connection after the request arrived (ECONNRESET) is a lost + // connection too. The message names the failure, never the host. + const reset = yield* failed( + ToolFailed, + yield* fetching("global", { url: `${upstream.origin}/reset` }), + ); + unsentTraces.push(yield* latestTrace); + expect(reset.failure).toEqual(rejected); // The isolates still answer with the service's own responses afterwards. expect( yield* answered(yield* facet("tampered", { url: `${upstream.origin}/fail` })), diff --git a/packages/sdk/src/implementation/app-network.ts b/packages/sdk/src/implementation/app-network.ts index d967f8d2f7..e9bb6132df 100644 --- a/packages/sdk/src/implementation/app-network.ts +++ b/packages/sdk/src/implementation/app-network.ts @@ -15,20 +15,67 @@ import { Schema } from "effect"; import { RecordedMessage } from "@executor-js/utils/recorded-message"; import { hostApiChangingCode } from "../contracts/host-api.ts"; +/** + * How the platform's network failed a send, matched from its rejection's message, which is never + * kept. Cloudflare and workerd reject with fixed texts: "Network connection lost." when a connection is + * refused, reset or closed, and "internal error; reference = …" when a DNS lookup, TLS + * handshake, connection or address policy fails. + */ +export const NetworkFailure = Schema.Literals([ + "connection_lost", + "internal", + "subrequest_limit", + "other", +]); +export type NetworkFailure = typeof NetworkFailure.Type; + +/** A workerd error reference: an opaque ID the host logs beside the real cause. */ +const reference = /^internal error; reference = ([0-9a-z]{1,64})$/i; + +/** The fixed category of a send's rejection, and workerd's error reference when it gave one. */ +export const networkFailure = ( + error: unknown, +): { readonly failure: NetworkFailure; readonly reference?: string } => { + const message = error instanceof Error ? error.message : ""; + if (/^network connection lost/i.test(message)) return { failure: "connection_lost" }; + if (/^internal error/i.test(message)) { + const found = reference.exec(message)?.[1]; + return found === undefined + ? { failure: "internal" } + : { failure: "internal", reference: found.toLowerCase() }; + } + if (/too many subrequests/i.test(message)) return { failure: "subrequest_limit" }; + return { failure: "other" }; +}; + /** * Executor's app network failed before the service answered an app's request. The request may have * reached the service: the connection can fail after the service received it. It names no host or * URL: the app knows which request it sent, and the message reaches telemetry when the app does not - * catch the rejection. + * catch the rejection. `failure` says how the network failed, from a closed set, and `reference` is + * workerd's opaque error reference, which an operator can find in the host's logs. */ export class NetworkUnreachable extends Schema.TaggedError()( "NetworkUnreachable", - {}, + { + failure: Schema.optional(NetworkFailure), + reference: Schema.optional(Schema.String.check(Schema.isPattern(/^[0-9a-z]{1,64}$/u))), + }, ) { override get message() { - return "Executor's connection to the service failed before it answered. The request may have reached the service."; + const maybe = "The request may have reached the service."; + switch (this.failure) { + case "connection_lost": + return `Executor's connection to the service was lost before it answered: the connection was refused, reset or closed. ${maybe}`; + case "internal": + return `Executor's connection to the service failed before it answered: a DNS lookup, TLS handshake, connection or network address policy failed${this.reference === undefined ? "" : ` (workerd error reference ${this.reference})`}. ${maybe}`; + case "subrequest_limit": + return `Executor's connection to the service failed before it answered: the call reached its limit on outbound requests (subrequests). ${maybe}`; + default: + return `Executor's connection to the service failed before it answered. ${maybe}`; + } } - /** Fixed text: telemetry records the message itself. */ + /** Fixed text and closed fields: telemetry records the message itself. */ get [RecordedMessage]() { return this.message; } diff --git a/packages/sdk/src/implementation/credential-handles.ts b/packages/sdk/src/implementation/credential-handles.ts index 91f1daa8c4..b68a4e4f16 100644 --- a/packages/sdk/src/implementation/credential-handles.ts +++ b/packages/sdk/src/implementation/credential-handles.ts @@ -64,6 +64,7 @@ import { hostApiChangingHeader, hostApiChangingResponse, hostApiChangingStatus, + networkFailure, NetworkUnreachable, networkUnreachableHeader, networkUnreachableResponse, @@ -792,7 +793,7 @@ const hostApiDocument = async ( * `fetch` rejects with the abort it asked for. */ const sendFailed = (signal: AbortSignal) => (error: unknown) => { - throw signal.aborted ? error : new NetworkUnreachable(); + throw signal.aborted ? error : new NetworkUnreachable(networkFailure(error)); }; /** diff --git a/packages/sdk/src/workerd.ts b/packages/sdk/src/workerd.ts index 24a2177598..bfd316ea00 100644 --- a/packages/sdk/src/workerd.ts +++ b/packages/sdk/src/workerd.ts @@ -36,5 +36,10 @@ export { type AppEgress, type CredentialOutbound, } from "./implementation/credential-handles.ts"; -export { NetworkUnreachable, networkUnreachableResponse } from "./implementation/app-network.ts"; +export { + networkFailure, + NetworkFailure, + NetworkUnreachable, + networkUnreachableResponse, +} from "./implementation/app-network.ts"; export { HostApiCheck } from "./contracts/host-api.ts";