Skip to content
This repository was archived by the owner on Aug 11, 2021. It is now read-only.

Commit 2a860e6

Browse files
committed
Merge pull request #12 from ipfs/write-race
update dependencies and patch for racing condition
2 parents 2cf6f1f + 78e754e commit 2a860e6

File tree

4 files changed

+27
-7
lines changed

4 files changed

+27
-7
lines changed

Diff for: .gitignore

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
test/repo-just-for-test*
2-
2+
test/test-repo-for-*
33
# Logs
44
logs
55
*.log
@@ -35,4 +35,4 @@ node_modules
3535
.node_repl_history
3636

3737
lib
38-
dist
38+
dist

Diff for: package.json

+3-3
Original file line numberDiff line numberDiff line change
@@ -42,8 +42,8 @@
4242
"buffer-loader": "0.0.1",
4343
"chai": "^3.5.0",
4444
"fs-blob-store": "^5.2.1",
45-
"idb-plus-blob-store": "^1.0.0",
46-
"ipfs-repo": "^0.6.1",
45+
"idb-plus-blob-store": "^1.1.1",
46+
"ipfs-repo": "^0.6.6",
4747
"lodash": "^4.8.2",
4848
"ncp": "^2.0.0",
4949
"pre-commit": "^1.1.2",
@@ -60,4 +60,4 @@
6060
"Stephen Whitmore <stephen.whitmore@gmail.com>",
6161
"dignifiedquire <dignifiedquire@gmail.com>"
6262
]
63-
}
63+
}

Diff for: src/block-service.js

+21-1
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,32 @@ function BlockService (ipfsRepo, exchange) {
1414
let done = false
1515

1616
ws.write(block.data)
17+
1718
ws.once('error', (err) => {
1819
done = true
1920
callback(err)
2021
})
22+
2123
ws.once('finish', () => {
22-
if (!done) callback()
24+
if (!done) {
25+
// Important to note: Writing to a stream
26+
// isn't an atomic process, because streams can be
27+
// piped, and the finish of one only represents that
28+
// the data was buffered to the next one.
29+
// This is something known and 'accepted' on the
30+
// streams API, however, since we expose a callback
31+
// interface on BlockService and a streams one,
32+
// the users will expect for the callback to be fired
33+
// when the final write was concluded. We add a
34+
// timeout to ensure that.
35+
// TODO: Create an elegant way to understand when
36+
// the block was actually flushed to disk. This
37+
// means changing how the blob-stores and repo are
38+
// implemented.
39+
// One option, is polling till we check it
40+
// is written.
41+
setTimeout(callback, 150)
42+
}
2343
})
2444
ws.end()
2545
}

Diff for: test/block-service-test.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ module.exports = (repo) => {
2929
})
3030

3131
it('store and get a block, with custom extension', (done) => {
32-
const b = new Block('A random data block', 'ext')
32+
const b = new Block('A random data block 2', 'ext')
3333
bs.addBlock(b, (err) => {
3434
expect(err).to.not.exist
3535
bs.getBlock(b.key, 'ext', (err, block) => {

0 commit comments

Comments
 (0)