vue-electron / vuex-electron

Integration of Vuex and Electron
MIT License
305 stars 97 forks source link

Error:EPERM:operation not permitted,rename #17

Closed gaodeng closed 5 years ago

gaodeng commented 5 years ago

qq 20181228143321

mint-dewit commented 5 years ago

I ran into the same issue. There should be no reason why we would not be allowed to change a file we created ourselves, so my guess is that Windows Defender is preventing this because we interact with the file too often. I've added the entire program folder of my vue-electron app as an exception, so I guess I'll see what that brings in a short while.

Unfortunately this error completely stops the program from working, so we should add least add some error handling and back up methods for this.

mint-dewit commented 5 years ago

Adding the exception did not help, unfortunately. Issue seems to have been reported in electron-store though: https://github.com/sindresorhus/electron-store/issues/31

morosefrog commented 5 years ago

I also encountered the same problem, is there a way to solve it?

gaodeng commented 5 years ago

PR https://github.com/vue-electron/vuex-electron/pull/20

michaeljpeake commented 5 years ago

I've just spent a few hours hunting this down. The following is my understanding...

Ultimately, I think the issue comes from here.

vuex-electron uses electron-store which uses conf which uses write-file-atomic. The latter has a function, writeFileSync, which is supposed to prevent multiple processes from overwriting the vuex.json file at the same time. It does this by taking a copy of it, editing the copy, then renaming that copy to replace the original. The rename command is this:

fs.renameSync(tmpfile, filename)

It turns out that in Windows (and it seems Windows only), the rename ability is not atomic; i.e. multiple processes can try to do it at once. When one process has beaten another to it, you get this EPERM error.

The solution for my application is going to be to throttle the use of setState() in vuex-electron. This is built on top of my previous fork which only allows the main function to write to the store (whereas this library currently lets the renderers to the writing also). I'll share a link to my version some time soon.

I may do a pull request in time but I think we need to offer developers the option of using a throttle or debounce and let them choose what is appropriate for them.

baruchiro commented 5 years ago

I'm new in node, electron, vue. But some questions:

michaeljpeake commented 5 years ago

Is this repository managed? I understand that this is just a plugin for vuex. Could it be time for fork or new development?

44 Basically, no. These things take up a lot of unpaid time sadly.

Is there a workaround for this bug?

I have created a fix (by throttling setState). I'll put it on my fork later today and share a link.

michaeljpeake commented 5 years ago

@baruchiro Regarding your yarn thing ...

My understanding is that electron-store@2.0.0 is kept because it is a dependency of this project. The problem, however, is with write-file-atomic and Windows.

baruchiro commented 5 years ago

My workaround was done one commit to adding all items in a collection, instead of committing each.

michaeljpeake commented 5 years ago

@baruchiro Feel free to try my forked repo.

When you initiate createPersistedState(), you can include throttle (number in milliseconds) in your options; e.g.

createPersistedState({throttle: 1000})

This will mean the app writes to disk a maximum of once every second. It's worked well for me. It's also a trailing throttle, meaning the last setState will be applied so long as the app isn't closed with throttle milliseconds.

baruchiro commented 5 years ago

@michaeljpeake Does your fork have a package?

michaeljpeake commented 5 years ago

You should be able to install it with npm install git+https://github.com/michaeljpeake/vuex-electron.git

tominal commented 4 years ago

@michaeljpeake there was an issue with your last variable. i forked the fork and it's working great. the slight delay in state change is a good compromise to avoid those annoying errors.

michaeljpeake commented 4 years ago

Thanks @tominal. I see I changed the variable name but not its declaration. Oops. Fixed in my repo too now, in case I come back to it.

Compromise is a suitable word. It doesn't feel quite right but it seems to be working. Also, if your store gets big, it's probably desirable to avoid writing it to disk constantly.

suifengtec commented 4 years ago

@michaeljpeake

You should be able to install it with npm install git+https://github.com/michaeljpeake/vuex-electron.git

somewhere of the filenode_modules/vuex-electron/dist/persisted-state.js , need to modify to :

var last, e = Date.now();
michaeljpeake commented 4 years ago

Thanks @suifengtec. I needed to actually build the thing rather than just update the src code.

sudhakar3697 commented 3 years ago

You should be able to install it with npm install git+https://github.com/michaeljpeake/vuex-electron.git

Hi @michaeljpeake

Any specific reason you decided not to publish a new npm module?. Please do consider publishing a package on npm with your forked repo.

Thank you.