-
Notifications
You must be signed in to change notification settings - Fork 5k
redis: close the socket when the connection fails #33479
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
robobun
wants to merge
1
commit into
main
Choose a base branch
from
farm/8d9df4c3/redis-fail-closes-socket
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+180
−4
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,171 @@ | ||
| import type { RedisOptions } from "bun"; | ||
| import { describe, expect, test } from "bun:test"; | ||
| import net from "net"; | ||
|
|
||
| /** | ||
| * A connection-level failure must close the socket, so `on_close` gets to run the | ||
| * client's reconnect / `onclose` policy. Without that the client is stranded in | ||
| * `failed && connected`: every command rejects, but nothing ever tells the app. | ||
| */ | ||
|
|
||
| const CRLF = "\r\n"; | ||
| const bulk = (value: string) => `$${Buffer.byteLength(value)}${CRLF}${value}${CRLF}`; | ||
| const HELLO_REPLY = `%3${CRLF}${bulk("server")}${bulk("redis")}${bulk("proto")}:3${CRLF}${bulk("version")}${bulk("7.4.0")}`; | ||
| /** `\x01` is not a RESP type byte, so the client must treat the reply as a protocol failure. */ | ||
| const UNPARSEABLE_REPLY = `\x01not-a-resp-type${CRLF}`; | ||
|
|
||
| /** Pull every complete `*N\r\n$len\r\n…` command frame out of `buffer`. */ | ||
| function takeCommands(buffer: Buffer): { commands: string[][]; rest: Buffer } { | ||
| const commands: string[][] = []; | ||
| let rest = buffer; | ||
| while (rest.length > 0 && rest[0] === 0x2a /* '*' */) { | ||
| const headerEnd = rest.indexOf(CRLF); | ||
| if (headerEnd < 0) break; | ||
| const argCount = Number(rest.subarray(1, headerEnd).toString()); | ||
| if (!Number.isInteger(argCount) || argCount < 0) break; | ||
|
|
||
| let pos = headerEnd + 2; | ||
| const argv: string[] = []; | ||
| let complete = true; | ||
| for (let i = 0; i < argCount; i++) { | ||
| if (pos >= rest.length || rest[pos] !== 0x24 /* '$' */) { | ||
| complete = false; | ||
| break; | ||
| } | ||
| const lengthEnd = rest.indexOf(CRLF, pos); | ||
| if (lengthEnd < 0) { | ||
| complete = false; | ||
| break; | ||
| } | ||
| const length = Number(rest.subarray(pos + 1, lengthEnd).toString()); | ||
| if (!Number.isInteger(length) || length < 0 || rest.length < lengthEnd + 2 + length + 2) { | ||
| complete = false; | ||
| break; | ||
| } | ||
| argv.push(rest.subarray(lengthEnd + 2, lengthEnd + 2 + length).toString()); | ||
| pos = lengthEnd + 2 + length + 2; | ||
| } | ||
| if (!complete) break; | ||
|
|
||
| commands.push(argv); | ||
| rest = rest.subarray(pos); | ||
| } | ||
| return { commands, rest }; | ||
| } | ||
|
|
||
| type MockServer = Disposable & { | ||
| port: number; | ||
| /** Resolves once a client connection has been torn down. */ | ||
| disconnected: Promise<void>; | ||
| }; | ||
|
|
||
| async function startMockValkey(respond: (socket: net.Socket, argv: string[]) => void) { | ||
| const { promise: disconnected, resolve: onDisconnected } = Promise.withResolvers<void>(); | ||
| const sockets: net.Socket[] = []; | ||
|
|
||
| const server = net.createServer(socket => { | ||
| sockets.push(socket); | ||
| let buffered = Buffer.alloc(0); | ||
| socket.on("data", chunk => { | ||
| buffered = buffered.length > 0 ? Buffer.concat([buffered, chunk]) : chunk; | ||
| const { commands, rest } = takeCommands(buffered); | ||
| buffered = rest; | ||
| for (const argv of commands) respond(socket, argv); | ||
| }); | ||
| socket.on("error", () => {}); | ||
| socket.on("close", () => onDisconnected()); | ||
| }); | ||
|
|
||
| await new Promise<void>((resolve, reject) => { | ||
| server.once("error", reject); | ||
| server.listen(0, "127.0.0.1", resolve); | ||
| }); | ||
|
|
||
| return { | ||
| port: (server.address() as net.AddressInfo).port, | ||
| disconnected, | ||
| [Symbol.dispose]() { | ||
| for (const socket of sockets) socket.destroy(); | ||
| server.close(); | ||
| }, | ||
| } satisfies MockServer; | ||
| } | ||
|
|
||
| function createClient(port: number, options: RedisOptions) { | ||
| return new Bun.RedisClient(`redis://127.0.0.1:${port}`, { connectionTimeout: 5_000, ...options }); | ||
| } | ||
|
|
||
| describe("Valkey: a failed connection is torn down", () => { | ||
| test("a protocol error closes the socket and fires onclose", async () => { | ||
| using server = await startMockValkey((socket, argv) => { | ||
| if (argv[0].toUpperCase() === "HELLO") socket.write(HELLO_REPLY); | ||
| else socket.write(UNPARSEABLE_REPLY); | ||
| }); | ||
| const client = createClient(server.port, { autoReconnect: false }); | ||
| const { promise: closed, resolve: onClosed } = Promise.withResolvers<void>(); | ||
| client.onclose = () => onClosed(); | ||
|
|
||
| try { | ||
| await client.connect(); | ||
| const rejection = await client.ping().then( | ||
| () => null, | ||
| (thrown: any) => thrown, | ||
| ); | ||
| expect(rejection?.code).toBe("ERR_REDIS_INVALID_RESPONSE_TYPE"); | ||
|
|
||
| await closed; | ||
| await server.disconnected; | ||
| expect(client.connected).toBe(false); | ||
| } finally { | ||
| client.close(); | ||
| } | ||
| }); | ||
|
|
||
| // `onclose` only fires on the terminal branch of `on_close` — the reconnecting branch | ||
| // never calls it — so seeing it here is proof the client did not re-dial. That matters: | ||
| // a repeatable post-handshake failure would otherwise loop forever, because a successful | ||
| // HELLO resets `retry_attempts` and `maxRetries` is never reached. | ||
| test("a protocol error is terminal even with autoReconnect enabled, and connect() revives", async () => { | ||
| let handshakes = 0; | ||
| let pings = 0; | ||
|
|
||
| using server = await startMockValkey((socket, argv) => { | ||
| switch (argv[0].toUpperCase()) { | ||
| case "HELLO": | ||
| handshakes++; | ||
| socket.write(HELLO_REPLY); | ||
| break; | ||
| case "PING": | ||
| // Break the first connection mid-stream, then answer normally. | ||
| socket.write(++pings === 1 ? UNPARSEABLE_REPLY : `+PONG${CRLF}`); | ||
| break; | ||
| default: | ||
| socket.write(`+OK${CRLF}`); | ||
| break; | ||
| } | ||
| }); | ||
| const client = createClient(server.port, { autoReconnect: true }); | ||
| const { promise: closed, resolve: onClosed } = Promise.withResolvers<void>(); | ||
| client.onclose = () => onClosed(); | ||
|
|
||
| try { | ||
| await client.connect(); | ||
| const rejection = await client.ping().then( | ||
| () => null, | ||
| (thrown: any) => thrown, | ||
| ); | ||
| expect(rejection?.code).toBe("ERR_REDIS_INVALID_RESPONSE_TYPE"); | ||
|
|
||
| await closed; | ||
| await server.disconnected; | ||
| expect(client.connected).toBe(false); | ||
|
|
||
| // The app is handed the failure and can re-dial on its own terms. | ||
| await client.connect(); | ||
| expect(await client.ping()).toBe("PONG"); | ||
| expect({ connected: client.connected, handshakes }).toEqual({ connected: true, handshakes: 2 }); | ||
| } finally { | ||
| client.close(); | ||
| } | ||
| }); | ||
| }); |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.