mitsuru / tough-cookie-filestore

28 stars 38 forks source link

Concurrency issues with saving to JSON file #3

Open nylen opened 10 years ago

nylen commented 10 years ago

The saveToFile method calls the async version of fs.writeFile, so it's possible that this method will be called multiple times concurrently. This would either corrupt the JSON file, or fail with an error - I'm not sure which.

nylen commented 10 years ago

This also means that there's not a reliable way to tell when tough-cookie-filestore has finished writing the JSON file. Since most people will probably be using this library from request, which only supports synchronous operations, I'd recommend just using fs.writeFileSync instead.

fritx commented 9 years ago

:+1: for this I just met the same problem The json file became a mess

undefined:1
895Z","hostOnly":false,"lastAccessed":"2015-01-22T13:44:01.895Z"}}}}94Z"},"pt4
                                                                    ^
SyntaxError: Unexpected number
    at Object.parse (native)
    at loadFromFile (/home/fritx/dev/qq/node_modules/tough-cookie-filestore/lib/filestore.js:150:32)
    at new FileCookieStore (/home/fritx/dev/qq/node_modules/tough-cookie-filestore/lib/filestore.js:14:5)

And I simply tried fs.writeFileSync instead inside saveToFile then it works

ngotchac commented 9 years ago

+1

nylen commented 9 years ago

Hey @sarkian, we at @request would like to use your fork of tough-cookie-filestore instead. Open an issue and ping me and @simov on it.

Here's some more context: https://github.com/request/request/pull/1192