Public: anyone can read this; the crew can change it.
Bun 1.4.4 leaves two tests red on main: the keep-alive handover and a websocket handshake the app refuses #12728
Check · Review · Land · Live done — Landed and live in production.
mason/forge:mason/12727-bun-1-4-4-leaves-two-tests-red-on-main-t → main 4 commits fork 1 thread · 1 unresolved
1 file
changed +108
−13
test/release-handover-keepalive.test.ts +108 −13
| @@ -21,7 +21,67 @@ afterAll(async () => { | ||
| 21 | 21 | for (const d of dirs) await rm(d, { recursive: true, force: true }); |
| 22 | 22 | }); |
| 23 | 23 | |
| 24 | test("after stopAccepting, a kept-alive connection is closed after one answer and the next request reaches the release", async () => { | |
| 24 | /** A minimal HTTP/1.1 client on one TCP connection, so reuse is provable. */ | |
| 25 | async function openRaw(port: number) { | |
| 26 | let buf = ""; | |
| 27 | let wake: (() => void) | undefined; | |
| 28 | let onClosed: () => void = () => {}; | |
| 29 | const closed = new Promise<void>((r) => { | |
| 30 | onClosed = r; | |
| 31 | }); | |
| 32 | const socket = await Bun.connect({ | |
| 33 | hostname: "127.0.0.1", | |
| 34 | port, | |
| 35 | socket: { | |
| 36 | data(_s, chunk) { | |
| 37 | buf += new TextDecoder().decode(chunk); | |
| 38 | wake?.(); | |
| 39 | }, | |
| 40 | close() { | |
| 41 | onClosed(); | |
| 42 | wake?.(); | |
| 43 | }, | |
| 44 | }, | |
| 45 | }); | |
| 46 | return { | |
| 47 | get(path: string) { | |
| 48 | return this.send(`GET ${path} HTTP/1.1\r\nHost: 127.0.0.1:${port}\r\n\r\n`); | |
| 49 | }, | |
| 50 | /** Write raw bytes without waiting: the front half of a split request. */ | |
| 51 | write: (raw: string) => socket.write(raw), | |
| 52 | /** Write raw bytes, then read one answer. */ | |
| 53 | send(raw: string) { | |
| 54 | buf = ""; | |
| 55 | socket.write(raw); | |
| 56 | return this.answer(); | |
| 57 | }, | |
| 58 | async answer() { | |
| 59 | const deadline = Date.now() + 3_000; | |
| 60 | for (;;) { | |
| 61 | const end = buf.indexOf("\r\n\r\n"); | |
| 62 | if (end >= 0) { | |
| 63 | const head = buf.slice(0, end); | |
| 64 | const length = Number(/content-length: (\d+)/i.exec(head)?.[1] ?? 0); | |
| 65 | if (buf.length - end - 4 >= length) { | |
| 66 | return { | |
| 67 | status: Number(head.slice(9, 12)), | |
| 68 | connection: /^connection: (.+)$/im.exec(head)?.[1]?.trim() ?? null, | |
| 69 | }; | |
| 70 | } | |
| 71 | } | |
| 72 | if (Date.now() > deadline) throw new Error("no answer"); | |
| 73 | await new Promise<void>((r) => { | |
| 74 | wake = r; | |
| 75 | setTimeout(r, 50); // real-timer: a socket read has no event to await but its own arrival; the wait is capped and woken by data | |
| 76 | }); | |
| 77 | } | |
| 78 | }, | |
| 79 | closed, | |
| 80 | close: () => socket.end(), | |
| 81 | }; | |
| 82 | } | |
| 83 | ||
| 84 | test("after stopAccepting, an idle kept-alive connection is closed by the handover and the next request reaches the release", async () => { | |
| 25 | 85 | const oldDir = await mkdtemp(join(tmpdir(), "forge-keepalive-old-")); |
| 26 | 86 | const newDir = await mkdtemp(join(tmpdir(), "forge-keepalive-new-")); |
| 27 | 87 | dirs.push(oldDir, newDir); |
| @@ -38,25 +98,32 @@ test("after stopAccepting, a kept-alive connection is closed after one answer an | ||
| 38 | 98 | }); |
| 39 | 99 | expect(made.ok).toBe(true); |
| 40 | 100 | const url = `http://127.0.0.1:${oldServers.port}/api/v1/users/alice`; |
| 41 | const ask = async () => { | |
| 42 | const res = await fetch(url, { signal: AbortSignal.timeout(3_000) }); | |
| 43 | await res.arrayBuffer(); | |
| 44 | return { status: res.status, connection: res.headers.get("connection") }; | |
| 45 | }; | |
| 101 | // One raw keep-alive connection: Bun 1.4.4's fetch no longer carries a | |
| 102 | // warmed connection across the handover, so the test holds its own. | |
| 103 | const sock = await openRaw(oldServers.port); | |
| 104 | const ask = () => sock.get("/api/v1/users/alice"); | |
| 46 | 105 | |
| 47 | // Warm a kept-alive connection while the old forge is the only listener. | |
| 106 | // Warm the kept-alive connection while the old forge is the only listener. | |
| 48 | 107 | for (let i = 0; i < 3; i++) expect((await ask()).status).toBe(200); |
| 49 | 108 | |
| 50 | 109 | const newApp = createApp({ dataDir: newDir, port: oldServers.port, selfSwap: true }); |
| 51 | 110 | const newServers = await serve(newApp); |
| 52 | 111 | const drained = oldServers.stopAccepting(5_000); |
| 53 | 112 | |
| 54 | // The old forge answers what was already its connection once more, and ends it. | |
| 55 | const last = await ask(); | |
| 56 | expect(last.status).toBe(200); | |
| 57 | expect(last.connection).toBe("close"); | |
| 58 | // Every request after that is the release's. | |
| 59 | for (let i = 0; i < 5; i++) expect((await ask()).status).toBe(404); | |
| 113 | // Bun 1.4.4 closes an idle kept-alive connection itself when the listener | |
| 114 | // stops, so the old forge cannot go on answering on it: the redial is the | |
| 115 | // release's. (A connection busy at that moment still gets `connection: | |
| 116 | // close` from the old forge's own fetch wrapper.) | |
| 117 | await sock.closed; | |
| 118 | ||
| 119 | // Every request after that is the release's: a fresh connection reaches it. | |
| 120 | const fresh = async () => { | |
| 121 | const res = await fetch(url, { signal: AbortSignal.timeout(3_000), keepalive: false }); | |
| 122 | await res.arrayBuffer(); | |
| 123 | return res.status; | |
| 124 | }; | |
| 125 | for (let i = 0; i < 5; i++) expect(await fresh()).toBe(404); | |
| 126 | sock.close(); | |
| 60 | 127 | |
| 61 | 128 | await drained; |
| 62 | 129 | oldServers.stop(); |
| @@ -64,3 +131,31 @@ test("after stopAccepting, a kept-alive connection is closed after one answer an | ||
| 64 | 131 | oldApp.close(); |
| 65 | 132 | newApp.close(); |
| 66 | 133 | }, 20_000); |
| 134 | ||
| 135 | test("a request in flight when stopAccepting runs is answered with connection: close", async () => { | |
| 136 | const dir = await mkdtemp(join(tmpdir(), "forge-keepalive-busy-")); | |
| 137 | dirs.push(dir); | |
| 138 | const app = createApp({ dataDir: dir, port: 0, selfSwap: true }); | |
| 139 | const servers = await serve(app); | |
| 140 | const sock = await openRaw(servers.port); | |
| 141 | const body = JSON.stringify({ username: "bob" }); | |
| 142 | const head = | |
| 143 | `POST /api/v1/users HTTP/1.1\r\nHost: 127.0.0.1:${servers.port}\r\n` + | |
| 144 | `Origin: http://127.0.0.1:${servers.port}\r\nContent-Type: application/json\r\n` + | |
| 145 | `Content-Length: ${body.length}\r\n\r\n`; | |
| 146 | // The handler is waiting on the rest of the body: the request is in flight. | |
| 147 | sock.write(head + body.slice(0, 2)); | |
| 148 | const deadline = Date.now() + 3_000; | |
| 149 | while (servers.inFlight === 0) { | |
| 150 | if (Date.now() > deadline) throw new Error("the request never reached the handler"); | |
| 151 | await Bun.sleep(10); // real-timer: the server exposes no event for a request arriving; capped by the deadline | |
| 152 | } | |
| 153 | const drained = servers.stopAccepting(5_000); | |
| 154 | sock.write(body.slice(2)); | |
| 155 | const answer = await sock.answer(); | |
| 156 | expect(answer.connection?.toLowerCase()).toBe("close"); | |
| 157 | await drained; | |
| 158 | sock.close(); | |
| 159 | servers.stop(); | |
| 160 | app.close(); | |
| 161 | }, 20_000); |
new above
offline