Skip to content

fix(agent): deduplicate parallel fetchSubnetKeys requests#1277

Closed
yasumorishima wants to merge 3 commits intodfinity:mainfrom
yasumorishima:1179-share-subnet-key-parallel-query-calls
Closed

fix(agent): deduplicate parallel fetchSubnetKeys requests#1277
yasumorishima wants to merge 3 commits intodfinity:mainfrom
yasumorishima:1179-share-subnet-key-parallel-query-calls

Conversation

@yasumorishima
Copy link
Copy Markdown
Contributor

Description

When multiple query calls are issued in parallel for the same canister, each call triggers its own read_state round-trip to fetch subnet keys before the first response can populate the cache.

This PR introduces an inflight deduplication map (#subnetKeysFetching) so that concurrent callers share a single read_state request, resulting in exactly one round-trip per canister regardless of concurrency.

Fixes #1179

Changes

packages/core/src/agent/agent/http/index.ts

  • Add #subnetKeysFetching: Map<string, Promise<SubnetNodeKeys>> field to HttpAgent
  • Extract the actual fetch logic from fetchSubnetKeys into a new private method #doFetchSubnetKeys
  • fetchSubnetKeys now checks the inflight map before issuing a new request; parallel callers reuse the same promise and the map entry is cleaned up via .finally()

e2e/node/basic/fetchSubnetKeys.test.ts

  • Add test: two concurrent fetchSubnetKeys calls for the same canister result in exactly one read_state request and identical node keys

CHANGELOG.md

  • Add entry under [Unreleased]

How Has This Been Tested?

  • All 552 existing unit tests pass (pnpm test)
  • New e2e test added in fetchSubnetKeys (parallel calls deduplication) suite

Checklist:

  • My changes follow the guidelines in CONTRIBUTING.md.
  • The title of this PR complies with Conventional Commits.
  • I have edited the CHANGELOG accordingly.
  • I have made corresponding changes to the documentation.

yasumorishima and others added 3 commits February 12, 2026 18:27
Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
When multiple query calls are issued in parallel for the same canister,
each would trigger its own read_state round-trip to fetch the subnet keys
before the first response could populate the cache.

Introduce a #subnetKeysFetching inflight map in HttpAgent that stores the
in-progress Promise<SubnetNodeKeys> for each canister. Parallel callers
reuse the same promise, resulting in exactly one read_state request per
canister regardless of concurrency.

Closes dfinity#1179
@yasumorishima yasumorishima requested a review from a team as a code owner March 5, 2026 14:13
@yasumorishima
Copy link
Copy Markdown
Contributor Author

Closing to recreate from upstream/main. The previous PR accidentally included fork-only files (.coderabbit.yaml, .github/workflows/codeql.yml) because the feature branch was cut from the fork's main instead of upstream/main. Will reopen with a clean branch shortly.

nikosxenakis pushed a commit that referenced this pull request Mar 10, 2026
# Description

When multiple query calls are issued in parallel for the same canister,
each call triggers its own `read_state` round-trip to fetch subnet keys
before the first response can populate the cache.

This PR introduces an inflight deduplication map (`#subnetKeysFetching`)
so that concurrent `fetchSubnetKeys` calls for the same canister share a
single `read_state` round-trip instead of each issuing their own.

Fixes #1179

## Changes

- `packages/core/src/agent/agent/http/index.ts`: add
`#subnetKeysFetching` map and split `fetchSubnetKeys` into public
wrapper + `#doFetchSubnetKeys`
- `e2e/node/basic/fetchSubnetKeys.test.ts`: add parallel deduplication
test
- `CHANGELOG.md`: add entry

> Note: this is a recreation of #1277, which was closed due to
accidental inclusion of fork-only files.

---------

Co-authored-by: Claude Sonnet 4.6 <noreply@anthropic.com>
Co-authored-by: yasumorishima <yasumplaystore@gmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Share subnet key across parallel query calls

1 participant