diff --git a/packages/cultmesh-ts/src/index.ts b/packages/cultmesh-ts/src/index.ts index dd9280e7..2e487c77 100644 --- a/packages/cultmesh-ts/src/index.ts +++ b/packages/cultmesh-ts/src/index.ts @@ -4310,6 +4310,12 @@ export interface CultMeshRudpServerSession { export interface CultMeshRudpDocumentServer { readonly bind: { host: string; port: number }; + /** + * Datagrams that could not be sent to a peer. Each is that peer's lost datagram: a + * reliable packet stays pending and is resent, and no other peer is affected. A datagram + * that can never be sent as built is not counted here: it ends that peer's session. + */ + readonly sendFailures: number; start(): Promise; close(): void; } @@ -5782,6 +5788,7 @@ export class CultMesh { }; const sessions = new Map(); let sessionSequence = 0; + let sendFailures = 0; const resendPollMs = Math.max(10, options.resendPollMs ?? 25); const sessionTimeoutMs = Math.max(1_000, options.sessionTimeoutMs ?? 30_000); const wireContract = options.wireContract ?? "cultnet.schema.v0"; @@ -5963,7 +5970,7 @@ export class CultMesh { /** * Sends one packet to a session's peer. A failure is that peer's alone: one that may - * pass (no route, full buffers) is a lost datagram, reported, and resent if it was + * pass (no route, full buffers) is a lost datagram, counted, and resent if it was * reliable; one that can never pass as built ends that peer's session. Neither ends * the server, a timer or another peer's session. */ @@ -5972,7 +5979,7 @@ export class CultMesh { if (isPermanentSendError(error)) { endUnsendableSession(record, error); } else { - reportError(error); + sendFailures += 1; } }); } @@ -6017,6 +6024,9 @@ export class CultMesh { return { bind, + get sendFailures() { + return sendFailures; + }, async start(): Promise { await new Promise((resolve, reject) => { socket.once("error", reject); diff --git a/packages/cultmesh-ts/test/rudp-document-server-send-failure.test.ts b/packages/cultmesh-ts/test/rudp-document-server-send-failure.test.ts index 8fa0f6a4..534a9440 100644 --- a/packages/cultmesh-ts/test/rudp-document-server-send-failure.test.ts +++ b/packages/cultmesh-ts/test/rudp-document-server-send-failure.test.ts @@ -115,7 +115,7 @@ test("a Connect whose Accept can never be sent starts no session, and the server } }); -test("a datagram that may yet pass is reported; one that never can ends only that peer's session", async () => { +test("a datagram that may yet pass is counted; one that never can ends only that peer's session", async () => { const connectionId = 0x10203092; const socket = createSocket("udp4"); // Sends to a failing port report the failure the way Node reports a system one: @@ -142,13 +142,16 @@ test("a datagram that may yet pass is reported; one that never can ends only tha await waitFor(() => served.puts.has("x-1"), "X's first put"); const xPort = served.puts.get("x-1")!; - // No route: X's acknowledgements are lost datagrams, reported, and end nothing. + // No route: X's acknowledgements are lost datagrams, counted, never reported as errors, + // and end nothing. + assert.equal(served.server.sendFailures, 0); failing.set(xPort, "EHOSTUNREACH"); x.send(put("x-2")); - await waitFor(() => served.errors.some((error) => (error as NodeJS.ErrnoException).code === "EHOSTUNREACH"), "the reported loss"); + await waitFor(() => served.server.sendFailures >= 2, "the counted losses"); y.send(put("y-1")); await waitFor(() => served.puts.has("y-1"), "Y's put"); assert.deepEqual(served.closed, [], "a loss that may pass ends no session"); + assert.deepEqual(served.errors.map((error) => error.message), [], "a lost datagram is not an error"); // X's retransmitted put is acknowledged again, and that datagram can never be sent. failing.set(xPort, "EMSGSIZE"); diff --git a/packages/cultnet-ts/src/rudp.ts b/packages/cultnet-ts/src/rudp.ts index d3617471..da431cd9 100644 --- a/packages/cultnet-ts/src/rudp.ts +++ b/packages/cultnet-ts/src/rudp.ts @@ -1049,6 +1049,7 @@ export class CultNetRudpSocketTransportConnection extends EventEmitter implement framesReceived: 0, framesSent: 0, packetsDropped: 0, + sendFailures: 0, }; constructor(options: CultNetRudpSocketTransportOptions) { @@ -1363,14 +1364,15 @@ export class CultNetRudpSocketTransportConnection extends EventEmitter implement /** * A datagram that can never be sent ends the session; any other failure is a lost - * datagram, reported on "error" as the socket reports it. + * datagram, counted and never emitted. Node reports a failed send only to its callback, + * not on the socket's "error" event, and an "error" with no listener ends the process. */ #sendFailed(error: Error): void { if (isPermanentSendError(error)) { this.#endUnsendableSession(error); return; } - this.emit("error", error); + this.#stats.sendFailures += 1; } } diff --git a/packages/cultnet-ts/src/transport.ts b/packages/cultnet-ts/src/transport.ts index f900aa23..02f5e8c1 100644 --- a/packages/cultnet-ts/src/transport.ts +++ b/packages/cultnet-ts/src/transport.ts @@ -16,6 +16,13 @@ export interface CultNetTransportStats { framesSent: number; /** Datagrams read and discarded because they belong to no session on this transport. */ packetsDropped: number; + /** + * Datagrams this transport could not send and treats as lost (no route, full buffers): + * a reliable packet stays pending and is resent. A send a caller made throws its failure + * to the caller instead, and a datagram that can never be sent as built ends the session. + * Zero for stream transports. + */ + sendFailures: number; } export interface CultNetReconnectPolicy { @@ -195,6 +202,7 @@ export class TcpFramedTransportConnection extends EventEmitter implements CultNe framesReceived: 0, framesSent: 0, packetsDropped: 0, + sendFailures: 0, }; constructor(stream: Duplex, profile: CultNetTransportProfile) { diff --git a/packages/cultnet-ts/test/rudp-send-failure.test.ts b/packages/cultnet-ts/test/rudp-send-failure.test.ts index 9abfe3d8..edcf0d08 100644 --- a/packages/cultnet-ts/test/rudp-send-failure.test.ts +++ b/packages/cultnet-ts/test/rudp-send-failure.test.ts @@ -10,6 +10,7 @@ import { CultNetRudpSession, CultNetRudpSocketTransportConnection, encodeRudpPacket, + invokeCultNetOperation, isPermanentSendError, startCultNetOperationServer, type CultNetOperationResponseMessage, @@ -212,8 +213,8 @@ test("a server transport whose resend can never be sent ends the session and sen mode = "lost"; server.send("schema", Buffer.from("one", "utf8")); server.send("schema", Buffer.from("two", "utf8")); - await waitFor(() => errors.length >= 2, "both writes lost"); - assert.deepEqual([...new Set(errors)], ["EHOSTUNREACH"], "a loss that may pass is reported and ends nothing"); + await waitFor(() => server.stats.sendFailures >= 4, "both writes lost, and lost again when resent"); + assert.deepEqual(errors, [], "a loss that may pass is counted, never emitted"); assert.deepEqual(reasons, []); // Both writes are due again together; the first can never be sent. @@ -229,3 +230,64 @@ test("a server transport whose resend can never be sent ends the session and sen server.close(); } }); + +// The kernel refuses a datagram to the limited broadcast address from a socket that has +// not asked for broadcast (EACCES): a real failure that may pass once the socket or the +// route changes, so it is a lost datagram. Node reports it only to the send's callback, +// never on the socket's "error" event, so nothing may turn it into an "error" either. +const UNREACHABLE = { host: "255.255.255.255", port: 9 }; + +test("a client transport with no error listener counts a send the network refuses and does not end the process", async () => { + const socket = await new Promise((resolve) => { + const created = dgram.createSocket("udp4"); + created.bind(0, "0.0.0.0", () => resolve(created)); + }); + const uncaught = watchUncaught(); + const client = new CultNetRudpSocketTransportConnection({ + runtimeId: "rudp-client", + socket, + mode: "client", + remoteHost: UNREACHABLE.host, + remotePort: UNREACHABLE.port, + connectionId: 0x10203095, + resendDelayMs: 10, + resendPollMs: 5, + }); + try { + client.connect(); + // The Connect and its resends, each refused by the kernel. + await waitFor(() => client.stats.sendFailures >= 5, "the refused Connect and its resends"); + assert.equal(client.connected, false); + assert.deepEqual(uncaught.errors, []); + } finally { + uncaught.stop(); + client.close(); + } +}); + +test("an operation call the network refuses rejects with a timeout and does not end the process", async () => { + const uncaught = watchUncaught(); + try { + await assert.rejects( + invokeCultNetOperation( + `rudp://${UNREACHABLE.host}:${UNREACHABLE.port}`, + { + schemaVersion: "cultnet.operation_request.v0", + messageId: "refused", + serviceId: "service", + operation: "operation", + payloadSchema: "payload", + payloadEncoding: "messagepack", + payload: new Uint8Array(), + diagnostics: [], + sourceRuntimeId: "caller", + }, + { runtimeId: "caller", timeoutMs: 400 }, + ), + /CultNet operation connection timed out/, + ); + assert.deepEqual(uncaught.errors, []); + } finally { + uncaught.stop(); + } +});