Skip to content

Commit

Permalink
fix(gatsby-transformer-sharp): prevent duplicate copy of the same file (
Browse files Browse the repository at this point in the history
#20620)

a race condition existed between existsSync() and .copy() for the same file causing the underlying fs-extra to randomly fail on unlink / chmod syscalls
  • Loading branch information
denisbabineau authored and pieh committed Jan 16, 2020
1 parent 541fdc2 commit 93f0645
Showing 1 changed file with 15 additions and 1 deletion.
16 changes: 15 additions & 1 deletion packages/gatsby-transformer-sharp/src/customize-schema.js
Original file line number Diff line number Diff line change
Expand Up @@ -375,6 +375,12 @@ const fluidNodeType = ({
}
}

/**
* Keeps track of asynchronous file copy to prevent sequence errors in the
* underlying fs-extra module during parallel copies of the same file
*/
const inProgressCopy = new Set()

const createFields = ({
pathPrefix,
getNodeAndSavePathDependency,
Expand Down Expand Up @@ -428,8 +434,16 @@ const createFields = ({
imageName
)

if (!fsExtra.existsSync(publicPath)) {
if (
!fsExtra.existsSync(publicPath) &&
!inProgressCopy.has(publicPath)
) {
// keep track of in progress copy, we should rely on `existsSync` but
// a race condition exists between the exists check and the copy
inProgressCopy.add(publicPath)
fsExtra.copy(details.absolutePath, publicPath, err => {
// this is no longer in progress
inProgressCopy.delete(publicPath)
if (err) {
console.error(
`error copying file from ${details.absolutePath} to ${publicPath}`,
Expand Down

0 comments on commit 93f0645

Please sign in to comment.