Skip to content

Commit

Permalink
Refresh cache before running WriteBundleRequest
Browse files Browse the repository at this point in the history
..only if the packaging request didn't run in the main thread.
  • Loading branch information
lettertwo committed Jul 7, 2023
1 parent 206c6c3 commit 7f06109
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 10 deletions.
10 changes: 0 additions & 10 deletions packages/core/core/src/requests/WriteBundleRequest.js
Expand Up @@ -129,16 +129,6 @@ async function run({input, options, api}) {
if (info.isLargeBlob) {
contentStream = options.cache.getStream(cacheKeys.content);
} else {
// 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. Thread A has read some value from cache, necessitating a read transaction.
// 2. Concurrently, Thread B finishes a packaging request.
// 3. Subsequently, Thread A 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();

contentStream = blobToStream(
await options.cache.getBlob(cacheKeys.content),
);
Expand Down
12 changes: 12 additions & 0 deletions packages/core/core/src/requests/WriteBundlesRequest.js
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

0 comments on commit 7f06109

Please sign in to comment.