Skip to content

Commit

Permalink
Always refresh cache when running WriteBundleRequest
Browse files Browse the repository at this point in the history
  • Loading branch information
lettertwo committed Jul 7, 2023
1 parent 206c6c3 commit b50035b
Showing 1 changed file with 11 additions and 10 deletions.
21 changes: 11 additions & 10 deletions packages/core/core/src/requests/WriteBundleRequest.js
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,17 @@ async function run({input, options, api}) {

let cacheKeys = info.cacheKeys;
let mapKey = cacheKeys.map;

// 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();

let fullPath = fromProjectPath(options.projectRoot, filePath);
if (mapKey && bundle.env.sourceMap && !bundle.env.sourceMap.inline) {
api.invalidateOnFileDelete(
Expand All @@ -129,16 +140,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

0 comments on commit b50035b

Please sign in to comment.