react-native-mmkv icon indicating copy to clipboard operation
react-native-mmkv copied to clipboard

Intermittent MMKV Data Loss after Background/Kill (Encrypted) with InvalidProtocolBuffer truncatedMessage Decode Error

Open pratikSeven opened this issue 3 months ago • 18 comments

Hi Team,

We are seeing an intermittent but impactful issue where the encrypted MMKV instance sometimes comes back empty after the app is backgrounded or force-closed for a period, resulting in user logout and loss of cached session/context. Looking for guidance to diagnose and confirm whether this is a corruption, flushing, or decoding/encryption edge case.

Summary

  • Normal operation: Reads/writes work reliably while the app is active.
  • Problem: After backgrounding or the OS/user killing the app (and reopening minutes later), the MMKV store occasionally initializes empty.
  • Frequency: Sporadic but frequent enough to affect production users.
  • Error observed (Android & iOS logs):
  • MiniPBCoder.cpp:591::decodeOneMap InvalidProtocolBuffer truncatedMessage
  • [E] MiniPBCoder.cpp:591::decodeOneMap InvalidProtocolBuffer truncatedMessage
  • Encryption is enabled.
  • One stored value is a JSON string (~4.9 kB). All other keys are very small (tokens, flags, etc.).

Steps to (Intermittently) Reproduce

  • Launch app; write several small keys plus one ~4.9 kB JSON string.
  • Confirm keys exist via getAllKeys().
  • Background app OR swipe-kill.
  • Wait 10–30+ minutes (sometimes less).
  • Relaunch.
  • Observe: storage is empty; decode error logged natively.

pratikSeven avatar Sep 22 '25 14:09 pratikSeven

Guten Tag, Hans here.

[!NOTE] New features, bugfixes, updates and other improvements are all handled mostly by @mrousavy in his free time. To support @mrousavy, please consider 💖 sponsoring him on GitHub 💖. Sponsored issues will be prioritized.

maintenance-hans[bot] avatar Sep 22 '25 15:09 maintenance-hans[bot]

@mrousavy facing the same issue. Only for IOS. The Data is suddenly getting wiped out while using encryption key with MMKV. Please look into it.

ninad-joshi-asurion avatar Sep 23 '25 06:09 ninad-joshi-asurion

we disabled the encryption and the storage is still getting wiped

andrejsoucek avatar Sep 23 '25 08:09 andrejsoucek

Which version are you using? I think it's time we need issue templates for react-native-mmkv.

Try V4 and let me know if you can reproduce it there

mrousavy avatar Sep 23 '25 10:09 mrousavy

I am using the react-native-mmkv v3.3.1 let me try the beta version.

pratikSeven avatar Sep 23 '25 10:09 pratikSeven

This problem also occurs here. I tested it on v4, and it happens too. I have around 500 users who are active daily with a good amount of screen time. I only see this issue about ten cases a day. So it's quite sporadic, there's no pattern, and it's not reproducible, but it does happen.

Apparently, if the app crashes or suddenly terminates, sometimes due to the phone's own system, because it's running in the background, etc., and by chance, at the same time, mmkv is in the middle of an operation, it ends up corrupting the file.

Or even during a read/write operation on a large amount of data that the phone doesn't have enough memory to handle, it ends up corrupting the file too. And because it doesn't have atomicity mechanisms or rowbacks like traditional databases, all the data is lost.

PatrickWalter387 avatar Sep 24 '25 23:09 PatrickWalter387

But only if it's encrypted, right?

mrousavy avatar Sep 25 '25 08:09 mrousavy

Yes in our case it is only happening when we are using encrypted MMKV

ninad-joshi-asurion avatar Sep 25 '25 12:09 ninad-joshi-asurion

we are facing the same issue but not in Encrypted does any one face this issue?

Ankitinara avatar Nov 03 '25 13:11 Ankitinara

@Ankitinara @ninad-joshi-asurion @PatrickWalter387 @pratikSeven just wondering, does everyone above having this issue use redux-persist?

nazmeln avatar Nov 11 '25 17:11 nazmeln

@nazmeln Actually, yes — I did have the same issue. It happened exactly as I described earlier. I ended up having to migrate to a custom implementation using blob files and integrate it with redux-persist.

PatrickWalter387 avatar Nov 11 '25 19:11 PatrickWalter387

@Ankitinara @ninad-joshi-asurion @PatrickWalter387 @pratikSeven just wondering, does everyone above having this issue use redux-persist?

Yes, we are using redux-persist.

pratikSeven avatar Nov 12 '25 10:11 pratikSeven

@Ankitinara @ninad-joshi-asurion @PatrickWalter387 @pratikSeven just wondering, does everyone above having this issue use redux-persist?

Yes we are using it redux persist. Faced this for both encrypted and non encrypted storage. Facing the same issue for V4 version as well

ninad-joshi-asurion avatar Nov 12 '25 10:11 ninad-joshi-asurion

Same here we are using redux-persist. We are facing the issue with non encrypted storage.

xaviraol avatar Nov 14 '25 07:11 xaviraol

It has to do with encryption, so every time you try to use storage.getString(key) you need to recrypt first.

const instance = createMMKV({
      id: mmkvId,
      encryptionKey: key,
      mode: 'single-process',
      readOnly: false,
    });

instance.recrypt(key);

FindingShrek avatar Nov 21 '25 04:11 FindingShrek

Are you sure the key is exactly 16 bytes?

elbadrawy avatar Nov 27 '25 22:11 elbadrawy

I'm not using redux persist or encryption but faced this issue too firsthand. My app crashed due to another issue, and when I restarted, previously stored user preferences were gone.

ottor-o avatar Nov 28 '25 10:11 ottor-o

Experiencing the same issue here, with a very minimal default setup (new MMKV() / createMMKV()). Seems to be fine in v3.3.0, but it occurs on both higher v3.3.x and v4.

aeresc avatar Nov 28 '25 14:11 aeresc