Skip to content
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

Refresh cache before writing contents to bundle #9123

Merged
merged 5 commits into from
Jul 19, 2023
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 4 additions & 0 deletions packages/core/cache/src/FSCache.js
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,10 @@ export class FSCache implements Cache {
logger.error(err, '@parcel/cache');
}
}

refresh(): void {
// NOOP
}
}

registerSerializableClass(`${packageJson.version}:FSCache`, FSCache);
4 changes: 4 additions & 0 deletions packages/core/cache/src/IDBCache.browser.js
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,10 @@ export class IDBCache implements Cache {
setLargeBlob(key: string, contents: Buffer | string): Promise<void> {
return this.setBlob(key, contents);
}

refresh(): void {
// NOOP
}
}

registerSerializableClass(`${packageJson.version}:IDBCache`, IDBCache);
7 changes: 7 additions & 0 deletions packages/core/cache/src/LMDBCache.js
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,13 @@ export class LMDBCache implements Cache {
async setLargeBlob(key: string, contents: Buffer | string): Promise<void> {
await this.fs.writeFile(path.join(this.dir, key), contents);
}
refresh(): void {
// Reset the read transaction for the store. This guarantees that
// the next read will see the latest changes to the store.
// Useful in scenarios where reads and writes are multi-threaded.
// See https://github.com/kriszyp/lmdb-js#resetreadtxn-void
this.store.resetReadTxn();
}
}

registerSerializableClass(`${packageJson.version}:LMDBCache`, LMDBCache);
1 change: 1 addition & 0 deletions packages/core/cache/src/types.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,5 @@ export interface Cache {
getLargeBlob(key: string): Promise<Buffer>;
setLargeBlob(key: string, contents: Buffer | string): Promise<void>;
getBuffer(key: string): Promise<?Buffer>;
refresh(): void;
}
12 changes: 12 additions & 0 deletions packages/core/core/src/requests/WriteBundlesRequest.js
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,18 @@ async function run({input, api, farm, options}) {

let info = await api.runRequest(request);

if (!useMainThread) {
// Force a refresh of the cache to avoid a race condition
// between threaded reads and writes that can result in an LMDB cache miss:
// 1. The main thread has read some value from cache, necessitating a read transaction.
// 2. Concurrently, Thread A finishes a packaging request.
// 3. Subsequently, the main thread is tasked with this request, but fails because the read transaction is stale.
// This only occurs if the reading thread has a transaction that was created before the writing thread committed,
// and the transaction is still live when the reading thread attempts to get the written value.
// See https://github.com/parcel-bundler/parcel/issues/9121
options.cache.refresh();
}

bundleInfoMap[bundle.id] = info;
if (!info.hashReferences.length) {
hashRefToNameHash.set(
Expand Down