Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 5 additions & 15 deletions apps/hosted/cloud/src/implementation/app-egress.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -17,7 +18,7 @@ import { RecordedMessage } from "@executor-js/utils/recorded-message";
export class AppEgressFailed extends Schema.TaggedError<AppEgressFailed>()("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"
Expand All @@ -30,17 +31,6 @@ export class AppEgressFailed extends Schema.TaggedError<AppEgressFailed>()("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 });
3 changes: 3 additions & 0 deletions e2e/support/egress-upstream.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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" });
Expand Down
11 changes: 10 additions & 1 deletion e2e/tests/app-egress-failures.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<string, unknown> = {};
const fetchedTraces: Array<string> = [];
Expand Down Expand Up @@ -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` })),
Expand Down
55 changes: 51 additions & 4 deletions packages/sdk/src/implementation/app-network.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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>()(
"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;
}
Expand Down
3 changes: 2 additions & 1 deletion packages/sdk/src/implementation/credential-handles.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ import {
hostApiChangingHeader,
hostApiChangingResponse,
hostApiChangingStatus,
networkFailure,
NetworkUnreachable,
networkUnreachableHeader,
networkUnreachableResponse,
Expand Down Expand Up @@ -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));
};

/**
Expand Down
7 changes: 6 additions & 1 deletion packages/sdk/src/workerd.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Loading