Node-fs-extra: emptyDir callback firing twice on EPERM error

Created on 16 Mar 2017  路  13Comments  路  Source: jprichardson/node-fs-extra

The callback is firing twice on my computer when the folder supplied cannot be removed because it is currently accessed by another program.

fs.emptyDir(`${directory}/public/cache`, function(error){
    console.log('Callback')
});

I have no problem if I use fs.emptyDirSync.
I was running version 2.1.1 of fs-extra.

bug platform-windows

Most helpful comment

Fixed in v2.1.2

All 13 comments

@kueckermann Does the callback call with the exact same error both times?

Can't see how this could happen in the code, but it wouldn't hurt to port the internals to promises to make sure this doesn't happen. @manidlou do you want to do this?

@RyanZim Looks like they are both EPERM but different functions (unlink and lstat).
Since this function is running on many directories I am logging the directory I am trying to empty and then the error if it exsists otherwise "No Error". I have shortened the file paths to make it clearer.

...
...\nodes\user_frontend_social_login,   No Error
...\nodes\user_frontend_user,           No Error
...\nodes\user_frontend_explore,        EPERM: operation not permitted, unlink '...\cache\en-US'
...\nodes\user_frontend_explore,        EPERM: operation not permitted, lstat '...\cache\en-US'

OK, this one is going to take a little digging.

Given that this error originates from unlink and lstat calls, it must be a bug in the remove module (which emptyDir uses under the hood); emptyDir itself doesn't call unlink or lstat.

Could you post the stack traces for both errors?

@manidlou do you want to do this?

Sure thing.

it must be a bug in the remove module (which emptyDir uses under the hood); emptyDir itself doesn't call unlink or lstat.

Agreed.

@RyanZim Here is the stack, not much there but maybe something you are looking for.

Error: EPERM: operation not permitted, unlink '...\cache\en-US'
    at Error (native)
Error: EPERM: operation not permitted, lstat '...\cache\en-US'
    at Error (native)

Again, I just shortened the file paths to lessen the text.

@kueckermann would you please tell us what OS system and node version you are using?

@manidlou I am on Windows 10, version 1607.
Node version 6.9.5

Ugh, probably a Windows-only bug.

@manidlou Don't bother with what I said about promises earlier, since the bug is in remove, that doesn't need done.

@manidlou Don't bother with what I said about promises earlier, since the bug is in remove, that doesn't need done. :+1:

@kueckermann I've got an idea. Try this:

Edit the file node_modules/fs-extra/lib/remove/rimraf.js. On line 86, it says fixWinEPERM(p, options, er, cb). Change that line to read return fixWinEPERM(p, options, er, cb). Then try to reproduce this issue and report back. Thanks!

@RyanZim I added return fixWinEPERM(p, options, er, cb) and this solved the issue! Good job.

OK, that needs to be fixed in our codebase. Also, that patch needs to be forwarded to rimraf, since this bug also probably exists there too. (fs-extra's remove is a customized version of rimraf.)

I'll try to get this done.

Fixed in v2.1.2

Was this page helpful?
0 / 5 - 0 ratings