Skip to content

Commit 96b1f95

Browse files
authored
Merge pull request #85 from ipfs/lock
fix(blockstore: lock getStream to avoid race issues
2 parents 477d6e5 + d12086e commit 96b1f95

File tree

3 files changed

+29
-18
lines changed

3 files changed

+29
-18
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: 26 additions & 15 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,25 @@ 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) {
63+
return deferred.abort(err)
64+
}
65+
66+
deferred.resolve(pull.values([
67+
new Block(Buffer.concat(data), ext)
68+
]))
69+
}))
70+
)
71+
})
5572

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-
)
73+
return deferred
6874
},
6975

7076
putStream () {
@@ -75,7 +81,10 @@ exports.setUp = (basePath, BlobStore, locks) => {
7581
const sink = write((blocks, cb) => {
7682
parallel(blocks.map((block) => (cb) => {
7783
writeBlock(block, (err, meta) => {
78-
if (err) return cb(err)
84+
if (err) {
85+
return cb(err)
86+
}
87+
7988
if (push) {
8089
const read = push
8190
push = null
@@ -94,7 +103,9 @@ exports.setUp = (basePath, BlobStore, locks) => {
94103

95104
const source = (end, cb) => {
96105
if (end) ended = end
97-
if (ended) return cb(ended)
106+
if (ended) {
107+
return cb(ended)
108+
}
98109

99110
if (written.length) {
100111
return cb(null, written.shift())

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)