feat(cli): add per-canister build lock to prevent parallel clobbering#472
Merged
feat(cli): add per-canister build lock to prevent parallel clobbering#472
Conversation
Parallel `mops build` invocations targeting the same canister race on writing .wasm, .did, and .most files, producing corrupted output. Add per-canister file locking via `proper-lockfile` so that concurrent builds of the same canister are serialized while different canisters can still build in parallel. Made-with: Cursor
- Wrap release() in try-catch in finally to prevent masking build errors - Reduce retry count from 120 to 60 to align with 5-minute stale timeout - Update ELOCKED error message to describe timeout, not a live contention - Document intentional redundancy of exitCleanup with signal-exit - Rename test to match what it actually asserts Made-with: Cursor
Kamirus
commented
Apr 1, 2026
| } | ||
|
|
||
| // proper-lockfile registers its own signal-exit handler, but it doesn't reliably | ||
| // fire on process.exit(). This manual handler covers that gap. Double-unlock is |
Collaborator
Author
There was a problem hiding this comment.
We could get rid of this exitCleanup after refactoring the cliError to throw an exception and not process.exit, but it is a huge refactor so I don't know if we want to commit to it : #473
Collaborator
There was a problem hiding this comment.
Ok, we can evaluate it separately after merging this PR. Exit cleanup seems like a reasonable / acceptable way to do this imo.
rvanasa
approved these changes
Apr 1, 2026
| } | ||
|
|
||
| // proper-lockfile registers its own signal-exit handler, but it doesn't reliably | ||
| // fire on process.exit(). This manual handler covers that gap. Double-unlock is |
Collaborator
There was a problem hiding this comment.
Ok, we can evaluate it separately after merging this PR. Exit cleanup seems like a reasonable / acceptable way to do this imo.
This file contains hidden or 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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Problem
Parallel
mops buildinvocations targeting the same canister race on writing.wasm,.did, and.mostoutput files, which can produce corrupted or mixed artifacts.Root cause
The build loop writes to
{outputDir}/{canisterName}.wasm(and.did,.most) with no inter-process coordination. Two concurrent builds of the same canister interleave writes to the same files.Fix
Add per-canister file locking using
proper-lockfile:.{canisterName}.buildlockinside the output directory.finallyblock on the normal path, and via a synchronousprocess.on('exit')handler whencliError()callsprocess.exit()(proper-lockfile's built-in signal-exit cleanup proved unreliable in this scenario).mops build canisterAandmops build canisterBcan still run concurrently.With exponential backoff from 500ms to 5s over 60 retries, the total max wait is roughly 4-5 minutes — intentionally aligned with the stale timeout. The idea: if the lock holder is alive, we wait. If it's dead, the stale detection kicks in at ~5 minutes and we take over. Either way, we don't wait forever.
Test plan
mops build fooviaPromise.all— both succeed