Skip to content

Commit b9d2fa2

Browse files
committed
fix(index): ignore index removal races when inserting
1 parent dc6482d commit b9d2fa2

File tree

1 file changed

+19
-13
lines changed

1 file changed

+19
-13
lines changed

Diff for: lib/entry-index.js

+19-13
Original file line numberDiff line numberDiff line change
@@ -30,16 +30,16 @@ module.exports.insert = insert
3030
function insert (cache, key, digest, opts) {
3131
opts = opts || {}
3232
const bucket = bucketPath(cache, key)
33+
const entry = {
34+
key: key,
35+
digest: digest,
36+
hashAlgorithm: opts.hashAlgorithm || 'sha512',
37+
time: +(new Date()),
38+
metadata: opts.metadata
39+
}
3340
return fixOwner.mkdirfix(
3441
path.dirname(bucket), opts.uid, opts.gid
3542
).then(() => {
36-
const entry = {
37-
key: key,
38-
digest: digest,
39-
hashAlgorithm: opts.hashAlgorithm || 'sha512',
40-
time: +(new Date()),
41-
metadata: opts.metadata
42-
}
4343
const stringified = JSON.stringify(entry)
4444
// NOTE - Cleverness ahoy!
4545
//
@@ -50,12 +50,18 @@ function insert (cache, key, digest, opts) {
5050
// Thanks to @isaacs for the whiteboarding session that ended up with this.
5151
return appendFileAsync(
5252
bucket, `\n${hashEntry(stringified)}\t${stringified}`
53-
).then(() => entry)
54-
}).then(entry => (
55-
fixOwner.chownr(bucket, opts.uid, opts.gid).then(() => (
56-
formatEntry(cache, entry)
57-
))
58-
))
53+
)
54+
}).then(
55+
() => fixOwner.chownr(bucket, opts.uid, opts.gid)
56+
).catch({code: 'ENOENT'}, () => {
57+
// There's a class of race conditions that happen when things get deleted
58+
// during fixOwner, or between the two mkdirfix/chownr calls.
59+
//
60+
// It's perfectly fine to just not bother in those cases and lie
61+
// that the index entry was written. Because it's a cache.
62+
}).then(() => {
63+
return formatEntry(cache, entry)
64+
})
5965
}
6066

6167
module.exports.find = find

0 commit comments

Comments
 (0)