-
Notifications
You must be signed in to change notification settings - Fork 2
🪲 BUG-#1: Fix RpcClient pending requests leak #13
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
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
675dd33
🪲 BUG-#1: Reject pending RPC requests when the process exits or fails…
FernandoCelmer d2b9fbd
❤️ TEST-#1: Add coverage for pending-request rejection on exit/spawnE…
FernandoCelmer 4e644e2
🪲 BUG-#1: Inject a fake ProcessHandle in tests instead of spawning a …
FernandoCelmer fc1a85d
🪲 BUG-#1: Clear pending before notifying resolvers; guard new request…
FernandoCelmer 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,86 @@ | ||
| import assert from "node:assert/strict"; | ||
| import { EventEmitter } from "node:events"; | ||
| import { test } from "node:test"; | ||
| import { ProcessHandle, RpcClient } from "../src/core-client/core-client"; | ||
|
|
||
| class FakeProcess extends EventEmitter implements ProcessHandle { | ||
| killed = false; | ||
|
|
||
| write(_data: string): void {} | ||
|
|
||
| kill(): void { | ||
| this.killed = true; | ||
| } | ||
| } | ||
|
|
||
| test("pending requests reject when the process exits mid-flight", async () => { | ||
| const fake = new FakeProcess(); | ||
| const client = new RpcClient("fake", [], process.cwd(), undefined, fake); | ||
|
|
||
| const pending = client.request("chat/send", { prompt: "hi" }); | ||
| fake.emit("exit", { code: 1, signal: null }); | ||
|
|
||
| const response = await pending; | ||
| assert.equal(response.error?.code, -32000); | ||
| }); | ||
|
|
||
| test("pending requests reject when the process fails to spawn", async () => { | ||
| const fake = new FakeProcess(); | ||
| const client = new RpcClient("fake", [], process.cwd(), undefined, fake); | ||
|
|
||
| const pending = client.request("chat/send", { prompt: "hi" }); | ||
| fake.emit("spawnError", new Error("ENOENT")); | ||
|
|
||
| const response = await pending; | ||
| assert.equal(response.error?.code, -32000); | ||
| assert.equal(response.error?.message, "ENOENT"); | ||
| }); | ||
|
|
||
| test("dispose rejects any still-pending request and kills the process", async () => { | ||
| const fake = new FakeProcess(); | ||
| const client = new RpcClient("fake", [], process.cwd(), undefined, fake); | ||
|
|
||
| const pending = client.request("chat/send", { prompt: "hi" }); | ||
| client.dispose(); | ||
|
|
||
| const response = await pending; | ||
| assert.equal(response.error?.code, -32000); | ||
| assert.equal(fake.killed, true); | ||
| }); | ||
|
|
||
| test("a real RPC response still resolves normally, not through rejectPending", async () => { | ||
| const fake = new FakeProcess(); | ||
| const client = new RpcClient("fake", [], process.cwd(), undefined, fake); | ||
|
|
||
| const pending = client.request("chat/send", { prompt: "hi" }); | ||
| fake.emit("line", JSON.stringify({ jsonrpc: "2.0", id: "1", result: { text: "ok" } })); | ||
|
|
||
| const response = await pending; | ||
| assert.deepEqual(response.result, { text: "ok" }); | ||
| }); | ||
|
|
||
| test("a re-entrant request made from inside a rejected .then() is not silently dropped", async () => { | ||
| const fake = new FakeProcess(); | ||
| const client = new RpcClient("fake", [], process.cwd(), undefined, fake); | ||
|
|
||
| const retryResponse = client.request("chat/send", { prompt: "first" }).then((first) => { | ||
| assert.equal(first.error?.code, -32000); | ||
| return client.request("chat/send", { prompt: "retry" }); | ||
| }); | ||
| fake.emit("exit", { code: 1, signal: null }); | ||
|
|
||
| const response = await retryResponse; | ||
| assert.equal(response.error?.code, -32000); | ||
| assert.equal(response.error?.message, "RpcClient is disposed"); | ||
| }); | ||
|
|
||
| test("requests made after the process has already exited reject immediately instead of hanging", async () => { | ||
| const fake = new FakeProcess(); | ||
| const client = new RpcClient("fake", [], process.cwd(), undefined, fake); | ||
|
|
||
| fake.emit("exit", { code: 1, signal: null }); | ||
| const response = await client.request("diagnostics/status"); | ||
|
|
||
| assert.equal(response.error?.code, -32000); | ||
| assert.equal(response.error?.message, "RpcClient is disposed"); | ||
| }); |
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.