-
Notifications
You must be signed in to change notification settings - Fork 1.5k
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
Implement robustness improvements in the compiler downloader #6056
Merged
Merged
Changes from all commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
72d8175
feat: add process mutex to the compiler downloader
galargh 5082048
test: port the multi-process-mutex tests
galargh b85b358
test: port the compiler downloader tests
galargh 50e983f
fix: compilation with solcjs on windows
galargh 133ab74
feat: always download native and wasm compilers
galargh ed94118
Merge branch 'v-next' into robust-compiler-downloader
galargh 9904353
chore: replace solc dependency with a thin solidity_compile wrapper
galargh 97ab1f7
chore: throw hardhat errors from synchronization util
galargh 064bdce
chore: do not reimplement the sleep function
galargh be49e78
docs: explain difference between tsx and node loader better
galargh 4e9c17e
chore: plaftorm -> platform
galargh b27f710
test: apply review comments
galargh c48a049
Merge branch 'v-next' into robust-compiler-downloader
galargh a6fab0e
Merge branch 'v-next' into robust-compiler-downloader
galargh 6745a2a
fix: await assert rejects with hardhat error call
galargh File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,126 @@ | ||
// Logic explanation: the fs.writeFile function, when used with the wx+ flag, performs an atomic operation to create a file. | ||
// If multiple processes try to create the same file simultaneously, only one will succeed. | ||
// This logic can be utilized to implement a mutex. | ||
// ATTENTION: in the current implementation, there's still a risk of two processes running simultaneously. | ||
// For example, if processA has locked the mutex and is running, processB will wait. | ||
// During this wait, processB continuously checks the elapsed time since the mutex lock file was created. | ||
// If an excessive amount of time has passed, processB will assume ownership of the mutex to avoid stale locks. | ||
// However, there's a possibility that processB might take ownership because the mutex creation file is outdated, even though processA is still running | ||
|
||
import fs from "node:fs"; | ||
import os from "node:os"; | ||
import path from "node:path"; | ||
|
||
import debug from "debug"; | ||
|
||
import { ensureError } from "./error.js"; | ||
import { FileSystemAccessError } from "./errors/fs.js"; | ||
import { sleep } from "./lang.js"; | ||
|
||
const log = debug("hardhat:util:multi-process-mutex"); | ||
const DEFAULT_MAX_MUTEX_LIFESPAN_IN_MS = 60000; | ||
const MUTEX_LOOP_WAITING_TIME_IN_MS = 100; | ||
|
||
export class MultiProcessMutex { | ||
readonly #mutexFilePath: string; | ||
readonly #mutexLifespanInMs: number; | ||
|
||
constructor(mutexName: string, maxMutexLifespanInMs?: number) { | ||
log(`Creating mutex with name '${mutexName}'`); | ||
|
||
this.#mutexFilePath = path.join(os.tmpdir(), `${mutexName}.txt`); | ||
this.#mutexLifespanInMs = | ||
maxMutexLifespanInMs ?? DEFAULT_MAX_MUTEX_LIFESPAN_IN_MS; | ||
} | ||
|
||
public async use<T>(f: () => Promise<T>): Promise<T> { | ||
log(`Starting mutex process with mutex file '${this.#mutexFilePath}'`); | ||
|
||
while (true) { | ||
if (await this.#tryToAcquireMutex()) { | ||
// Mutex has been acquired | ||
return this.#executeFunctionAndReleaseMutex(f); | ||
} | ||
|
||
// Mutex not acquired | ||
if (this.#isMutexFileTooOld()) { | ||
// If the mutex file is too old, it likely indicates a stale lock, so the file should be removed | ||
log( | ||
`Current mutex file is too old, removing it at path '${this.#mutexFilePath}'`, | ||
); | ||
this.#deleteMutexFile(); | ||
} else { | ||
// wait | ||
await sleep(MUTEX_LOOP_WAITING_TIME_IN_MS / 1000); | ||
} | ||
} | ||
} | ||
|
||
async #tryToAcquireMutex() { | ||
try { | ||
// Create a file only if it does not exist | ||
fs.writeFileSync(this.#mutexFilePath, "", { flag: "wx+" }); | ||
return true; | ||
} catch (e) { | ||
ensureError<NodeJS.ErrnoException>(e); | ||
|
||
if (e.code === "EEXIST") { | ||
// File already exists, so the mutex is already acquired | ||
return false; | ||
} | ||
|
||
throw new FileSystemAccessError(e.message, e); | ||
} | ||
} | ||
|
||
async #executeFunctionAndReleaseMutex<T>(f: () => Promise<T>): Promise<T> { | ||
log(`Mutex acquired at path '${this.#mutexFilePath}'`); | ||
|
||
try { | ||
return await f(); | ||
} finally { | ||
// Release the mutex | ||
log(`Mutex released at path '${this.#mutexFilePath}'`); | ||
this.#deleteMutexFile(); | ||
log(`Mutex released at path '${this.#mutexFilePath}'`); | ||
} | ||
} | ||
|
||
#isMutexFileTooOld(): boolean { | ||
let fileStat; | ||
try { | ||
fileStat = fs.statSync(this.#mutexFilePath); | ||
} catch (e) { | ||
ensureError<NodeJS.ErrnoException>(e); | ||
|
||
if (e.code === "ENOENT") { | ||
// The file might have been deleted by another process while this function was trying to access it. | ||
return false; | ||
} | ||
|
||
throw new FileSystemAccessError(e.message, e); | ||
} | ||
|
||
const now = new Date(); | ||
const fileDate = new Date(fileStat.ctime); | ||
const diff = now.getTime() - fileDate.getTime(); | ||
|
||
return diff > this.#mutexLifespanInMs; | ||
} | ||
|
||
#deleteMutexFile() { | ||
try { | ||
log(`Deleting mutex file at path '${this.#mutexFilePath}'`); | ||
fs.unlinkSync(this.#mutexFilePath); | ||
} catch (e) { | ||
ensureError<NodeJS.ErrnoException>(e); | ||
|
||
if (e.code === "ENOENT") { | ||
// The file might have been deleted by another process while this function was trying to access it. | ||
return; | ||
} | ||
|
||
throw new FileSystemAccessError(e.message, e); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
I have copied the multi-process mutex from the v2.