Skip to content

Commit 87629aa

Browse files
committed
fixed race condition getting an old file version to read
1 parent bae971c commit 87629aa

File tree

2 files changed

+26
-7
lines changed

2 files changed

+26
-7
lines changed

index.js

Lines changed: 25 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -131,8 +131,7 @@ WriteRequest.prototype.makeWriter = function () {
131131
self.writer = writer
132132

133133
writer.onwriteend = function (e) {
134-
self.file.updateSize(e.currentTarget.length)
135-
self.onwrite(null)
134+
self.onwrite(null, e)
136135
}
137136

138137
writer.onerror = function (err) {
@@ -143,7 +142,7 @@ WriteRequest.prototype.makeWriter = function () {
143142
})
144143
}
145144

146-
WriteRequest.prototype.onwrite = function (err) {
145+
WriteRequest.prototype.onwrite = function (err, e) {
147146
const req = this.req
148147
this.req = null
149148

@@ -152,6 +151,10 @@ WriteRequest.prototype.onwrite = function (err) {
152151
this.mutex.release()
153152
}
154153

154+
if (!err) {
155+
this.file.updateSize(e.currentTarget.length, this.truncating)
156+
}
157+
155158
if (this.truncating) {
156159
this.truncating = false
157160
if (!err) return this.run(req)
@@ -244,7 +247,16 @@ ReadRequest.prototype.onread = function (err, buf) {
244247

245248
if (err && this.retry) {
246249
this.retry = false
247-
if (this.lock(this)) this.run(req)
250+
if (this.lock(this)) {
251+
this.file.clearFile()
252+
this.run(req)
253+
}
254+
return
255+
}
256+
257+
if (err && err.name === 'NotReadableError') {
258+
this.file.clearFile()
259+
this.run(req)
248260
return
249261
}
250262

@@ -287,8 +299,15 @@ class EntryFile {
287299
return this._size
288300
}
289301

290-
updateSize (size) {
291-
this._size = size
302+
updateSize (size, truncating = false) {
303+
if (truncating || size > this._size) {
304+
this._size = size
305+
}
306+
307+
this.clearFile()
308+
}
309+
310+
clearFile () {
292311
this._file = null
293312
}
294313

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
"devDependencies": {
1111
"budo": "^11.6.3",
1212
"puppeteer": "^3.0.2",
13-
"random-access-test": "github:random-access-storage/random-access-test",
13+
"random-access-test": "^1.0.0",
1414
"standard": "^11.0.1",
1515
"tap-finished": "0.0.1",
1616
"tape": "^5.0.0"

0 commit comments

Comments
 (0)