Skip to content

Prevent NetworkConnections from leaking#8079

Merged
alcuadrado merged 6 commits intomainfrom
memory-optimization
Mar 24, 2026
Merged

Prevent NetworkConnections from leaking#8079
alcuadrado merged 6 commits intomainfrom
memory-optimization

Conversation

@alcuadrado
Copy link
Copy Markdown
Member

No description provided.

@changeset-bot
Copy link
Copy Markdown

changeset-bot bot commented Mar 23, 2026

🦋 Changeset detected

Latest commit: 5bdee7f

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

@alcuadrado alcuadrado force-pushed the cache-genesis-state branch from 80d70dc to 437adf2 Compare March 23, 2026 17:17
@alcuadrado alcuadrado force-pushed the memory-optimization branch from f02c38e to 3ee3fdd Compare March 23, 2026 17:21
@alcuadrado alcuadrado added no docs needed This PR doesn't require links to documentation no peer bump needed labels Mar 23, 2026
@alcuadrado alcuadrado requested review from Copilot and kanej March 23, 2026 17:23
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 aims to prevent network/provider objects from being retained longer than necessary by cleaning up EventEmitter listeners on close and breaking a potential reference cycle in the EDR subscription callback path.

Changes:

  • Remove all EventEmitter listeners when closing HttpProvider.
  • Use a WeakRef for EdrProvider in the EDR subscriptionCallback to avoid strongly retaining the provider.
  • Remove all EventEmitter listeners when closing EdrProvider.

Reviewed changes

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

File Description
v-next/hardhat/src/internal/builtin-plugins/network-manager/http-provider.ts Clears listeners during shutdown to reduce retention/leaks tied to provider events.
v-next/hardhat/src/internal/builtin-plugins/network-manager/edr/edr-provider.ts Breaks subscription callback strong reference cycle and clears listeners on close.
.changeset/wild-pets-change.md Adds a patch changeset describing the leak-related optimization.
Comments suppressed due to low confidence (1)

v-next/hardhat/src/internal/builtin-plugins/network-manager/http-provider.ts:169

  • close() removes listeners before attempting to close the dispatcher and only sets #dispatcher to undefined after await this.#dispatcher.close(). If dispatcher.close() rejects, the provider remains usable (because #dispatcher is still set) even though close() was called, which can lead to confusing behavior and resource leaks. Consider ensuring the provider is marked closed regardless (e.g., set #dispatcher = undefined in a finally block, or before awaiting close()).
    this.removeAllListeners();
    if (this.#dispatcher !== undefined) {
      // See https://github.com/nodejs/undici/discussions/3522#discussioncomment-10498734
      await this.#dispatcher.close();
      this.#dispatcher = undefined;
    }

Comment on lines 197 to +201
subscriptionCallback: (event: SubscriptionEvent) => {
edrProvider.onSubscriptionEvent(event);
const deferredProvider = edrProviderWeakRef.deref();
if (deferredProvider !== undefined) {
deferredProvider.onSubscriptionEvent(event);
}
Copy link

Copilot AI Mar 23, 2026

Choose a reason for hiding this comment

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

edrProviderWeakRef is assigned only after createProvider(...) resolves, but it’s dereferenced unconditionally in subscriptionCallback. If the callback is invoked before edrProviderWeakRef is assigned (e.g., if the underlying provider emits an event during initialization), this will throw when calling .deref(). Consider making edrProviderWeakRef optional (WeakRef<EdrProvider> | undefined) and guarding before dereferencing.

Copilot uses AI. Check for mistakes.
Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

good point

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

addressed in 166f6e6

Copy link
Copy Markdown
Member

@kanej kanej left a comment

Choose a reason for hiding this comment

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

I have no points to add beyond co-pilot.

I tested this against OpenZeppelin and it passed - I have done no deeper profiling - but I suspect we will be doing multiple rounds of which this is the first.

@alcuadrado alcuadrado force-pushed the cache-genesis-state branch from d50fbff to ff71fb0 Compare March 24, 2026 21:34
Base automatically changed from cache-genesis-state to main March 24, 2026 21:57
@alcuadrado alcuadrado force-pushed the memory-optimization branch from 8f8ca33 to 5bdee7f Compare March 24, 2026 21:59
Copilot AI review requested due to automatic review settings March 24, 2026 21:59
@alcuadrado alcuadrado enabled auto-merge March 24, 2026 21:59
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 5 out of 5 changed files in this pull request and generated no new comments.

@alcuadrado alcuadrado added this pull request to the merge queue Mar 24, 2026
Merged via the queue into main with commit 814ce31 Mar 24, 2026
217 checks passed
@alcuadrado alcuadrado deleted the memory-optimization branch March 24, 2026 22:09
@github-actions github-actions bot mentioned this pull request Mar 24, 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