Repository navigation
Fix cancellation and provider limits in SDK directory clients #942
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
base: main
Are you sure you want to change the base?
Changes from all commits
0915b82
20ed380
aaa0db1
290e63d
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,31 @@ | ||
| // Copyright 2026 Google LLC | ||
| // | ||
| // Licensed under the Apache License, Version 2.0 (the "License"); | ||
| // you may not use this file except in compliance with the License. | ||
| // You may obtain a copy of the License at | ||
| // | ||
| // http://www.apache.org/licenses/LICENSE-2.0 | ||
| // | ||
| // Unless required by applicable law or agreed to in writing, software | ||
| // distributed under the License is distributed on an "AS IS" BASIS, | ||
| // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| // See the License for the specific language governing permissions and | ||
| // limitations under the License. | ||
|
|
||
| import assert from "node:assert/strict"; | ||
| import { test } from "node:test"; | ||
| import { findProviders } from "./discovery.ts"; | ||
|
|
||
| test("directory validates the provider limit before contacting routers", async () => { | ||
| const key = new Uint8Array(); | ||
| assert.deepEqual(await findProviders([], key, 0), []); | ||
| await assert.rejects(findProviders([], key, -1), RangeError); | ||
| await assert.rejects(findProviders([], key, 0.5), RangeError); | ||
| await assert.rejects(findProviders([], key, 0xffffffff + 1), RangeError); | ||
| await assert.rejects(findProviders([], key, Number.NaN), RangeError); | ||
| }); | ||
|
|
||
| test("directory rejects an already aborted provider lookup", async () => { | ||
| const reason = new Error("caller gave up"); | ||
| await assert.rejects(findProviders([], new Uint8Array(), 0, AbortSignal.abort(reason)), reason); | ||
| }); | ||
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -114,6 +114,7 @@ export async function request(routers: DirectoryRouter[], req: DirectoryRequest, | |||||||||||||||||||||||||||||||||||||
| let asked = 0; | ||||||||||||||||||||||||||||||||||||||
| let empty: DirectoryResponse | undefined; | ||||||||||||||||||||||||||||||||||||||
| for (const router of routers) { | ||||||||||||||||||||||||||||||||||||||
| signal?.throwIfAborted(); | ||||||||||||||||||||||||||||||||||||||
| const conn = router.connection(); | ||||||||||||||||||||||||||||||||||||||
| if (conn === undefined) { | ||||||||||||||||||||||||||||||||||||||
| continue; | ||||||||||||||||||||||||||||||||||||||
|
|
@@ -122,18 +123,18 @@ export async function request(routers: DirectoryRouter[], req: DirectoryRequest, | |||||||||||||||||||||||||||||||||||||
| const bounded = signal === undefined ? AbortSignal.timeout(REQUEST_TIMEOUT_MS) : AbortSignal.any([signal, AbortSignal.timeout(REQUEST_TIMEOUT_MS)]); | ||||||||||||||||||||||||||||||||||||||
| try { | ||||||||||||||||||||||||||||||||||||||
| const resp = await ask(conn, req, bounded); | ||||||||||||||||||||||||||||||||||||||
| signal?.throwIfAborted(); | ||||||||||||||||||||||||||||||||||||||
| if (lookup && resp.providers.length === 0 && resp.peer === undefined) { | ||||||||||||||||||||||||||||||||||||||
| empty = resp; | ||||||||||||||||||||||||||||||||||||||
| continue; | ||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||
| return resp; | ||||||||||||||||||||||||||||||||||||||
| } catch (err) { | ||||||||||||||||||||||||||||||||||||||
| failures.push(`${router.peerId}: ${err instanceof Error ? err.message : String(err)}`); | ||||||||||||||||||||||||||||||||||||||
| if (signal?.aborted === true) { | ||||||||||||||||||||||||||||||||||||||
| break; | ||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||
| signal?.throwIfAborted(); | ||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||
| signal?.throwIfAborted(); | ||||||||||||||||||||||||||||||||||||||
| if (empty !== undefined) { | ||||||||||||||||||||||||||||||||||||||
| return empty; | ||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||
|
|
@@ -162,7 +163,12 @@ export async function withdraw(routers: DirectoryRouter[], key: Uint8Array, sign | |||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||
| /** Who provides key, up to limit, with their addresses; empty when nobody does. */ | ||||||||||||||||||||||||||||||||||||||
| export async function findProviders(routers: DirectoryRouter[], key: Uint8Array, limit = DEFAULT_LIMIT, signal?: AbortSignal): Promise<PeerRecord[]> { | ||||||||||||||||||||||||||||||||||||||
| const resp = await request(routers, create(DirectoryRequestSchema, { request: { case: "findProviders", value: { key, limit: Math.max(limit, 1) } } }), signal); | ||||||||||||||||||||||||||||||||||||||
| signal?.throwIfAborted(); | ||||||||||||||||||||||||||||||||||||||
| if (!Number.isInteger(limit) || limit < 0 || limit > 0xffffffff) { | ||||||||||||||||||||||||||||||||||||||
| throw new RangeError("directory provider limit must be an unsigned 32-bit integer"); | ||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||
| if (limit === 0) return []; | ||||||||||||||||||||||||||||||||||||||
| const resp = await request(routers, create(DirectoryRequestSchema, { request: { case: "findProviders", value: { key, limit } } }), signal); | ||||||||||||||||||||||||||||||||||||||
| return resp.providers.slice(0, limit); | ||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||
|
Comment on lines
165
to
173
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. General Correctness & RobustnessIf
Suggested change
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Good catch, taken in 290e63d: |
||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -0,0 +1,28 @@ | ||||||||||||||||||||||||||||||||||||||
| # Copyright 2026 Google LLC | ||||||||||||||||||||||||||||||||||||||
| # | ||||||||||||||||||||||||||||||||||||||
| # Licensed under the Apache License, Version 2.0 (the "License"); | ||||||||||||||||||||||||||||||||||||||
| # you may not use this file except in compliance with the License. | ||||||||||||||||||||||||||||||||||||||
| # You may obtain a copy of the License at | ||||||||||||||||||||||||||||||||||||||
| # | ||||||||||||||||||||||||||||||||||||||
| # http://www.apache.org/licenses/LICENSE-2.0 | ||||||||||||||||||||||||||||||||||||||
| # | ||||||||||||||||||||||||||||||||||||||
| # Unless required by applicable law or agreed to in writing, software | ||||||||||||||||||||||||||||||||||||||
| # distributed under the License is distributed on an "AS IS" BASIS, | ||||||||||||||||||||||||||||||||||||||
| # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||||||||||||||||||||||||||||||||||||||
| # See the License for the specific language governing permissions and | ||||||||||||||||||||||||||||||||||||||
| # limitations under the License. | ||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||
| import pytest | ||||||||||||||||||||||||||||||||||||||
| import trio | ||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||
| from agent_mesh.discovery import find_providers | ||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||
| def test_directory_checks_provider_limit_before_contacting_routers(): | ||||||||||||||||||||||||||||||||||||||
| async def run(): | ||||||||||||||||||||||||||||||||||||||
| assert await find_providers(None, [], b"", limit=0) == [] | ||||||||||||||||||||||||||||||||||||||
| for limit in (-1, 0.5, 0xFFFFFFFF + 1): | ||||||||||||||||||||||||||||||||||||||
| with pytest.raises(ValueError, match="limit"): | ||||||||||||||||||||||||||||||||||||||
| await find_providers(None, [], b"", limit=limit) | ||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||
| trio.run(run) | ||||||||||||||||||||||||||||||||||||||
|
Comment on lines
+21
to
+28
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Section 3: Test coverage must follow the pyramidTo ensure the Python test suite is as comprehensive as the JavaScript one, consider adding test cases for non-integer limits (like
Suggested change
References
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Added in 290e63d: the Python test now rejects |
||||||||||||||||||||||||||||||||||||||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Section 3: Test coverage must follow the pyramid
Consider adding a test case for limits exceeding the 32-bit unsigned integer range (e.g.,
0xffffffff + 1) to fully cover the validation logic and ensure parity with the implementation.References
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Added in 290e63d: the test now also rejects
0xffffffff + 1andNaN.