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: catch eexists in moveOperations promise #206

Merged
merged 1 commit into from
May 16, 2023
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
25 changes: 14 additions & 11 deletions lib/content/write.js
Original file line number Diff line number Diff line change
Expand Up @@ -168,18 +168,21 @@ async function moveToDestination (tmp, cache, sri, opts) {
moveOperations.set(
destination,
fs.mkdir(destDir, { recursive: true })
.then(() => moveFile(tmp.target, destination, { overwrite: false }))
.then(async () => {
await moveFile(tmp.target, destination, { overwrite: false })
tmp.moved = true
return tmp.moved
})
.catch(err => {
if (!err.message.startsWith('The destination file exists')) {
throw Object.assign(err, { code: 'EEXIST' })
}
}).finally(() => {
moveOperations.delete(destination)
})

)
try {
await moveOperations.get(destination)
tmp.moved = true
} catch (err) {
if (!err.message.startsWith('The destination file exists')) {
throw Object.assign(err, { code: 'EEXIST' })
}
} finally {
moveOperations.delete(destination)
}
return moveOperations.get(destination)
}

function sizeError (expected, found) {
Expand Down
9 changes: 7 additions & 2 deletions test/put.js
Original file line number Diff line number Diff line change
Expand Up @@ -130,16 +130,21 @@ t.test('signals error if error writing to cache', async t => {
t.equal(streamErr.code, 'EBADSIZE', 'got error from stream write')
})

t.test('concurrent puts', async t => {
t.test('concurrent puts with identical content', async t => {
const CACHE = t.testdir()
await Promise.all([
put(CACHE, KEY, CONTENT),
put(CACHE, `${KEY}2`, CONTENT),
put(CACHE, KEY, CONTENT),
put(CACHE, `${KEY}2`, CONTENT),
put(CACHE, KEY, CONTENT),
put(CACHE, `${KEY}2`, CONTENT),
put(CACHE, KEY, CONTENT),
put(CACHE, `${KEY}2`, CONTENT),
put(CACHE, KEY, CONTENT),
put(CACHE, `${KEY}2`, CONTENT),
put(CACHE, KEY, CONTENT),
put(CACHE, KEY, CONTENT),
put(CACHE, `${KEY}2`, CONTENT),
])
const tmpFiles = await fs.readdir(path.join(CACHE, 'tmp'))
t.strictSame(tmpFiles, [], 'Nothing left in tmp')
Expand Down