Concurrent `setItem` and `getItem` can lead into an unahdled `does not look like a valid storage file`
See original GitHub issueConcureent setItem
and getItem
can lead into an unahdled does not look like a valid storage file
:
parse error: {} for: Error: [node-persist][readFile] .node-persist/storage/37b51d194a7513e45b56f6524f2d51f2 does not look like a validstorage file! at fs.readFile (node_modules/node-persist/src/local-storage.js:278:66) at FSReqWrap.readFileAfterClose [as oncomplete] (fs.js:525:3)
The code used to reproduce create that error was:
const localdb = require('node-persist')
const storage = localdb.create({ ttl: true, logging: true })
async function test2 () {
await storage.setItem('bar', { 'foo': 'Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet. Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet.' })
await storage.getItem('bar')
await storage.setItem('bar', { 'foo': 'Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet. Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet.' })
await storage.getItem('bar')
await storage.setItem('bar', { 'foo': 'Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet. Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet.' })
await storage.getItem('bar')
}
async function test3 () {
await storage.getItem('bar')
await storage.getItem('bar')
await storage.getItem('bar')
await storage.getItem('bar')
await storage.getItem('bar')
}
async function main () {
await storage.init()
try {
await Promise.all([
test2(),
test3(),
test2(),
test3(),
test2()
])
} catch (err) {
console.error(err)
}
}
main().catch(err => {
console.error(err)
})
The problem cannot be realiable be reproduces.
Issue Analytics
- State:
- Created 5 years ago
- Comments:16
Top Results From Across the Web
Node-persist on heavy Node.js app not returning values
It seems that node-persist does not handle concurrent reads and writes reliable. If a getItem is called while a setItem is in progress, ......
Read more >node-persist - Bountysource
Concureent setItem and getItem can lead into an unahdled does not look like a valid storage file : parse error: {} for: Error:...
Read more >New node-persist code is generating errors in the logs - GitLab
In the future, promise rejections that are not handled will terminate the Node.js process ... does not look like a valid storage file!...
Read more >API | Async Storage - GitHub Pages
getItem . Gets a string value for given key . This function can either return a string value for existing key or return...
Read more >CUDA C++ Programming Guide - NVIDIA Documentation Center
The programming guide to the CUDA model and interface. ... As a result, the final binary may perform worse than would be possible...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
When
storage.init
(storage beingnode-persist
) encounters a corrupt file it throws this error.I tried using
try/catch
and.catch()
callbacks to handle this error gracefully, but neither work because the error is not bubbling up to where I can handle it.For those looking for a drop-in replacement for this library, I’ve switched to https://www.npmjs.com/package/node-localstorage and it seems to be working fine. The api is basically similar with
getItem()
andsetItem()
I think the better approach would be to serialise read and writes based on the queue. One approach as mentioned in my SO answer(see the gist) above is to chain promises. I think we can enhance it to simply chain promises per key rather than whole universe of read and write through the library.
Please see the basic idea here : https://gist.github.com/biswanaths/b8999c9e4e5a9e979dcde061074528f6
I believe whatever solution to this problem should live in library. The end user does not need to know and work around whether the library is storing kv per file or using a single file.