Skip to content

Commit

Permalink
fixed race condition getting an old file version to read
Browse files Browse the repository at this point in the history
  • Loading branch information
tinchoz49 committed May 4, 2020
1 parent bae971c commit 87629aa
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 7 deletions.
31 changes: 25 additions & 6 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -131,8 +131,7 @@ WriteRequest.prototype.makeWriter = function () {
self.writer = writer

writer.onwriteend = function (e) {
self.file.updateSize(e.currentTarget.length)
self.onwrite(null)
self.onwrite(null, e)
}

writer.onerror = function (err) {
Expand All @@ -143,7 +142,7 @@ WriteRequest.prototype.makeWriter = function () {
})
}

WriteRequest.prototype.onwrite = function (err) {
WriteRequest.prototype.onwrite = function (err, e) {
const req = this.req
this.req = null

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

if (!err) {
this.file.updateSize(e.currentTarget.length, this.truncating)
}

if (this.truncating) {
this.truncating = false
if (!err) return this.run(req)
Expand Down Expand Up @@ -244,7 +247,16 @@ ReadRequest.prototype.onread = function (err, buf) {

if (err && this.retry) {
this.retry = false
if (this.lock(this)) this.run(req)
if (this.lock(this)) {
this.file.clearFile()
this.run(req)
}
return
}

if (err && err.name === 'NotReadableError') {
this.file.clearFile()
this.run(req)
return
}

Expand Down Expand Up @@ -287,8 +299,15 @@ class EntryFile {
return this._size
}

updateSize (size) {
this._size = size
updateSize (size, truncating = false) {
if (truncating || size > this._size) {
this._size = size
}

this.clearFile()
}

clearFile () {
this._file = null
}

Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
"devDependencies": {
"budo": "^11.6.3",
"puppeteer": "^3.0.2",
"random-access-test": "github:random-access-storage/random-access-test",
"random-access-test": "^1.0.0",
"standard": "^11.0.1",
"tap-finished": "0.0.1",
"tape": "^5.0.0"
Expand Down

0 comments on commit 87629aa

Please sign in to comment.