Skip to content

Introduce new inter-process mutex implementation#7942

Closed
alcuadrado wants to merge 6 commits intomainfrom
new-lock-strategy
Closed

Introduce new inter-process mutex implementation#7942
alcuadrado wants to merge 6 commits intomainfrom
new-lock-strategy

Conversation

@alcuadrado
Copy link
Copy Markdown
Member

@alcuadrado alcuadrado commented Feb 11, 2026

This PR introduces a new implementation of the MultiProcessMutex, which inverts the behavior when compared to the previous one.

Instead of considering a lock staled based on the time that the process has been waiting for it, it does it when the process that acquired it no longer exists. This can be really quick, without having to wait much. It's also less error prone.

Please read the jsdoc for more info.

This PR also fixes three related bugs:

  1. The download function in the request module was always using the same tmp path for every file with the same name, leading to more race-y code. This was only triggered because the locks weren't working (see 3)
  2. The download function didn't properly wait for the file to be closed before moving it.
  3. The compiler downloader could use different paths (based on new tmp dirs each time) for the lock in each process. Leading to them potentially not working.

@changeset-bot
Copy link
Copy Markdown

changeset-bot Bot commented Feb 11, 2026

🦋 Changeset detected

Latest commit: 9d8bbc1

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

This PR includes changesets to release 21 packages
Name Type
@nomicfoundation/hardhat-utils Major
hardhat Patch
@nomicfoundation/hardhat-errors Patch
@nomicfoundation/hardhat-ethers-chai-matchers Patch
@nomicfoundation/hardhat-ethers Patch
@nomicfoundation/ignition-core Patch
@nomicfoundation/hardhat-ignition Patch
@nomicfoundation/hardhat-keystore Patch
@nomicfoundation/hardhat-ledger Patch
@nomicfoundation/hardhat-mocha Patch
@nomicfoundation/hardhat-network-helpers Patch
@nomicfoundation/hardhat-node-test-runner Patch
@nomicfoundation/hardhat-test-utils Patch
@nomicfoundation/hardhat-typechain Patch
@nomicfoundation/hardhat-verify Patch
@nomicfoundation/hardhat-viem-assertions Patch
@nomicfoundation/hardhat-viem Patch
@nomicfoundation/hardhat-zod-utils Patch
@nomicfoundation/hardhat-ignition-ethers Patch
@nomicfoundation/hardhat-ignition-viem Patch
@nomicfoundation/ignition-ui 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

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 replaces the time-based stale lock detection mechanism in MultiProcessMutex with a PID-liveness-based approach. Instead of declaring locks stale after a timeout, the new implementation checks if the lock-owning process is still alive using process.kill(pid, 0). This change eliminates the risk of two processes running simultaneously in critical sections when long-running tasks exceed the previous timeout threshold.

Changes:

  • Replaced file-based locking (fs.writeFileSync with wx+ flag) with directory-based locking (fs.mkdirSync with recursive: false)
  • Implemented JSON metadata storage inside lock directories containing PID, hostname, platform, uid, and creation timestamp
  • Added comprehensive error classes for incompatible locks (different hostname/platform/uid) and stale lock removal failures

Reviewed changes

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

Show a summary per file
File Description
v-next/hardhat-utils/src/synchronization.ts Core implementation rewrite: directory-based locks with PID liveness checks, metadata storage, exit/signal handlers, and exponential backoff polling
v-next/hardhat-utils/src/errors/synchronization.ts New error classes for mutex failures including timeout, stale locks, and incompatibility scenarios
v-next/hardhat-utils/test/synchronization.ts Expanded test suite covering stale lock recovery, cross-process scenarios, error conditions, metadata handling, and exit/signal cleanup
v-next/hardhat-utils/test/fixture-projects/lock/lock-holder.ts Test fixture for spawning child processes that acquire and hold locks
v-next/hardhat-utils/test/fixture-projects/lock/lock-and-exit.ts Test fixture for verifying lock cleanup on process exit
v-next/hardhat-utils/test/helpers/synchronization.ts Removed obsolete helper file

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread v-next/hardhat-utils/src/synchronization.ts Outdated
Comment thread v-next/hardhat-utils/src/synchronization.ts Outdated
Comment thread v-next/hardhat-utils/test/synchronization.ts Outdated
@alcuadrado alcuadrado requested a review from Copilot February 11, 2026 19:09
@alcuadrado alcuadrado added no docs needed This PR doesn't require links to documentation no peer bump needed labels Feb 11, 2026
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 7 out of 7 changed files in this pull request and generated 5 comments.


💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread v-next/hardhat-utils/src/synchronization.ts Outdated
Comment thread v-next/hardhat-utils/src/synchronization.ts
Comment thread v-next/hardhat-utils/src/synchronization.ts Outdated
Comment thread v-next/hardhat-utils/test/synchronization.ts Outdated
Comment thread v-next/hardhat-utils/src/synchronization.ts Outdated
@alcuadrado alcuadrado requested a review from Copilot February 11, 2026 19:16
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 7 out of 7 changed files in this pull request and generated 5 comments.


💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread v-next/hardhat-utils/src/synchronization.ts Outdated
Comment thread v-next/hardhat-utils/src/synchronization.ts Outdated
Comment thread v-next/hardhat-utils/src/synchronization.ts
Comment thread v-next/hardhat-utils/test/synchronization.ts
Comment thread v-next/hardhat-utils/src/synchronization.ts Outdated
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 7 out of 7 changed files in this pull request and generated 1 comment.


💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread v-next/hardhat-utils/src/errors/synchronization.ts Outdated
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 8 out of 8 changed files in this pull request and generated 2 comments.


💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread v-next/hardhat-utils/test/synchronization.ts Outdated
Comment thread v-next/hardhat-utils/src/synchronization.ts Outdated
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 8 out of 8 changed files in this pull request and generated 2 comments.


💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread v-next/hardhat-utils/test/synchronization.ts Outdated
Comment thread v-next/hardhat-utils/test/synchronization.ts Outdated
@alcuadrado
Copy link
Copy Markdown
Member Author

@ChristopherDedominici this is ready to be reviewed now

@alcuadrado alcuadrado force-pushed the new-lock-strategy branch 3 times, most recently from b1491ef to 38435c7 Compare February 17, 2026 23:35
Comment thread .changeset/chilled-ads-exist.md Outdated
Comment thread v-next/hardhat-utils/src/synchronization.ts
Comment thread v-next/hardhat-utils/src/synchronization.ts
Comment thread v-next/hardhat-utils/src/synchronization.ts Outdated
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 14 out of 14 changed files in this pull request and generated 3 comments.


💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread v-next/hardhat-utils/src/synchronization.ts Outdated
Comment thread v-next/hardhat-utils/test/synchronization.ts Outdated
Comment thread v-next/hardhat-utils/src/synchronization.ts
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 14 out of 14 changed files in this pull request and generated 4 comments.


💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread v-next/hardhat-utils/src/synchronization.ts
Comment thread v-next/hardhat-utils/src/synchronization.ts
Comment thread v-next/hardhat-utils/src/synchronization.ts Outdated
Comment thread v-next/hardhat-utils/src/synchronization.ts Outdated
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 14 out of 14 changed files in this pull request and generated 1 comment.


💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread v-next/hardhat-utils/src/synchronization.ts Outdated
@alcuadrado
Copy link
Copy Markdown
Member Author

alcuadrado commented Feb 19, 2026

Let's go for the lockfile version instead, because it's simpler to reason about, and the chances of people using hardhat in a network filesystem (AKA NFS) are really low.

@alcuadrado alcuadrado closed this Feb 19, 2026
@github-actions github-actions Bot mentioned this pull request Feb 23, 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