Skip to content

Commit bd846d0

Browse files
authored
fix: catch eexists in moveOperations promise (#206)
1 parent 747320f commit bd846d0

File tree

2 files changed

+21
-13
lines changed

2 files changed

+21
-13
lines changed

lib/content/write.js

+14-11
Original file line numberDiff line numberDiff line change
@@ -168,18 +168,21 @@ async function moveToDestination (tmp, cache, sri, opts) {
168168
moveOperations.set(
169169
destination,
170170
fs.mkdir(destDir, { recursive: true })
171-
.then(() => moveFile(tmp.target, destination, { overwrite: false }))
171+
.then(async () => {
172+
await moveFile(tmp.target, destination, { overwrite: false })
173+
tmp.moved = true
174+
return tmp.moved
175+
})
176+
.catch(err => {
177+
if (!err.message.startsWith('The destination file exists')) {
178+
throw Object.assign(err, { code: 'EEXIST' })
179+
}
180+
}).finally(() => {
181+
moveOperations.delete(destination)
182+
})
183+
172184
)
173-
try {
174-
await moveOperations.get(destination)
175-
tmp.moved = true
176-
} catch (err) {
177-
if (!err.message.startsWith('The destination file exists')) {
178-
throw Object.assign(err, { code: 'EEXIST' })
179-
}
180-
} finally {
181-
moveOperations.delete(destination)
182-
}
185+
return moveOperations.get(destination)
183186
}
184187

185188
function sizeError (expected, found) {

test/put.js

+7-2
Original file line numberDiff line numberDiff line change
@@ -130,16 +130,21 @@ t.test('signals error if error writing to cache', async t => {
130130
t.equal(streamErr.code, 'EBADSIZE', 'got error from stream write')
131131
})
132132

133-
t.test('concurrent puts', async t => {
133+
t.test('concurrent puts with identical content', async t => {
134134
const CACHE = t.testdir()
135135
await Promise.all([
136136
put(CACHE, KEY, CONTENT),
137+
put(CACHE, `${KEY}2`, CONTENT),
137138
put(CACHE, KEY, CONTENT),
139+
put(CACHE, `${KEY}2`, CONTENT),
138140
put(CACHE, KEY, CONTENT),
141+
put(CACHE, `${KEY}2`, CONTENT),
139142
put(CACHE, KEY, CONTENT),
143+
put(CACHE, `${KEY}2`, CONTENT),
140144
put(CACHE, KEY, CONTENT),
145+
put(CACHE, `${KEY}2`, CONTENT),
141146
put(CACHE, KEY, CONTENT),
142-
put(CACHE, KEY, CONTENT),
147+
put(CACHE, `${KEY}2`, CONTENT),
143148
])
144149
const tmpFiles = await fs.readdir(path.join(CACHE, 'tmp'))
145150
t.strictSame(tmpFiles, [], 'Nothing left in tmp')

0 commit comments

Comments
 (0)