Skip to content

Commit 970e081

Browse files
fix(blockstore): lock getStream to avoid race issues
1 parent 477d6e5 commit 970e081

File tree

3 files changed

+19
-16
lines changed

3 files changed

+19
-16
lines changed

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,8 +47,8 @@
4747
"ipfs-block": "^0.3.0",
4848
"lock": "^0.1.3",
4949
"multihashes": "^0.2.2",
50+
"pull-defer": "^0.2.2",
5051
"pull-stream": "^3.4.5",
51-
"pull-through": "^1.0.18",
5252
"pull-write": "^1.1.0",
5353
"run-parallel": "^1.1.6",
5454
"run-series": "^1.1.4",
@@ -66,4 +66,4 @@
6666
"greenkeeperio-bot <[email protected]>",
6767
"nginnever <[email protected]>"
6868
]
69-
}
69+
}

src/stores/blockstore.js

Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ const base32 = require('base32.js')
77
const path = require('path')
88
const write = require('pull-write')
99
const parallel = require('run-parallel')
10-
const through = require('pull-through')
10+
const defer = require('pull-defer/source')
1111

1212
const PREFIX_LENGTH = 5
1313

@@ -52,19 +52,22 @@ exports.setUp = (basePath, BlobStore, locks) => {
5252
}
5353

5454
const p = multihashToPath(key, extension)
55+
const deferred = defer()
56+
57+
lock(p, (release) => {
58+
const ext = extension === 'data' ? 'protobuf' : extension
59+
pull(
60+
store.read(p),
61+
pull.collect(release((err, data) => {
62+
if (err) return deferred.abort(err)
63+
deferred.resolve(pull.values([
64+
new Block(Buffer.concat(data), ext)
65+
]))
66+
}))
67+
)
68+
})
5569

56-
const ext = extension === 'data' ? 'protobuf' : extension
57-
let data = []
58-
59-
return pull(
60-
store.read(p),
61-
through(function (values) {
62-
data = data.concat(values)
63-
}, function () {
64-
this.queue(new Block(Buffer.concat(data), ext))
65-
this.queue(null)
66-
})
67-
)
70+
return deferred
6871
},
6972

7073
putStream () {

test/blockstore-test.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ module.exports = (repo) => {
109109

110110
it('massive read', (done) => {
111111
parallel(_.range(20 * 100).map((i) => (cb) => {
112-
const j = i % 20
112+
const j = i % blockCollection.length
113113
pull(
114114
repo.blockstore.getStream(blockCollection[j].key),
115115
pull.collect((err, meta) => {

0 commit comments

Comments
 (0)