Skip to content

refactor: switch to double check on mutex#8102

Merged
kanej merged 1 commit intomainfrom
feat/mutex-for-network-handler-setup
Apr 1, 2026
Merged

refactor: switch to double check on mutex#8102
kanej merged 1 commit intomainfrom
feat/mutex-for-network-handler-setup

Conversation

@kanej
Copy link
Copy Markdown
Member

@kanej kanej commented Apr 1, 2026

In the Network request path we have an expensive initial creation of the request handlers, this creation happens within a mutex.

This PR moves to a double check pattern to avoid moving into the mutex if the handlers have already been set.

Copilot AI review requested due to automatic review settings April 1, 2026 08:33
@changeset-bot
Copy link
Copy Markdown

changeset-bot Bot commented Apr 1, 2026

🦋 Changeset detected

Latest commit: d5f8394

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 1 package
Name Type
hardhat Patch

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

@kanej kanej force-pushed the feat/mutex-for-network-handler-setup branch from 9d19e54 to 0b74323 Compare April 1, 2026 08:35
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR optimizes the network hook handler initialization path by avoiding mutex acquisition when request handlers are already cached for a connection, reducing overhead on steady-state request processing.

Changes:

  • Refactor onRequest to use a double-checked lookup around AsyncMutex.exclusiveRun.
  • Add a new test suite covering handler initialization, reuse, per-connection separation, and closeConnection behavior.
  • Add a changeset documenting the performance improvement.

Reviewed changes

Copilot reviewed 3 out of 3 changed files in this pull request and generated 1 comment.

File Description
packages/hardhat/src/internal/builtin-plugins/network-manager/hook-handlers/network.ts Implements double-checked locking to skip the mutex when handlers are already cached.
packages/hardhat/test/internal/builtin-plugins/network-manager/hook-handlers/network.ts Adds tests for initialization/caching behavior (including a concurrent call test).
.changeset/forty-groups-bake.md Records the patch-level performance improvement in release notes.

@kanej kanej added no docs needed This PR doesn't require links to documentation no peer bump needed labels Apr 1, 2026
@kanej kanej force-pushed the feat/mutex-for-network-handler-setup branch 2 times, most recently from cf2aec6 to 41a2c5c Compare April 1, 2026 09:56
Copilot AI review requested due to automatic review settings April 1, 2026 09:56
@kanej kanej force-pushed the feat/mutex-for-network-handler-setup branch from 41a2c5c to 9f0fadc Compare April 1, 2026 09:57
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 3 out of 3 changed files in this pull request and generated 1 comment.

Avoid moving into the Network handler creation mutex, if the handlers
already exist.
@kanej kanej force-pushed the feat/mutex-for-network-handler-setup branch from 9f0fadc to d5f8394 Compare April 1, 2026 10:12
@kanej kanej marked this pull request as ready for review April 1, 2026 10:18
@kanej kanej requested review from alcuadrado and Copilot April 1, 2026 10:18
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 3 out of 3 changed files in this pull request and generated 1 comment.

@kanej kanej added this pull request to the merge queue Apr 1, 2026
Merged via the queue into main with commit 79c602e Apr 1, 2026
424 checks passed
@kanej kanej deleted the feat/mutex-for-network-handler-setup branch April 1, 2026 15:46
@github-actions github-actions Bot mentioned this pull request Mar 31, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

no docs needed This PR doesn't require links to documentation no peer bump needed

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants