isar / hive

Lightweight and blazing fast key-value database written in pure Dart.
Apache License 2.0
4.11k stars 410 forks source link

Exception when opening encrypted box after re-initialising Hive #655

Open MichaelM97 opened 3 years ago

MichaelM97 commented 3 years ago

Steps to Reproduce I updated Hive from version 2.0.0 -> 2.0.4, I performed logic which re-initialises Hive (using Hive.initFlutter) with a new subDir, I then got the following exception when attempting to open an encrypted box:

7.3.0 - [Firebase/Firestore][I-FST000001] WatchStream (2839f3918) Stream error: 'Not found: Target id not found: 64'
flutter: Recovering corrupted box.
[VERBOSE-2:ui_dart_state.cc(186)] Unhandled Exception: HiveError: This should not happen. Please open an issue on GitHub.
#0      BinaryReaderImpl.readFrame (package:hive/src/binary/binary_reader_impl.dart:250:7)
#1      FrameHelper.framesFromBytes (package:hive/src/binary/frame_helper.dart:21:26)
#2      FrameIoHelper.framesFromFile (package:hive/src/io/frame_io_helper.dart:42:12)
<asynchronous suspension>
#3      StorageBackendVm.initialize (package:hive/src/backend/vm/storage_backend_vm.dart:86:11)
<asynchronous suspension>
#4      HiveImpl._openBox (package:hive/src/hive_impl.dart:116:9)
<asynchronous suspension>
#5      HiveImpl.openBox (package:hive/src/hive_impl.dart:145:12)
<asynchronous suspension>
#6      loadSearchResultsFromStorage.<anonymous closure> (package:hunni/middleware/file_middleware.dart:157:30)
<asynchronous suspension>

Now whenever I open my app, when attempting to open an encrypted box I get the following exception:

[VERBOSE-2:ui_dart_state.cc(186)] Unhandled Exception: HiveError: This should not happen. Please open an issue on GitHub.
#0      BinaryReaderImpl.readFrame (package:hive/src/binary/binary_reader_impl.dart:250:7)
#1      FrameHelper.framesFromBytes (package:hive/src/binary/frame_helper.dart:21:26)
#2      FrameIoHelper.framesFromFile (package:hive/src/io/frame_io_helper.dart:42:12)
<asynchronous suspension>
#3      StorageBackendVm.initialize (package:hive/src/backend/vm/storage_backend_vm.dart:86:11)
<asynchronous suspension>
#4      HiveImpl._openBox (package:hive/src/hive_impl.dart:116:9)
<asynchronous suspension>
#5      HiveImpl.openBox (package:hive/src/hive_impl.dart:145:12)
<asynchronous suspension>

Code sample

Provide a few simple lines of code to show your problem.

Version

alexeenko-frontend commented 3 years ago

@MichaelM97 I have the same problem. Any news?

BurykinNikolay commented 3 years ago

same problem

salihagic commented 2 years ago

Same problem with hive "2.0.5"

yakubmoris commented 1 year ago

Facing the same Issue.

hive: ^2.2.1 hive_flutter: ^1.1.0 hive_generator: ^1.1.3

image

getting this error 'Recovering corrupted box' upon **re-initialising on next run, mentioning the steps what I got on my side. It might help to figure out exact issue.

steps to reproduce :

  1. uninstall the app with hive if its installed earlier
  2. install app and make sure Hive.initFlutter() and register addapter runs and you also saved some data after that. During this session I also confirmed, the saved data I was able to read it back without any issue.
  3. close app by back press OR by doing swipe to make force close. Problem starts from here.
  4. start app again you won't find the saved data in hive in very first run after installing app same time you will see the error Recovering corrupted box in console
  5. this time you save have to save the data again into hive, data saved during this session can be retrieved successfully on all future app runs.

any news when it will get fixed please?

jakobleck commented 1 year ago

@yakubmoris Could you perhaps provide a MWE for your reproduction? Could not reproduce it both in a complex app and a small MWE. (I have encountered the "Recovering corrupted box" as well, with hive 2.1.0, hive_flutter 1.1.0, flutter 3.3.5, but the origins of the problem are totally unclear and not reproducible as of now.)

RootSoft commented 1 year ago

@yakubmoris Currently running into the same situation. In my case, the code to generate and store an EncryptionKey (using FlutterSecureStorage) is running multiple times because I provided it to multiple boxes. => The last executed generateSecureKey() is overriding existing ones. My fix was to move the generateEncryptionKey method first, before providing Boxes. Hope this helps.