mrousavy / react-native-mmkv

⚡️ The fastest key/value storage for React Native. ~30x faster than AsyncStorage!
https://mrousavy.com
MIT License
5.91k stars 254 forks source link

android getString from encrypted returns wrong value on hot reloading with expo #738

Open 5o50 opened 2 weeks ago

5o50 commented 2 weeks ago

Hello

Thank you for taking care of this lib

We're using an encrypted storage on both iOS and android through expo. We store a serialized store in a key and deserialize it and load the store on app startup. It works on both iOS an android perfectly.

But when expo hot reloads the apps

iOS app gets the right value from getString() "{ "version": 1, "token": "xxx" }". so it parses and the app reload successfully

Android app fails to parse the serialized store returned from getString() because it returns "\u0000" instead of "{ "version": 1, "token": "xxx" }".

The code is the same for both platforms and when I manually reload the apps getString() returns the right value on both iOS and Android.

only when hot reloading android returns "\u0000" from getString() and we use an encrypted store

thus I wonder if there is a bug on the mmkv android implementation when decrypt or it might need a sleep to wait the IO to finish and return the right value JS wise ?

maintenance-hans[bot] commented 2 weeks ago

Guten Tag, Hans here! 🍻

Thanks for your report. It looks like you have a valid issue with hot reloading in your Android app. However, I need some more information to help troubleshoot this properly.

Could you please provide logs from adb logcat while replicating the issue? This will help mrousavy understand what exactly is happening behind ze scenes.

Also, if you find ze project helpful, consider sponsoring it here for even better support. Looking forward to your logs!

Note: If you think I made a mistake, please ping @mrousavy to take a look.

5o50 commented 2 weeks ago

adblog cat of the fail on hot reloading

2024-09-08 18:21:29.654 21955-22434 MMKV                    com.name.app.local                I  Loading C++ library...
2024-09-08 18:21:29.654 21955-22434 MMKV                    com.name.app.local                I  Installing MMKV JSI Bindings for MMKV root directory: /data/user/0/com.name.app.local/files/mmkv
2024-09-08 18:21:29.654 21955-22434 MMKV                    com.name.app.local                I  Successfully installed MMKV JSI Bindings!
2024-09-08 18:21:29.654 21955-22434 RNMMKV                  com.name.app.local                I  Creating MMKV instance "storage"... (Path: , Encrypted: 1)
2024-09-08 18:21:29.664 21955-22434 ReactNativeJS           com.name.app.local                D  readSession 1
                                                                                                    {
                                                                                                      "userId": "01J41X34R9AB55CXMERAWCN7MJ"
                                                                                                    }
2024-09-08 18:21:29.664 21955-22434 RNMMKV                  com.name.app.local                I  Creating MMKV instance "user-01J41X34R9AB55CXMERAWCN7MJ-storage"... (Path: , Encrypted: 1)
2024-09-08 18:21:29.664 21955-22434 ReactNativeJS           com.name.app.local                D  readSession 2
                                                                                                    {
                                                                                                      "sessionStorage": {
                                                                                                        "user-01J41X34R9AB55CXMERAWCN7MJ-storage": [
                                                                                                          "session"
                                                                                                        ]
                                                                                                      }
                                                                                                    }
2024-09-08 18:21:29.664 21955-22434 ReactNativeJS           com.name.app.local                D  readSession 3
                                                                                                    {
                                                                                                      "serialized": "\u0000"
                                                                                                    }
2024-09-08 18:21:29.665 21955-22434 ReactNativeJS           com.name.app.local                E  SyntaxError: JSON Parse error: Unexpected character: 
2024-09-08 18:21:29.667 21955-22434 ReactNativeJS           com.name.app.local                D  session for userId !!!!????
                                                                                                    {
                                                                                                      "userId": "01J41X34R9AB55CXMERAWCN7MJ",
                                                                                                      "session": null
                                                                                                    }
2024-09-08 18:21:29.668 21955-22434 ReactNativeJS           com.name.app.local                E  Error: Session not found, js engine: hermes
2024-09-08 18:21:29.671 21955-22434 ReactNativeJS           com.name.app.local                E  Error: Session not found, js engine: hermes
2024-09-08 18:21:29.672 21955-22434 ReactNativeJS           com.name.app.local                E  Error: Session not found, js engine: hermes

adb log cat of the success on manual reload

2024-09-08 18:24:41.785 22995-23142 MMKV                    com.name.app.local                I  Loading C++ library...
2024-09-08 18:24:41.786 22995-23142 MMKV                    com.name.app.local                I  Installing MMKV JSI Bindings for MMKV root directory: /data/user/0/com.name.app.local/files/mmkv
2024-09-08 18:24:41.786 22995-23142 MMKV                    com.name.app.local                I  Successfully installed MMKV JSI Bindings!
2024-09-08 18:24:41.786 22995-23142 RNMMKV                  com.name.app.local                I  Creating MMKV instance "storage"... (Path: , Encrypted: 1)
2024-09-08 18:24:41.797 22995-23142 ReactNativeJS           com.name.app.local                D  readSession 1
                                                                                                    {
                                                                                                      "userId": "01J41X34R9AB55CXMERAWCN7MJ"
                                                                                                    }
2024-09-08 18:24:41.797 22995-23142 RNMMKV                  com.name.app.local                I  Creating MMKV instance "user-01J41X34R9AB55CXMERAWCN7MJ-storage"... (Path: , Encrypted: 1)
2024-09-08 18:24:41.797 22995-23142 ReactNativeJS           com.name.app.local                D  readSession 2
                                                                                                    {
                                                                                                      "sessionStorage": {
                                                                                                        "user-01J41X34R9AB55CXMERAWCN7MJ-storage": [
                                                                                                          "session"
                                                                                                        ]
                                                                                                      }
                                                                                                    }
2024-09-08 18:24:41.797 22995-23142 ReactNativeJS           com.name.app.local                D  readSession 3
                                                                                                    {
                                                                                                      "serialized": "{\"version\":1,\"token\":\"xxxx\"}"
                                                                                                    }
2024-09-08 18:24:41.797 22995-23142 ReactNativeJS           com.name.app.local                D  readSession 4
2024-09-08 18:24:41.797 22995-23142 ReactNativeJS           com.name.app.local                D  readSession 5
2024-09-08 18:24:41.803 22995-23142 ReactNativeJS           com.name.app.local                D  session for userId !!!!????
                                                                                                    {
                                                                                                      "userId": "01J41X34R9AB55CXMERAWCN7MJ",
                                                                                                      "session": {
                                                                                                        "version": 1,
                                                                                                        "token": "xxxx"
                                                                                                      }
                                                                                                    }
2024-09-08 18:24:41.896 22995-23199 ProfileInstaller        com.name.app.local                D  Installing profile for com.name.app.local
2024-09-08 18:24:41.907 22995-23142 ReactNativeJS           com.name.app.local                I  Loading react-native-worklets-core...
2024-09-08 18:24:41.908 22995-23142 ReactNativeJS           com.name.app.local                I  Worklets loaded successfully
mrousavy commented 2 weeks ago

which version

5o50 commented 2 weeks ago

react-native-mmkv: 2.12.2 expo: 51

divyansh0908nl commented 1 week ago

Happening on iOS too. On hot reload, it's returning empty string. But when the app is reloading it's loading correct values.

version: 3.0.0-beta.5

After saving the value to the mmkv, I also put added a setTimeout for two seconds which loads the same value, and it was returning "" or null sometimes.