huan / flash-store

FlashStore is a high-performance Key-Value Persistent Local Database using ES6 Map's API (Async & Sync), Powered by LevelDB/RocksDB/SQLite and TypeScript.
https://paka.dev/npm/flash-store
Apache License 2.0
20 stars 4 forks source link

Question about `flockSync()` #76

Closed su-chang closed 4 years ago

su-chang commented 4 years ago

In the constructor of FlashStore, I have some questions about these code:

    this.lockFd = fs.openSync(lockFile, 'w')
    try {
      flockSync(this.lockFd, 'exnb')
    } catch (e) {
      log.error('FlashStore', 'constructor() wEorkdir("%s") is busy: maybe there another FlashStore are using it?', this.workdir)
      throw e
    }

I found that if we call wechaty.reset() some Errors will happened, and then we can not use cache any more.

If we change the params exnb to shnb in method flockSync(this.lockFd, 'exnb') , it would make cache work in the next start.

But I'm not sure it would make some other bugs or not.

huan commented 4 years ago

Please check if you close the flashStore instance correctly when you are resetting the puppet.

If you open a flash store workdir for the second time without close the previous one, you will not be able to get the lock.

That's by design, so we need to use LOCK_EX instead of LOCK_SH for flock. See: http://man7.org/linux/man-pages/man2/flock.2.html

su-chang commented 4 years ago

Thanks for your response, I have found the reason of this bug, one FlashStore has not been closed success. Everything is ok now!

Thanks for learning something about LOCK_EX and LOCK_SH too~

huan commented 4 years ago

Glad to know that. Cheers!