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

fix(gatsby): fix race condition in cache lock #28097

Merged
merged 6 commits into from
Nov 17, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 3 additions & 5 deletions packages/gatsby/src/cache/cache-fs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -228,11 +228,7 @@ DiskStore.prototype.reset = wrapCallback(async function (): Promise<void> {
* @private
*/
DiskStore.prototype._lock = function _lock(filePath): Promise<void> {
return new Promise((resolve, reject) =>
innerLock(resolve, reject, filePath)
).then(() => {
globalGatsbyCacheLock.set(filePath, Date.now())
})
return new Promise((resolve, reject) => innerLock(resolve, reject, filePath))
}

function innerLock(resolve, reject, filePath): void {
Expand All @@ -251,6 +247,8 @@ function innerLock(resolve, reject, filePath): void {
innerLock(resolve, reject, filePath)
}, 50)
} else {
// set sync
globalGatsbyCacheLock.set(filePath, Date.now())
resolve()
}
} catch (e) {
Expand Down
60 changes: 34 additions & 26 deletions packages/gatsby/src/cache/json-file-store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -104,32 +104,40 @@ exports.read = async function (path, options): Promise<string> {
}

const externalBuffers = []
const data = JSON.parse(dataString, function bufferReceiver(k, value) {
if (value && value.type === `Buffer` && value.data) {
return Buffer.from(value.data)
} else if (
value &&
value.type === `ExternalBuffer` &&
typeof value.index === `number` &&
typeof value.size === `number`
) {
//JSON.parse is sync so we need to return a buffer sync, we will fill the buffer later
const buffer = Buffer.alloc(value.size)
externalBuffers.push({
index: +value.index,
buffer: buffer,
})
return buffer
} else if (
value &&
value.type === `Infinity` &&
typeof value.sign === `number`
) {
return Infinity * value.sign
} else {
return value
}
})
let data
try {
data = JSON.parse(dataString, function bufferReceiver(k, value) {
if (value && value.type === `Buffer` && value.data) {
return Buffer.from(value.data)
} else if (
value &&
value.type === `ExternalBuffer` &&
typeof value.index === `number` &&
typeof value.size === `number`
) {
//JSON.parse is sync so we need to return a buffer sync, we will fill the buffer later
const buffer = Buffer.alloc(value.size)
externalBuffers.push({
index: +value.index,
buffer: buffer,
})
return buffer
} else if (
value &&
value.type === `Infinity` &&
typeof value.sign === `number`
) {
return Infinity * value.sign
} else {
return value
}
})
} catch (e) {
throw new Error(
"json-file-store failed to JSON.parse this string: `" +
dataString.replace(/\n/g, `⏎`)
)
}

//read external buffers
await Promise.all(
Expand Down