cacha icon indicating copy to clipboard operation
cacha copied to clipboard

Handle cross-process conflict

Open jamestalmage opened this issue 8 years ago • 2 comments

We need this to work across multiple process.

Specifically, how to handle cacha.get when the file is still open for write via cacha.set in a separate process.

I am not sure exactly how Node behaves in this scenario (hopefully it just returns / throws an error). If that's the case, maybe just a delay / retry loop.

Alternatively (but more complicated, and I'm still not 100% sure how it would look); Have it write to a temp file, then rename. I think renames are atomic cross-platform.

Windows: how to do atomic writes in a file

Things UNIX can do atomically

I think we are mostly safe on this since the file name is the hash of the contents. That prevents us from having to decide who "wins" in a race condition for writes (if a race condition for the same filename exists, it doesn't matter who wins - they are both trying to write the same contents).

The issue is mostly how to handle reads while the file is write-locked.

jamestalmage avatar Nov 13 '15 03:11 jamestalmage

See https://github.com/yeoman/configstore/issues/20 and how it was solved.

sindresorhus avatar Nov 13 '15 07:11 sindresorhus

TL; DR; https://github.com/iarna/write-file-atomic

jamestalmage avatar Nov 13 '15 08:11 jamestalmage