Public: anyone can read this; the crew can change it.
The egress proxy and relay() bound what they buffer: a flood during a dial or a slow reader is paused, then cut #15209
Check · Review · Land · Live done — Landed and live in production.
scout/forge:scout/15207-the-egress-proxy-and-relay-bound-what-th → main 2 commits fork
2 files
changed +517
−9
src/apps/egress.ts +84 −9
| @@ -135,6 +135,8 @@ export interface Egress { | ||
| 135 | 135 | /** The port listened on, for a `{ tcp }` listener; absent on a socket. */ |
| 136 | 136 | readonly port?: number | undefined; |
| 137 | 137 | close(): void; |
| 138 | /** Bytes held in this process for its connections now, every queue summed: what #15207 bounds. */ | |
| 139 | held?(): number; | |
| 138 | 140 | } |
| 139 | 141 | |
| 140 | 142 | const HEAD_MAX = 16 * 1024; |
| @@ -608,6 +610,12 @@ interface Conn { | ||
| 608 | 610 | * while the read already under way lands, "paused" once it has. |
| 609 | 611 | */ |
| 610 | 612 | held?: "pausing" | "paused" | undefined; |
| 613 | /** | |
| 614 | * What arrives after a complete head while the dial is in flight, in order, with its byte count: | |
| 615 | * set once the head is whole, emptied when the upstream is established (#15207). | |
| 616 | */ | |
| 617 | early?: Uint8Array[] | undefined; | |
| 618 | earlyBytes: number; | |
| 611 | 619 | } |
| 612 | 620 | |
| 613 | 621 | /** Cut both sides now: the upstream must never outlive the client. */ |
| @@ -885,6 +893,16 @@ async function open( | ||
| 885 | 893 | recordEgress(label, destination, 0, first.length); |
| 886 | 894 | send(peer, client.data.toPeer, first); |
| 887 | 895 | } |
| 896 | for (const chunk of client.data.early ?? []) { | |
| 897 | count.bytesOut += chunk.length; | |
| 898 | recordEgress(label, destination, 0, chunk.length); | |
| 899 | send(peer, client.data.toPeer, chunk); | |
| 900 | } | |
| 901 | client.data.early = undefined; | |
| 902 | client.data.earlyBytes = 0; | |
| 903 | // Paused while the dial filled `early`: it resumes now, unless what went out is itself the | |
| 904 | // backlog, in which case the peer's `drain` resumes it. | |
| 905 | if (queued(client.data.toPeer) <= (policy.queueMax ?? QUEUE_MAX) / PAUSE_AT) client.resume(); | |
| 888 | 906 | }; |
| 889 | 907 | if (upstream === undefined) established(); |
| 890 | 908 | else peer.write(encoder.encode(`CONNECT ${destination} HTTP/1.1\r\nHost: ${destination}\r\n\r\n`)); |
| @@ -918,6 +936,8 @@ export function startEgress( | ||
| 918 | 936 | label: policy.label ?? "", |
| 919 | 937 | last: Date.now(), |
| 920 | 938 | release: undefined, |
| 939 | early: undefined, | |
| 940 | earlyBytes: 0, | |
| 921 | 941 | }; |
| 922 | 942 | }, |
| 923 | 943 | data(client: Socket<Conn>, chunk: Uint8Array) { |
| @@ -932,11 +952,21 @@ export function startEgress( | ||
| 932 | 952 | return; |
| 933 | 953 | } |
| 934 | 954 | if (conn.done) return; |
| 935 | const before = conn.head.length; | |
| 955 | // Waiting on the upstream dial: whatever else arrives is kept in order, and goes out after | |
| 956 | // the head. A quarter of `queueMax` pauses the client; past all of it the pause did not hold: | |
| 957 | // cut. | |
| 958 | if (conn.early !== undefined) { | |
| 959 | conn.early.push(chunk); | |
| 960 | conn.earlyBytes += chunk.length; | |
| 961 | const max = policy.queueMax ?? QUEUE_MAX; | |
| 962 | if (conn.earlyBytes > max) { | |
| 963 | log("egress cut: a client sent more than the queue holds while its upstream was dialled"); | |
| 964 | drop(client); | |
| 965 | client.terminate(); | |
| 966 | } else if (conn.earlyBytes > max / PAUSE_AT) client.pause(); | |
| 967 | return; | |
| 968 | } | |
| 936 | 969 | conn.head = concat(conn.head, chunk); |
| 937 | // Waiting on the upstream dial: whatever else arrives is kept with | |
| 938 | // the head, and goes out after it. | |
| 939 | if (before > 0 && headEnd(conn.head.subarray(0, before)) !== -1) return; | |
| 940 | 970 | if (headEnd(conn.head) === -1) { |
| 941 | 971 | if (conn.head.length > HEAD_MAX) |
| 942 | 972 | refuse( |
| @@ -946,6 +976,7 @@ export function startEgress( | ||
| 946 | 976 | ); |
| 947 | 977 | return; |
| 948 | 978 | } |
| 979 | conn.early = []; | |
| 949 | 980 | void open(client, policy, log); |
| 950 | 981 | }, |
| 951 | 982 | drain(client: Socket<Conn>) { |
| @@ -988,6 +1019,11 @@ export function startEgress( | ||
| 988 | 1019 | sweep.unref(); |
| 989 | 1020 | return { |
| 990 | 1021 | port: "port" in server ? server.port : undefined, |
| 1022 | held() { | |
| 1023 | let n = 0; | |
| 1024 | for (const { data } of live) n += data.earlyBytes + queued(data.toPeer) + queued(data.toClient); | |
| 1025 | return n; | |
| 1026 | }, | |
| 991 | 1027 | close() { |
| 992 | 1028 | clearInterval(sweep); |
| 993 | 1029 | for (const client of live) { |
| @@ -1018,6 +1054,8 @@ export function relay( | ||
| 1018 | 1054 | * `in.sock` is named in its owner's log. A missing name (the app not yet listening) is not told. |
| 1019 | 1055 | */ |
| 1020 | 1056 | onRefused: (reason: string) => void = () => {}, |
| 1057 | /** Bytes held for a side that does not read; the tests shrink it. */ | |
| 1058 | queueMax: number = QUEUE_MAX, | |
| 1021 | 1059 | ): Egress { |
| 1022 | 1060 | interface Pair { |
| 1023 | 1061 | peer: Socket<undefined> | undefined; |
| @@ -1026,6 +1064,22 @@ export function relay( | ||
| 1026 | 1064 | toClient: Uint8Array[]; |
| 1027 | 1065 | done: boolean; |
| 1028 | 1066 | } |
| 1067 | // A backlog past a quarter of `queueMax` pauses the side that fills it until the other drains; | |
| 1068 | // past all of it the pause did not hold, and both sides are cut (#15207). Unlike the egress data | |
| 1069 | // path, this cuts at the chunk that passes `queueMax` rather than waiting for the pause to land: | |
| 1070 | // deliberate, as the 4 MiB default leaves three quarters of it for the read already under way. | |
| 1071 | const pauseAt = queueMax / PAUSE_AT; | |
| 1072 | const live = new Set<Pair>(); | |
| 1073 | const cut = (client: Socket<Pair>): void => { | |
| 1074 | client.data.done = true; | |
| 1075 | client.data.peer?.terminate(); | |
| 1076 | client.terminate(); | |
| 1077 | }; | |
| 1078 | /** After `filler` added to a backlog of `backlog` bytes: pause it, or cut both past the max. */ | |
| 1079 | const bound = (client: Socket<Pair>, filler: { pause(): void }, backlog: number): void => { | |
| 1080 | if (backlog > queueMax) cut(client); | |
| 1081 | else if (backlog > pauseAt) filler.pause(); | |
| 1082 | }; | |
| 1029 | 1083 | const handlers = { |
| 1030 | 1084 | open(client: Socket<Pair>) { |
| 1031 | 1085 | const pair: Pair = { |
| @@ -1036,6 +1090,7 @@ export function relay( | ||
| 1036 | 1090 | done: false, |
| 1037 | 1091 | }; |
| 1038 | 1092 | client.data = pair; |
| 1093 | live.add(pair); | |
| 1039 | 1094 | // A unix target is in a directory the cell writes (#14752): dial the inode checked, never the |
| 1040 | 1095 | // name. |
| 1041 | 1096 | let pinned: PinnedSocket | undefined; |
| @@ -1058,11 +1113,14 @@ export function relay( | ||
| 1058 | 1113 | dialling = Bun.connect<undefined>({ |
| 1059 | 1114 | ...dial, |
| 1060 | 1115 | socket: { |
| 1061 | data(_peer, chunk) { | |
| 1062 | if (!pair.done) send(client, pair.toClient, chunk); | |
| 1116 | data(peer, chunk) { | |
| 1117 | if (pair.done) return; | |
| 1118 | send(client, pair.toClient, chunk); | |
| 1119 | bound(client, peer, queued(pair.toClient)); | |
| 1063 | 1120 | }, |
| 1064 | 1121 | drain(peer) { |
| 1065 | 1122 | flush(peer, pair.toPeer); |
| 1123 | if (!pair.done && queued(pair.toPeer) <= pauseAt) client.resume(); | |
| 1066 | 1124 | }, |
| 1067 | 1125 | close() { |
| 1068 | 1126 | if (!pair.done) { |
| @@ -1094,6 +1152,7 @@ export function relay( | ||
| 1094 | 1152 | pair.peer = peer; |
| 1095 | 1153 | for (const chunk of pair.early) send(peer, pair.toPeer, chunk); |
| 1096 | 1154 | pair.early = []; |
| 1155 | if (queued(pair.toPeer) <= pauseAt) client.resume(); | |
| 1097 | 1156 | }, |
| 1098 | 1157 | () => { |
| 1099 | 1158 | pinned?.close(); |
| @@ -1104,18 +1163,27 @@ export function relay( | ||
| 1104 | 1163 | }, |
| 1105 | 1164 | data(client: Socket<Pair>, chunk: Uint8Array) { |
| 1106 | 1165 | const pair = client.data; |
| 1107 | if (pair.peer === undefined) pair.early.push(chunk); | |
| 1108 | else send(pair.peer, pair.toPeer, chunk); | |
| 1166 | if (pair.done) return; | |
| 1167 | if (pair.peer === undefined) { | |
| 1168 | pair.early.push(chunk); | |
| 1169 | bound(client, client, queued(pair.early)); | |
| 1170 | return; | |
| 1171 | } | |
| 1172 | send(pair.peer, pair.toPeer, chunk); | |
| 1173 | bound(client, client, queued(pair.toPeer)); | |
| 1109 | 1174 | }, |
| 1110 | 1175 | drain(client: Socket<Pair>) { |
| 1111 | 1176 | flush(client, client.data.toClient); |
| 1177 | if (!client.data.done && queued(client.data.toClient) <= pauseAt) client.data.peer?.resume(); | |
| 1112 | 1178 | }, |
| 1113 | 1179 | close(client: Socket<Pair>) { |
| 1114 | 1180 | client.data.done = true; |
| 1181 | live.delete(client.data); | |
| 1115 | 1182 | client.data.peer?.terminate(); |
| 1116 | 1183 | }, |
| 1117 | 1184 | error(client: Socket<Pair>) { |
| 1118 | 1185 | client.data.done = true; |
| 1186 | live.delete(client.data); | |
| 1119 | 1187 | client.data.peer?.terminate(); |
| 1120 | 1188 | }, |
| 1121 | 1189 | }; |
| @@ -1127,5 +1195,12 @@ export function relay( | ||
| 1127 | 1195 | port: listen.tcp, |
| 1128 | 1196 | socket: handlers, |
| 1129 | 1197 | }); |
| 1130 | return { close: () => server.stop(true) }; | |
| 1198 | return { | |
| 1199 | close: () => server.stop(true), | |
| 1200 | held: () => { | |
| 1201 | let n = 0; | |
| 1202 | for (const p of live) n += queued(p.early) + queued(p.toPeer) + queued(p.toClient); | |
| 1203 | return n; | |
| 1204 | }, | |
| 1205 | }; | |
| 1131 | 1206 | } |
test/egress-bounded.test.ts added +433
| @@ -0,0 +1,433 @@ | ||
| 1 | /** | |
| 2 | * The egress proxy and `relay()` bound what they hold for a side that does not read (#15207): bytes | |
| 3 | * that arrive while the upstream is dialled, and either direction of a relayed pair, pause the side | |
| 4 | * that fills the queue, so a flood stalls at the socket instead of growing the forge's memory. An | |
| 5 | * ordinary exchange through each path stays byte-identical. | |
| 6 | */ | |
| 7 | ||
| 8 | import { afterEach, expect, test } from "bun:test"; | |
| 9 | import type { Socket } from "bun"; | |
| 10 | import { randomBytes } from "node:crypto"; | |
| 11 | import { type Egress, relay, startEgress } from "../src/apps/egress.ts"; | |
| 12 | import { freePort } from "../src/apps/process.ts"; | |
| 13 | ||
| 14 | const pending: (() => void)[] = []; | |
| 15 | afterEach(() => { | |
| 16 | while (pending.length > 0) pending.pop()?.(); | |
| 17 | }); | |
| 18 | ||
| 19 | /** More than loopback's socket buffers hold on both ends together. */ | |
| 20 | const FLOOD = 16 * 1024 * 1024; | |
| 21 | /** What the flood tests let the proxy hold: past the largest single read Bun delivers (512 KiB). */ | |
| 22 | const HOLD = 2 * 1024 * 1024; | |
| 23 | const CHUNK = 64 * 1024; | |
| 24 | /** Byte `i` of every stream is `i % 251`, so a reader checks the order without keeping a copy. */ | |
| 25 | const PATTERN = Uint8Array.from({ length: 251 * 8 + CHUNK }, (_, i) => i % 251); | |
| 26 | ||
| 27 | /** | |
| 28 | * One end of a stream: what it wrote, what it read and whether that was in order, and its close. | |
| 29 | */ | |
| 30 | interface End { | |
| 31 | sent: number; | |
| 32 | got: number; | |
| 33 | inOrder: boolean; | |
| 34 | closed: PromiseWithResolvers<void>; | |
| 35 | wake: (() => void) | undefined; | |
| 36 | } | |
| 37 | ||
| 38 | /** | |
| 39 | * Write `total` bytes of the pattern to `s` as fast as it takes them, until all are or it closes. | |
| 40 | */ | |
| 41 | async function pump(s: Socket<End>, total: number): Promise<void> { | |
| 42 | let open = true; | |
| 43 | void s.data.closed.promise.then(() => (open = false)); | |
| 44 | while (s.data.sent < total && open) { | |
| 45 | const len = Math.min(CHUNK, total - s.data.sent); | |
| 46 | const n = s.write(PATTERN.subarray(s.data.sent % 251, (s.data.sent % 251) + len)); | |
| 47 | if (n > 0) s.data.sent += n; | |
| 48 | if (n < len) { | |
| 49 | await new Promise<void>((resolve) => (s.data.wake = resolve)); | |
| 50 | s.data.wake = undefined; | |
| 51 | } | |
| 52 | } | |
| 53 | } | |
| 54 | ||
| 55 | const ends = { | |
| 56 | open(s: Socket<End>) { | |
| 57 | s.data = { sent: 0, got: 0, inOrder: true, closed: Promise.withResolvers<void>(), wake: undefined }; | |
| 58 | }, | |
| 59 | data(s: Socket<End>, chunk: Uint8Array) { | |
| 60 | for (let i = 0; i < chunk.length; i++) if (chunk[i] !== (s.data.got + i) % 251) s.data.inOrder = false; | |
| 61 | s.data.got += chunk.length; | |
| 62 | }, | |
| 63 | drain(s: Socket<End>) { | |
| 64 | s.data.wake?.(); | |
| 65 | }, | |
| 66 | close(s: Socket<End>) { | |
| 67 | s.data.closed.resolve(); | |
| 68 | s.data.wake?.(); | |
| 69 | }, | |
| 70 | error(s: Socket<End>) { | |
| 71 | s.data.closed.resolve(); | |
| 72 | s.data.wake?.(); | |
| 73 | }, | |
| 74 | }; | |
| 75 | ||
| 76 | /** Resolve once `cond` holds, checking every few milliseconds. */ | |
| 77 | function until(cond: () => boolean): Promise<void> { | |
| 78 | return new Promise((resolve) => { | |
| 79 | const t = setInterval(() => { | |
| 80 | if (!cond()) return; | |
| 81 | clearInterval(t); | |
| 82 | resolve(); | |
| 83 | }, 2); | |
| 84 | pending.push(() => clearInterval(t)); | |
| 85 | }); | |
| 86 | } | |
| 87 | ||
| 88 | /** The most `e` held at any sample, sampled every millisecond until the test ends. */ | |
| 89 | function sample(e: Egress): () => number { | |
| 90 | let max = 0; | |
| 91 | const t = setInterval(() => (max = Math.max(max, e.held?.() ?? 0)), 1); | |
| 92 | pending.push(() => clearInterval(t)); | |
| 93 | return () => Math.max(max, e.held?.() ?? 0); | |
| 94 | } | |
| 95 | ||
| 96 | /** | |
| 97 | * A reader that stopped is paused past a quarter of HOLD, held there, then let go: the proxy never | |
| 98 | * held more than HOLD, nothing was cut, and every byte arrived in order. The pause is what keeps | |
| 99 | * the flood under HOLD without a cut: delete it and the queue passes HOLD, the cut fires, and the | |
| 100 | * bytes never all arrive. | |
| 101 | */ | |
| 102 | async function heldThenLetGo(e: Egress, max: () => number, resume: () => void, writer: () => End, reader: () => End): Promise<void> { | |
| 103 | // A cut on either end ends every wait at once, and fails below. | |
| 104 | let cut = false; | |
| 105 | for (const end of [writer(), reader()]) void end.closed.promise.then(() => (cut = true)); | |
| 106 | // Pressure: the proxy holds past the pause line and the writer's own socket is full, so it waits | |
| 107 | // on a drain that only the proxy reading again would bring. | |
| 108 | await until(() => cut || ((e.held?.() ?? 0) > HOLD / 4 && writer().wake !== undefined)); | |
| 109 | resume(); | |
| 110 | await until(() => cut || reader().got >= FLOOD); | |
| 111 | expect(cut).toBe(false); | |
| 112 | expect(max()).toBeLessThanOrEqual(HOLD); | |
| 113 | expect(writer().sent).toBe(FLOOD); | |
| 114 | expect(reader().inOrder).toBe(true); | |
| 115 | } | |
| 116 | ||
| 117 | test("a flood while the upstream is dialled is held under queueMax and goes out in order once it answers", async () => { | |
| 118 | // The cell's proxy answers CONNECT only when told to, then reads the tunnel. | |
| 119 | const answer = Promise.withResolvers<void>(); | |
| 120 | let far: Socket<End> | undefined; | |
| 121 | const upstream = Bun.listen<End & { head?: boolean }>({ | |
| 122 | hostname: "127.0.0.1", | |
| 123 | port: 0, | |
| 124 | socket: { | |
| 125 | ...ends, | |
| 126 | open(s) { | |
| 127 | ends.open(s); | |
| 128 | s.data.head = true; | |
| 129 | far = s; | |
| 130 | }, | |
| 131 | data(s, chunk) { | |
| 132 | if (!s.data.head) return ends.data(s, chunk); | |
| 133 | // The proxy sends the CONNECT alone and nothing more until it has the answer. | |
| 134 | s.data.head = false; | |
| 135 | void answer.promise.then(() => s.write("HTTP/1.1 200 Connection Established\r\n\r\n")); | |
| 136 | }, | |
| 137 | }, | |
| 138 | }); | |
| 139 | pending.push(() => upstream.stop(true)); | |
| 140 | const listen = freePort(); | |
| 141 | const egress = startEgress( | |
| 142 | { tcp: listen }, | |
| 143 | { | |
| 144 | hosts: [], | |
| 145 | origins: [{ host: "example.test", port: 443 }], | |
| 146 | label: `bd${listen}`, | |
| 147 | queueMax: HOLD, | |
| 148 | upstream: { host: "127.0.0.1", port: upstream.port }, | |
| 149 | }, | |
| 150 | ); | |
| 151 | pending.push(() => egress.close()); | |
| 152 | const max = sample(egress); | |
| 153 | const sock = await Bun.connect<End>({ hostname: "127.0.0.1", port: listen, socket: { ...ends, data() {} } }); | |
| 154 | pending.push(() => sock.terminate()); | |
| 155 | sock.write("CONNECT example.test:443 HTTP/1.1\r\n\r\n"); | |
| 156 | await until(() => far !== undefined); | |
| 157 | void pump(sock, FLOOD); | |
| 158 | await heldThenLetGo(egress, max, () => answer.resolve(), () => sock.data, () => far!.data); | |
| 159 | }, 30_000); | |
| 160 | ||
| 161 | test("a relay target writing faster than its client reads is paused, and resumes in order", async () => { | |
| 162 | let near: Socket<End> | undefined; | |
| 163 | const target = Bun.listen<End>({ | |
| 164 | hostname: "127.0.0.1", | |
| 165 | port: 0, | |
| 166 | socket: { | |
| 167 | ...ends, | |
| 168 | open(s) { | |
| 169 | ends.open(s); | |
| 170 | near = s; | |
| 171 | void pump(s, FLOOD); | |
| 172 | }, | |
| 173 | }, | |
| 174 | }); | |
| 175 | pending.push(() => target.stop(true)); | |
| 176 | const listen = freePort(); | |
| 177 | const r = relay({ tcp: listen }, { tcp: target.port }, () => {}, HOLD); | |
| 178 | pending.push(() => r.close()); | |
| 179 | const max = sample(r); | |
| 180 | // A client that does not read until it is let go. | |
| 181 | const client = await Bun.connect<End>({ | |
| 182 | hostname: "127.0.0.1", | |
| 183 | port: listen, | |
| 184 | socket: { ...ends, open(s) { | |
| 185 | ends.open(s); | |
| 186 | s.pause(); | |
| 187 | }, }, | |
| 188 | }); | |
| 189 | pending.push(() => client.terminate()); | |
| 190 | await until(() => near !== undefined); | |
| 191 | await heldThenLetGo(r, max, () => client.resume(), () => near!.data, () => client.data); | |
| 192 | }, 30_000); | |
| 193 | ||
| 194 | test("a relay client writing faster than its target reads is paused, and resumes in order", async () => { | |
| 195 | let far: Socket<End> | undefined; | |
| 196 | // A target that does not read until it is let go. | |
| 197 | const target = Bun.listen<End>({ | |
| 198 | hostname: "127.0.0.1", | |
| 199 | port: 0, | |
| 200 | socket: { ...ends, open(s) { | |
| 201 | ends.open(s); | |
| 202 | s.pause(); | |
| 203 | far = s; | |
| 204 | }, }, | |
| 205 | }); | |
| 206 | pending.push(() => target.stop(true)); | |
| 207 | const listen = freePort(); | |
| 208 | const r = relay({ tcp: listen }, { tcp: target.port }, () => {}, HOLD); | |
| 209 | pending.push(() => r.close()); | |
| 210 | const max = sample(r); | |
| 211 | const sock = await Bun.connect<End>({ hostname: "127.0.0.1", port: listen, socket: ends }); | |
| 212 | pending.push(() => sock.terminate()); | |
| 213 | void pump(sock, FLOOD); | |
| 214 | await until(() => far !== undefined); | |
| 215 | await heldThenLetGo(r, max, () => far!.resume(), () => sock.data, () => far!.data); | |
| 216 | }, 30_000); | |
| 217 | ||
| 218 | test("one chunk past queueMax while the upstream is dialled cuts the client, and says so", async () => { | |
| 219 | // The cell's proxy accepts but never answers CONNECT: the dial is in flight for ever. | |
| 220 | let dialled = false; | |
| 221 | const silent = Bun.listen({ hostname: "127.0.0.1", port: 0, socket: { open: () => void (dialled = true), data() {} } }); | |
| 222 | pending.push(() => silent.stop(true)); | |
| 223 | const listen = freePort(); | |
| 224 | const lines: string[] = []; | |
| 225 | const egress = startEgress( | |
| 226 | { tcp: listen }, | |
| 227 | { | |
| 228 | hosts: [], | |
| 229 | origins: [{ host: "example.test", port: 443 }], | |
| 230 | label: `bc${listen}`, | |
| 231 | queueMax: 1024, | |
| 232 | upstream: { host: "127.0.0.1", port: silent.port }, | |
| 233 | }, | |
| 234 | (line) => lines.push(line), | |
| 235 | ); | |
| 236 | pending.push(() => egress.close()); | |
| 237 | const sock = await Bun.connect<End>({ hostname: "127.0.0.1", port: listen, socket: ends }); | |
| 238 | pending.push(() => sock.terminate()); | |
| 239 | sock.write("CONNECT example.test:443 HTTP/1.1\r\n\r\n"); | |
| 240 | await until(() => dialled); | |
| 241 | // One write, one read on loopback: past queueMax in the first `data` after the head. | |
| 242 | expect(sock.write(PATTERN.subarray(0, CHUNK))).toBe(CHUNK); | |
| 243 | await sock.data.closed.promise; | |
| 244 | expect(lines.some((l) => l.startsWith("egress cut: "))).toBe(true); | |
| 245 | expect(egress.held?.()).toBe(0); | |
| 246 | }, 30_000); | |
| 247 | ||
| 248 | // The relay writes what it reads straight into the other side's socket, so a backlog starts only | |
| 249 | // once that socket's buffers are full. With `queueMax` 0 the first byte the relay must keep is past | |
| 250 | // it: the cut is reached as soon as the buffers fill, and never left to the pause. | |
| 251 | test("a relay client flooding a target that never reads is cut, and the target sees the close", async () => { | |
| 252 | let far: Socket<End> | undefined; | |
| 253 | const target = Bun.listen<End>({ | |
| 254 | hostname: "127.0.0.1", | |
| 255 | port: 0, | |
| 256 | socket: { ...ends, open(s) { | |
| 257 | ends.open(s); | |
| 258 | s.pause(); | |
| 259 | far = s; | |
| 260 | }, }, | |
| 261 | }); | |
| 262 | pending.push(() => target.stop(true)); | |
| 263 | const listen = freePort(); | |
| 264 | const r = relay({ tcp: listen }, { tcp: target.port }, () => {}, 0); | |
| 265 | pending.push(() => r.close()); | |
| 266 | const sock = await Bun.connect<End>({ hostname: "127.0.0.1", port: listen, socket: ends }); | |
| 267 | pending.push(() => sock.terminate()); | |
| 268 | void pump(sock, FLOOD); | |
| 269 | await sock.data.closed.promise; | |
| 270 | await until(() => far !== undefined); | |
| 271 | // A paused socket still hears the close: resume it so its close callback runs. | |
| 272 | far!.resume(); | |
| 273 | await far!.data.closed.promise; | |
| 274 | expect(sock.data.sent).toBeLessThan(FLOOD); | |
| 275 | expect(r.held?.()).toBe(0); | |
| 276 | }, 30_000); | |
| 277 | ||
| 278 | test("a relay target flooding a client that never reads is cut, and the target sees the close", async () => { | |
| 279 | let near: Socket<End> | undefined; | |
| 280 | const target = Bun.listen<End>({ | |
| 281 | hostname: "127.0.0.1", | |
| 282 | port: 0, | |
| 283 | socket: { | |
| 284 | ...ends, | |
| 285 | open(s) { | |
| 286 | ends.open(s); | |
| 287 | near = s; | |
| 288 | void pump(s, FLOOD); | |
| 289 | }, | |
| 290 | }, | |
| 291 | }); | |
| 292 | pending.push(() => target.stop(true)); | |
| 293 | const listen = freePort(); | |
| 294 | const r = relay({ tcp: listen }, { tcp: target.port }, () => {}, 0); | |
| 295 | pending.push(() => r.close()); | |
| 296 | const client = await Bun.connect<End>({ | |
| 297 | hostname: "127.0.0.1", | |
| 298 | port: listen, | |
| 299 | socket: { ...ends, open(s) { | |
| 300 | ends.open(s); | |
| 301 | s.pause(); | |
| 302 | }, }, | |
| 303 | }); | |
| 304 | pending.push(() => client.terminate()); | |
| 305 | await until(() => near !== undefined); | |
| 306 | await near!.data.closed.promise; | |
| 307 | client.resume(); | |
| 308 | await client.data.closed.promise; | |
| 309 | expect(near!.data.sent).toBeLessThan(FLOOD); | |
| 310 | expect(r.held?.()).toBe(0); | |
| 311 | }, 30_000); | |
| 312 | ||
| 313 | /** An echo server that honours its own backpressure. */ | |
| 314 | function echo(): number { | |
| 315 | const server = Bun.listen<{ queue: Uint8Array[] }>({ | |
| 316 | hostname: "127.0.0.1", | |
| 317 | port: 0, | |
| 318 | socket: { | |
| 319 | open(s) { | |
| 320 | s.data = { queue: [] }; | |
| 321 | }, | |
| 322 | data(s, chunk) { | |
| 323 | if (s.data.queue.length > 0) { | |
| 324 | s.data.queue.push(chunk); | |
| 325 | return; | |
| 326 | } | |
| 327 | const n = s.write(chunk); | |
| 328 | if (n < chunk.length) { | |
| 329 | s.data.queue.push(chunk.subarray(Math.max(n, 0))); | |
| 330 | s.pause(); | |
| 331 | } | |
| 332 | }, | |
| 333 | drain(s) { | |
| 334 | while (s.data.queue.length > 0) { | |
| 335 | const c = s.data.queue[0]!; | |
| 336 | const n = s.write(c); | |
| 337 | if (n < c.length) { | |
| 338 | s.data.queue[0] = c.subarray(Math.max(n, 0)); | |
| 339 | return; | |
| 340 | } | |
| 341 | s.data.queue.shift(); | |
| 342 | } | |
| 343 | s.resume(); | |
| 344 | }, | |
| 345 | }, | |
| 346 | }); | |
| 347 | pending.push(() => server.stop(true)); | |
| 348 | return server.port; | |
| 349 | } | |
| 350 | ||
| 351 | /** | |
| 352 | * Connect to `port`, write `out` (after `head`), and collect everything back until `want` bytes. | |
| 353 | */ | |
| 354 | async function exchange(port: number, head: Uint8Array, out: Uint8Array, want: number, slow = false): Promise<Uint8Array> { | |
| 355 | const got: Uint8Array[] = []; | |
| 356 | let n = 0; | |
| 357 | const done = Promise.withResolvers<void>(); | |
| 358 | const queue: Uint8Array[] = []; | |
| 359 | const sock = await Bun.connect<undefined>({ | |
| 360 | hostname: "127.0.0.1", | |
| 361 | port, | |
| 362 | socket: { | |
| 363 | data(s, chunk) { | |
| 364 | got.push(new Uint8Array(chunk)); | |
| 365 | n += chunk.length; | |
| 366 | if (n >= want) done.resolve(); | |
| 367 | else if (slow && got.length % 16 === 0) { | |
| 368 | // Stop reading for a while, so the far side's backlog fills and it is paused. | |
| 369 | s.pause(); | |
| 370 | setTimeout(() => s.resume(), 100); | |
| 371 | } | |
| 372 | }, | |
| 373 | drain(s) { | |
| 374 | while (queue.length > 0) { | |
| 375 | const c = queue[0]!; | |
| 376 | const w = s.write(c); | |
| 377 | if (w < c.length) { | |
| 378 | queue[0] = c.subarray(Math.max(w, 0)); | |
| 379 | return; | |
| 380 | } | |
| 381 | queue.shift(); | |
| 382 | } | |
| 383 | }, | |
| 384 | close: () => done.resolve(), | |
| 385 | error: () => done.resolve(), | |
| 386 | }, | |
| 387 | }); | |
| 388 | pending.push(() => sock.terminate()); | |
| 389 | for (const c of [head, out]) { | |
| 390 | if (queue.length > 0) { | |
| 391 | queue.push(c); | |
| 392 | continue; | |
| 393 | } | |
| 394 | const w = sock.write(c); | |
| 395 | if (w < c.length) queue.push(c.subarray(Math.max(w, 0))); | |
| 396 | } | |
| 397 | await done.promise; | |
| 398 | const all = new Uint8Array(n); | |
| 399 | let at = 0; | |
| 400 | for (const c of got) { | |
| 401 | all.set(c, at); | |
| 402 | at += c.length; | |
| 403 | } | |
| 404 | return all; | |
| 405 | } | |
| 406 | ||
| 407 | test("an ordinary exchange through the proxy and the relay is byte-identical, a slow reader included", async () => { | |
| 408 | const port = echo(); | |
| 409 | const payload = new Uint8Array(randomBytes(8 * 1024 * 1024)); | |
| 410 | // Through the proxy: the payload follows the head at once, so most of it arrives during the dial. | |
| 411 | const listen = freePort(); | |
| 412 | const egress = startEgress( | |
| 413 | { tcp: listen }, | |
| 414 | { hosts: [], origins: [{ host: "127.0.0.1", port }], label: `bi${listen}` }, | |
| 415 | ); | |
| 416 | pending.push(() => egress.close()); | |
| 417 | const established = new TextEncoder().encode("HTTP/1.1 200 Connection Established\r\n\r\n"); | |
| 418 | const viaProxy = await exchange( | |
| 419 | listen, | |
| 420 | new TextEncoder().encode(`CONNECT 127.0.0.1:${port} HTTP/1.1\r\n\r\n`), | |
| 421 | payload, | |
| 422 | established.length + payload.length, | |
| 423 | true, | |
| 424 | ); | |
| 425 | expect(Buffer.from(viaProxy.subarray(0, established.length)).equals(Buffer.from(established))).toBe(true); | |
| 426 | expect(Buffer.from(viaProxy.subarray(established.length)).equals(Buffer.from(payload))).toBe(true); | |
| 427 | // Through the relay, both directions at once. | |
| 428 | const at = freePort(); | |
| 429 | const r = relay({ tcp: at }, { tcp: port }); | |
| 430 | pending.push(() => r.close()); | |
| 431 | const viaRelay = await exchange(at, new Uint8Array(0), payload, payload.length, true); | |
| 432 | expect(Buffer.from(viaRelay).equals(Buffer.from(payload))).toBe(true); | |
| 433 | }, 30_000); |
new above
offline