Skip to content

Commit

Permalink
Handle fs.read EAGAIN errors by trying again
Browse files Browse the repository at this point in the history
  • Loading branch information
isaacs committed Sep 18, 2012
1 parent b8d93ba commit ff11bee
Showing 1 changed file with 35 additions and 1 deletion.
36 changes: 35 additions & 1 deletion graceful-fs.js
Expand Up @@ -2,6 +2,7 @@
// fs operations wait until some have closed before trying to open more. // fs operations wait until some have closed before trying to open more.


var fs = require("fs") var fs = require("fs")
, mkdirp = require("mkdirp")


// there is such a thing as TOO graceful. // there is such a thing as TOO graceful.
if (fs.open === gracefulOpen) return if (fs.open === gracefulOpen) return
Expand Down Expand Up @@ -259,7 +260,6 @@ if (!fs.lchown) {







// on Windows, A/V software can lock the directory, causing this // on Windows, A/V software can lock the directory, causing this
// to fail with an EACCES or EPERM if the directory contains newly // to fail with an EACCES or EPERM if the directory contains newly
// created files. Try again on failure, for up to 1 second. // created files. Try again on failure, for up to 1 second.
Expand All @@ -277,3 +277,37 @@ if (process.platform === "win32") {
}) })
} }
} }


// if read() returns EAGAIN, then just try it again.
var read = fs.read
fs.read = function (fd, buffer, offset, length, position, callback_) {
var callback
if (callback_ && typeof callback_ === 'function') {
var eagCounter = 0
callback = function (er, bytesRead) {
if (er && er.code === 'EAGAIN' && eagCounter < 10) {
eagCounter ++
return read.call(fs, fd, buffer, offset, length, position, callback)
}
callback_(er, bytesRead)
}
}
return read.call(fs, fd, buffer, offset, length, position, callback)
}

var readSync = fs.readSync
fs.readSync = function (fd, buffer, offset, length, position) {
var eagCounter = 0
while (true) {
try {
return readSync.call(fs, fd, buffer, offset, length, position)
} catch (er) {
if (er.code === 'EAGAIN' && eagCounter < 10) {
eagCounter ++
continue
}
throw er
}
}
}

0 comments on commit ff11bee

Please sign in to comment.