Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Intermittent uncaught error from gaze #134

Closed
callumlocke opened this issue Jul 9, 2014 · 0 comments
Closed

Intermittent uncaught error from gaze #134

callumlocke opened this issue Jul 9, 2014 · 0 comments

Comments

@callumlocke
Copy link
Contributor

I'm getting this error a lot while using gaze to watch a directory, and using Sublime Text 3 (on OS X Mountain Lion) to edit files in that directory. About 1 in every 5 saves causes my Node process to exit with this error:

Error: ENOENT, no such file or directory '/Users/callum.locke/code/ft/ft/manual/exp2-app/.subl38b.tmp'
  at Object.fs.lstatSync (fs.js:679:18)
  at /Users/callum.locke/code/exp/node_modules/gaze/lib/gaze.js:270:21
  at iterate (/Users/callum.locke/code/exp/node_modules/gaze/lib/helper.js:106:5)
  at Object.forEachSeries (/Users/callum.locke/code/exp/node_modules/gaze/lib/helper.js:121:3)
  at /Users/callum.locke/code/exp/node_modules/gaze/lib/gaze.js:267:12
  at ReaddirReq.Req.done (/Users/callum.locke/code/exp/node_modules/gaze/node_modules/graceful-fs/graceful-fs.js:143:5)
  at ReaddirReq.done (/Users/callum.locke/code/exp/node_modules/gaze/node_modules/graceful-fs/graceful-fs.js:90:22)
  at ReaddirReq.Req.done (/Users/callum.locke/code/ft/web/node_modules/graceful-fs/graceful-fs.js:143:5)
  at ReaddirReq.done (/Users/callum.locke/code/ft/web/node_modules/graceful-fs/graceful-fs.js:90:22)
  at Object.oncomplete (fs.js:107:15)

The problem is lines 270-271 in lib/gaze.js:

if (!fs.existsSync(filepath)) return next();
var stat = fs.lstatSync(filepath); // this line occasionally throws an ENOENT error

I think this is probably due to this thing explained in the Node docs for fs.exists:

fs.exists() is an anachronism and exists only for historical reasons. There should almost never be a reason to use it in your own code.

In particular, checking if a file exists before opening it is an anti-pattern that leaves you vulnerable to race conditions: another process may remove the file between the calls to fs.exists() and fs.open(). Just open the file and handle the error when it's not there.

@shama shama closed this as completed in 9747a56 Jul 10, 2014
shama added a commit that referenced this issue Jul 10, 2014
Prevent race condition (fixes #134)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant