Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

HiveError: This should not happen. (while opening a box) #192

Closed
seanmc86 opened this issue Jan 21, 2020 · 25 comments
Closed

HiveError: This should not happen. (while opening a box) #192

seanmc86 opened this issue Jan 21, 2020 · 25 comments
Assignees
Labels
bug Something isn't working

Comments

@seanmc86
Copy link

seanmc86 commented Jan 21, 2020

Error
HiveError: This should not happen. Please open an issue on GitHub.

#0      BinaryReaderImpl.readFrame (package:hive/src/binary/binary_reader_impl.dart:234:7)
#1      FrameHelper.framesFromBytes (package:hive/src/binary/frame_helper.dart:16:26)
#2      FrameIoHelper.framesFromFile (package:hive/src/io/frame_io_helper.dart:37:12)
<asynchronous suspension>
#3      StorageBackendVm.initialize (package:hive/src/backend/vm/storage_backend_vm.dart:74:29)
<asynchronous suspension>
#4      BoxBaseImpl.initialize (package:hive/src/box/box_base_impl.dart:82:20)
#5      HiveImpl._openBox (package:hive/src/hive_impl.dart:97:17)
<asynchronous suspension>
#6      HiveImpl.openBox (package:hive/src/hive_impl.dart:114:18)
#7      LocalDataSourceImpl.getLastPersons (package:xx/data/provider/local_data_source.dart:35:35)
#8      MessageRepositoryImpl.getPersons (package:xx/data/repository/message_repository.dart:34:43)
#9      HomeBloc.mapEventToState (package:xx/bloc/home/home_bloc.dart:23:31)
<asynchronous suspension>
#10     Bloc._bindStateSubject.<anonymous closure> (package:bloc/src/bloc.dart:155:14)
#11     Stream.asyncExpand.onListen.<anonymous closure> (dart:async/stream.dart:576:30)
#12     _rootRunUnary (dart:async/zone.dart:1134:38)
#13     _CustomZone.runUnary (dart:async/zone.dart:1031:19)
#14     _CustomZone.runUnaryGuarded (dart:async/zone.dart:933:7)
#15     _BufferingStreamSubscription._sendData (dart:async/stream_impl.dart:338:11)
#16     _DelayedData.perform (dart:async/stream_impl.dart:593:14)
#17     _StreamImplEvents.handleNext (dart:async/stream_impl.dart:709:11)
#18     _PendingEvents.schedule.<anonymous closure> (dart:async/stream_impl.dart:669:7)
#19     _rootRun (dart:async/zone.dart:1122:38)
#20     _CustomZone.run (dart:async/zone.dart:1023:19)
#21     _CustomZone.runGuarded (dart:async/zone.dart:925:7)
#22     _CustomZone.bindCallbackGuarded.<anonymous closure> (dart:async/zone.dart:965:23)
#23     _rootRun (dart:async/zone.dart:1126:13)
#24     _CustomZone.run (dart:async/zone.dart:1023:19)
#25     _CustomZone.runGuarded (dart:async/zone.dart:925:7)
#26     _CustomZone.bindCallbackGuarded.<anonymous closure> (dart:async/zone.dart:965:23)
#27     _microtaskLoop (dart:async/schedule_microtask.dart:43:21)
#28     _startMicrotaskLoop (dart:async/schedule_microtask.dart:52:5)

Steps to Reproduce
On app start-up I have a function to retrieve persons HiveObjects that are stored:

final personsBox = await Hive.openBox<Person>('persons');

Error is immediately seen on this line. The app runs a few times without issue, then this popped up suddenly on a subsequent run.

Code sample

Future<void> main() async {
final docDirectory = await getApplicationDocumentsDirectory();
Hive.init(docDirectory.path);
Hive.registerAdapter(PersonAdapter());
}

...

final personsBox = await Hive.openBox<Person>('persons');

Version

  • Platform: VSCode on macOS, Flutter debugging on iOS
  • Hive version: 1.3.0
Flutter 1.12.13+hotfix.5 • channel stable • https://github.com/flutter/flutter.git
Framework • revision 27321ebbad (6 weeks ago) • 2019-12-10 18:15:01 -0800
Engine • revision 2994f7e1e6
Tools • Dart 2.7.0
@seanmc86 seanmc86 added the bug Something isn't working label Jan 21, 2020
@simc
Copy link
Member

simc commented Jan 21, 2020

Did you find a way to reproduce this error? It would be very helpful...

@seanmc86
Copy link
Author

seanmc86 commented Jan 21, 2020

I have been trying to without any luck so far.

I initially thought it had something to do with my app state. Some context on what the app is doing:

  1. Hive is responsible for persisting various HiveObjects, including [Persons] and [Messages]
  2. My app on first load creates 5 fake [Person] and 10 fake [Message], putting them into [Persons] and [Messages] boxes respectively using auto incrementing keys, via add()
  3. After this, every few seconds it creates a new fake [Message] and calls the same add() function to the box
  4. If the app is reopened or cold restarted, it will retrieve the existing [Persons] and [Messages] and only repeat step 3 above

On first load this process always works. If I hot or cold restart, kill the app, or send it to background and back to foreground, all of them have the tendency to produce the error on subsequent loads.

I have noticed on some reloads, the debugger catches a "Recovering corrupt box" on the [Persons] box, but then I lose the existing associations between [Persons] and already generated [Messages]. Sometimes it does not display this message though, and simply fails to openBox().

I will keep trying other ways, but any suggestions on testing are appreciated.

@miltoneiji
Copy link
Contributor

Hi, I am also having this bug but I did not find a way to reproduce it in a consistent way too.
By reading the code and trying to understand it, I had some doubts:

  1. Sanity-check: the BinaryReaderImpl has a buffer that is basically a list of UInt8, right? Then, in order to read a UInt32, you join 4 of those UInt8, right?
  2. So, why that result can not be lesser than 8? Shouldn't it be possible?

@daibao520
Copy link

same problem
My app creash and restart immediately by android system,then this error occur

@daibao520
Copy link

android
1 open a new activity
2 flutter activity go to background
3 close new activity
4 some times you see the log like bellow

W/ActivityThread(29610): handleWindowVisibility: no activity for token android.os.BinderProxy@fe57651
V/ActivityThread(29610): callActivityOnCreate
D/HMSAgent(29610): Activity(1252)->Application(380)->ActivityMgr(170)->onActivityCreated
D/HMSAgent(29610): onCreated:com.yunzhan.liaobei.MainActivity@2663eb6
D/FlutterActivityAndFragmentDelegate(29610): Setting up FlutterEngine.
D/FlutterActivityAndFragmentDelegate(29610): No preferred FlutterEngine was provided. Creating a new FlutterEngine for this FlutterFragment.
D/FlutterActivityAndFragmentDelegate(29610): Attaching FlutterEngine to the Activity that owns this Fragment.

5 FlutterEngine create and main funcion in dart be called.
6 error show
7 restart app error show

@leisim

@stevenspiel
Copy link

stevenspiel commented Apr 7, 2020

I'm on 1.4.1+1 and see that the error comes from lib/src/binary/binary_reader_impl.dart:249

Frame readFrame({HiveCipher cipher, bool lazy = false, int frameOffset}) {
    if (availableBytes < 4) return null;

    var frameLength = readUint32();
    if (frameLength < 8) {
      throw HiveError(
          'This should not happen. Please open an issue on GitHub.');
    }
    if (availableBytes < frameLength - 4) return null;

In my scenario, frameLength is 0. When using a debugger to step into the readUint32() that returns 0, this is the state of things:

Screen Shot 2020-04-07 at 3 06 35 PM

While I'm unsure of how to reproduce this, I hope what I provided helps. @leisim Does anything stand out to you?

Is it a problem that _bufferLimit == _offset?

Also, fwiw, _buffer.toString() returns this value:

[44, 0, 0, 0, 1, 25, 83, 117, 112, 112, 111, 114, 116, 101, 100, 86, 101, 114, 115, 105, 111, 110, 70, 101, 116, 99, 104, 101, 100, 65, 116, 16, 0, 80, 24, 7, 15, 21, 119, 66, 160, 122, 75, 115, 47, 0, 0, 0, 1, 28, 73, 110, 115, 101, 99, 116, 105, 99, 105, 100, 101, 84, 97, 103, 83, 99, 111, 114, 101, 70, 101, 116, 99, 104, 101, 100, 65, 116, 16, 0, 128, 47, 7, 15, 21, 119, 66, 246, 184, 134, 244, 45, 0, 0, 0, 1, 26, 70, 101, 101, 100, 65, 110, 100, 83, 117, 112, 112, 108, 101, 109, 101, 110, 116, 70, 101, 116, 99, 104, 101, 100, 65, 116, 16, 0, 160, 48, 7, 15, 21, 119, 66, 21, 164, 55, 19, 33, 0, 0, 0, 1, 14, 71, 114, 97, 115, 115, 70, 101, 116, 99, 104, 101, 100, 65, 116, 16, 0, 176, 49, 7, 15, 21, 119, 66, 42, 199, 90, 199, 40, 0, 0, 0, 1, 21, 67, 97, 116, 116, 108, 101, 73, 110, 116, 97, 107, 101, 70, 101, 116, 99, 104, 101, 100, 65, 116, 16, 0, 224, 49, 7, 15, 21, 119, 66, 115, 116, 145, 64, 42, 0, 0, 0, 1, 23, 68, 111, 99, 116, 111, 114, 105, 110, 103, 83, 99, 111, 114, 101, 70, 101, 116, 99, 104, 101, 100, 65, 116, 16, 0, 32, 54, 7, 15, 21, 119, 66, 89, 72, 38, 229, 40, 0, 0, 0, 1, 21, 87, 111, 114, 109, 105, 110, 103, 83, 99, 111, 114, 101, 70, 101, 116, 99, 104, 101, 100, 65, 116, 16, 0, 16, 208, 7, 15, 21, 119, 66, 168, 114, 240, 103, 45, 0, 0, 0, 1, 26, 82, 101, 103, 105, 111, 110, 65, 99, 114, 101, 115, 80, 101, 114, 65, 85, 89, 70, 101, 116, 99, 104, 101, 100, 65, 116, 16, 0, 144, 208, 7, 15, 21, 119, 66, 130, 183, 181, 110, 40, 0, 0, 0, 1, 21, 86, 97, 99, 99, 105, 110, 101, 83, 99, 111, 114, 101, 70, 101, 116, 99, 104, 101, 100, 65, 116, 16, 0, 144, 211, 7, 15, 21, 119, 66, 72, 119, 165, 215, 39, 0, 0, 0, 1, 20, 70, 101, 101, 100, 105, 110, 103, 66, 101, 108, 116, 70, 101, 116, 99, 104, 101, 100, 65, 116, 16, 0, 112, 57, 224, 15, 21, 119, 66, 32, 38, 225, 10, 43, 0, 0, 0, 1, 24, 82, 101, 102, 101, 114, 101, 110, 99, 101, 87, 101, 105, 103, 104, 116, 70, 101, 116, 99, 104, 101, 100, 65, 116, 16, 0, 240, 173, 225, 15, 21, 119, 66, 220, 135, 203, 79, 40, 0, 0, 0, 1, 21, 73, 109, 112, 108, 97, 110, 116, 83, 99, 111, 114, 101, 70, 101, 116, 99, 104, 101, 100, 65, 116, 16, 0, 240, 184, 218, 92, 21, 119, 66, 133, 130, 70, 48, 28, 0, 0, 0, 1, 13, 100, 101, 102, 97, 117, 108, 116, 66, 114, 101, 101, 100, 115, 9, 0, 0, 0, 0, 104, 140, 33, 31, 25, 0, 0, 0, 1, 6, 109, 97, 120, 65, 103, 101, 1, 0, 0, 0, 0, 0, 0, 0, 0, 34, 170, 118, 217, 32, 0, 0, 0, 1, 21, 73, 109, 112, 108, 97, 110, 116, 83, 99, 111, 114, 101, 70, 101, 116, 99, 104, 101, 100, 65, 116, 0, 74, 62, 229, 244, 0, 0, 0, 0, 0, 0, 0, 32, 0, 0, 0, 1, 21, 87, 111, 114, 109, 105, 110, 103, 83, 99, 111, 114, 101, 70, 101, 116, 99, 104, 101, 100, 65, 116, 0, 66, 219, 246, 183, 28, 0, 0, 0, 1, 13, 100, 101, 102, 97, 117, 108, 116, 66, 114, 101, 101, 100, 115, 9, 0, 0, 0, 0, 104, 140, 33, 31, 25, 0, 0, 0, 1, 6, 109, 97, 120, 65, 103, 101, 1, 0, 0, 0, 0, 0, 0, 0, 0, 34, 170, 118, 217, 40, 0, 0, 0, 1, 21, 73, 109, 112, 108, 97, 110, 116, 83, 99, 111, 114, 101, 70, 101, 116, 99, 104, 101, 100, 65, 116, 16, 0, 96, 82, 152, 95, 21, 119, 66, 154, 104, 215, 88, 40, 0, 0, 0, 1, 21, 87, 111, 114, 109, 105, 110, 103, 83, 99, 111, 114, 101, 70, 101, 116, 99, 104, 101, 100, 65, 116, 16, 0, 176, 82, 152, 95, 21, 119, 66, 118, 171, 120, 235, 28, 0, 0, 0, 1, 13, 100, 101, 102, 97, 117, 108, 116, 66, 114, 101, 101, 100, 115, 9, 0, 0, 0, 0, 104, 140, 33, 31, 25, 0, 0, 0, 1, 6, 109, 97, 120, 65, 103, 101, 1, 0, 0, 0, 0, 0, 0, 0, 0, 34, 170, 118, 217, 28, 0, 0, 0, 1, 13, 100, 101, 102, 97, 117, 108, 116, 66, 114, 101, 101, 100, 115, 9, 0, 0, 0, 0, 104, 140, 33, 31, 25, 0, 0, 0, 1, 6, 109, 97, 120, 65, 103, 101, 1, 0, 0, 0, 0, 0, 0, 0, 0, 34, 170, 118, 217]

@stevenspiel
Copy link

@leisim Is it safe to say I need to just wait for the Rust implementation?

@stevenspiel
Copy link

I think the issue is solved for me after restructuring some of my reading/writing to be synchronous. For me, it was a matter of using Future.forEach over Future.wait so it performs the methods sequentially.

@stevenspiel
Copy link

I also needed to delete my old box and start a new one, because I think it was corrupt.

@BasedMusa
Copy link

BasedMusa commented Apr 12, 2020

@stevenspiel @leisim I also have this.

@BasedMusa
Copy link

After checking the debugger, when it reads my first value via the readDouble method, it first checks if it has the available space needed to do this, using the _requireBytes function. That is where the crash occurs,
The code checks if _offset + bytes is more than the bufferLimit or not. At this point, my bufferLimit was 2693 and my buffer was 2697 and my _offset was 153 and bytes was 8. Hope this helps :)

Also, could you please explain how _offset + bytes or 153 + 8 turned out to be more than 2697?

@BasedMusa
Copy link

Update I modified your _requireBytes code to this.

  @pragma('vm:prefer-inline')
  @pragma('dart2js:tryInline')
  void _requireBytes(int bytes) {
    print("OFFSET: $_offset");
    print("BYTES: $bytes");
    print("BUFFER LIMIT: $_bufferLimit");

    print("\n\n$_offset + $bytes > $_bufferLimit IS ${_offset + bytes > _bufferLimit}");
    if (_offset + bytes > _bufferLimit) {
      throw RangeError('Not enough bytes available.');
    }
  }

Here is the last print statement right before it throws the exception:

OFFSET: 223
BYTES: 1128616521
BUFFER LIMIT: 2693

223 + 1128616521 > 2693 IS true

EXCEPTION

Launching lib/main.dart on macOS in debug mode...
Building macOS application...

...
...
...
flutter: OFFSET: 214
flutter: BYTES: 1
flutter: BUFFER LIMIT: 2693
flutter: 

214 + 1 > 2693 IS false
flutter: OFFSET: 215
flutter: BYTES: 4
flutter: BUFFER LIMIT: 2693
flutter: 

215 + 4 > 2693 IS false
Syncing files to device macOS...
flutter: OFFSET: 219
flutter: BYTES: 4
flutter: BUFFER LIMIT: 2693
flutter: 

219 + 4 > 2693 IS false
flutter: OFFSET: 223
flutter: BYTES: 1128616521
flutter: BUFFER LIMIT: 2693
flutter: 

223 + 1128616521 > 2693 IS true
flutter: HiveInterface: Init: UnexpectedError: RangeError: Not enough bytes available.: #0      BinaryReaderImpl._requireBytes (package:hive/src/binary/binary_reader_impl.dart:54:7)
#1      BinaryReaderImpl.viewBytes (package:hive/src/binary/binary_reader_impl.dart:78:5)
#2      BinaryReaderImpl.readString (package:hive/src/binary/binary_reader_impl.dart:140:16)
#3      BinaryReaderImpl.readStringList (package:hive/src/binary/binary_reader_impl.dart:197:17)
#4      BinaryReaderImpl.read (package:hive/src/binary/binary_reader_impl.dart:317:16)
#5      TemplateSequencePropertyAdapter.read (package:based_kit/utils/models.g.dart:177:71)
#6      BinaryReaderImpl.read (package:hive/src/binary/binary_reader_impl.dart:330:33)
#7      BinaryReaderImpl.readMap (package:hive/src/binary/binary_reader_impl.dart:217:21)
#8      BinaryReaderImpl.read (package:hive/src/binary/binary_reader_impl.dart:321:16)
#9      TemplateSequenceOptionAdapter.read (package:based_kit/utils/models.g.dart:122:71)
#10     BinaryReaderImpl.read (package:hive/src/binary/binary_reader_impl.dart:330:33)
#11     BinaryReaderImpl.readMap (package:hive/src/binary/binary_reader_impl.dart:217:21)
#12     BinaryReaderImpl.read (package:hive/src/binary/binary_reader_impl.dart:321:16)
#13     CodeTemplateAdapter.read (package:based_kit/utils/models.g.dart:91:71)
#14     BinaryReaderImpl.read (package:hive/src/binary/binary_reader_impl.dart:330:33)
#15     BinaryReaderImpl.readFrame (package:hive/src/binary/binary_reader_impl.dart:278:26)
#16     FrameHelper.framesFromBytes (package:hive/src/binary/frame_helper.dart:17:26)
#17     FrameIoHelper.framesFromFile (package:hive/src/io/frame_io_helper.dart:41:12)
<asynchronous suspension>
#18     StorageBackendVm.initialize (package:hive/src/backend/vm/storage_backend_vm.dart:82:30)
<asynchronous suspension>
#19     BoxBaseImpl.initialize (package:hive/src/box/box_base_impl.dart:90:20)
#20     HiveImpl._openBox (package:hive/src/hive_impl.dart:90:17)
<asynchronous suspension>
#21     HiveImpl.openBox (package:hive/src/hive_impl.dart:111:18)
#22     HiveInterface.init (package:based_kit/utils/hive_interface.dart:21:18)
<asynchronous suspension>
#23     main (package:based_kit/main.dart:10:23)
#24     _runMainZoned.<anonymous closure>.<anonymous closure> (dart:ui/hooks.dart:241:25)
#25     _rootRun (dart:async/zone.dart:1184:13)
#26     _CustomZone.run (dart:async/zone.dart:1077:19)
#27     _runZoned (dart:async/zone.dart:1619:10)
#28     runZonedGuarded (dart:async/zone.dart:1608:12)
#29     _runMainZoned.<anonymous closure> (dart:ui/hooks.dart:233:5)
#30     _startIsolate.<anonymous closure> (dart:isolate-patch/isolate_patch.dart:301:19)
#31     _RawReceivePortImpl._handleMessage (dart:isolate-patch/isolate_patch.dart:168:12)

@mateusfccp
Copy link

I am also having this issue and can't find how to fix, also related to frameLength being < 8 (in my case, 0).

@RaviKavaiya
Copy link

@leisim @themisir any updates on this? I ran into this issue multiple times. But this seems to be arbitrary to me. I couldn't find a reproducible sample code. But AFAIK, this once happened when I tried to open a box and immediately closing it (and around 5-6 times of HOT RESTARTING the app). That corrupted my whole box (also note that I couldn't again reproduce this).

I have fear that whether this would happen in one of my production apps.

You can see that the same issue is mentioned multiple times.

My request is, please take a look at this issue as this seems to be very critical 🙏

@themisir
Copy link
Contributor

themisir commented Aug 22, 2020

Duplicate of #263

@themisir themisir marked this as a duplicate of #263 Aug 22, 2020
@themisir
Copy link
Contributor

I did not noticed this issue was opened first, let's discuss about the issue on #263

@bnayagrawal
Copy link

Please someone help me solve this issue, Our company app is going to production soon.

@themisir
Copy link
Contributor

themisir commented Nov 2, 2020

Please someone help me solve this issue, Our company app is going to production soon.

Could you share hive code you're using? We don't know exact reason for the issue currently. But if you have the code that reproduces this error it might help us to know what's going wrong. I would check your code in a private session if it would be cool to you, I just want to know what's wrong to solve this issue which is active for months.

@ch-muhammad-adil
Copy link

E/flutter (17289): [ERROR:flutter/lib/ui/ui_dart_state.cc(177)] Unhandled Exception: HiveError: This should not happen. Please open an issue on GitHub.
E/flutter (17289): #0      BinaryReaderImpl.readFrame (package:hive/src/binary/binary_reader_impl.dart:249:7)
E/flutter (17289): #1      FrameHelper.framesFromBytes (package:hive/src/binary/frame_helper.dart:17:26)
E/flutter (17289): #2      FrameIoHelper.framesFromFile (package:hive/src/io/frame_io_helper.dart:41:12)
E/flutter (17289): <asynchronous suspension>
E/flutter (17289): #3      StorageBackendVm.initialize (package:hive/src/backend/vm/storage_backend_vm.dart:82:30)
E/flutter (17289): <asynchronous suspension>
E/flutter (17289): #4      BoxBaseImpl.initialize (package:hive/src/box/box_base_impl.dart:90:20)
E/flutter (17289): #5      HiveImpl._openBox (package:hive/src/hive_impl.dart:106:22)
E/flutter (17289): <asynchronous suspension>
E/flutter (17289): #6      HiveImpl.openBox (package:hive/src/hive_impl.dart:135:18)
E/flutter (17289): #7      _SplashScreenState.initializations (package:alp/screens/splash/splash_screen.dart:60:27)
E/flutter (17289): <asynchronous suspension>
E/flutter (17289): #8      _SplashScreenState.afterFirstLayout (package:alp/screens/splash/splash_screen.dart:224:5)
E/flutter (17289): #9      AfterLayoutMixin.initState.<anonymous closure> (package:alp/helpers/marker_generator.dart:109:38)
E/flutter (17289): #10     SchedulerBinding._invokeFrameCallback (package:flutter/src/scheduler/binding.dart:1117:15)
E/flutter (17289): #11     SchedulerBinding.handleDrawFrame (package:flutter/src/scheduler/binding.dart:1063:9)
E/flutter (17289): #12     SchedulerBinding.scheduleWarmUpFrame.<anonymous closure> (package:flutter/src/scheduler/binding.dart:864:7)
E/flutter (17289): #13     _rootRun (dart:async/zone.dart:1182:47)
E/flutter (17289): #14     _CustomZone.run (dart:async/zone.dart:1093:19)
E/flutter (17289): #15     _CustomZone.runGuarded (dart:async/zone.dart:997:7)
E/flutter (17289): #16     _CustomZone.bindCallbackGuarded.<anonymous closure> (dart:async/zone.dart:1037:23)
E/flutter (17289): #17     _rootRun (dart:async/zone.dart:1190:13)
E/flutter (17289): #18     _CustomZone.run (dart:async/zone.dart:1093:19)
E/flutter (17289): #19     _CustomZone.bindCallback.<anonymous closure> (dart:async/zone.dart:1021:23)
E/flutter (17289): #20     Timer._createTimer.<anonymous closure> (dart:async-patch/timer_patch.dart:18:15)
E/flutter (17289): #21     _Timer._runTimers (dart:isolate-patch/timer_impl.dart:397:19)
E/flutter (17289): #22     _Timer._handleMessage (dart:isolate-patch/timer_impl.dart:428:5)
E/flutter (17289): #23     _RawReceivePortImpl._handleMessage (dart:isolate-patch/isolate_patch.dart:168:12)
E/flutter (17289): 
E/flutter (17289): [ERROR:flutter/lib/ui/ui_dart_state.cc(177)] Unhandled Exception: HiveError: This should not happen. Please open an issue on GitHub.
E/flutter (17289): #0      BinaryReaderImpl.readFrame (package:hive/src/binary/binary_reader_impl.dart:249:7)
E/flutter (17289): #1      FrameHelper.framesFromBytes (package:hive/src/binary/frame_helper.dart:17:26)
E/flutter (17289): #2      FrameIoHelper.framesFromFile (package:hive/src/io/frame_io_helper.dart:41:12)
E/flutter (17289): <asynchronous suspension>
E/flutter (17289): #3      StorageBackendVm.initialize (package:hive/src/backend/vm/storage_backend_vm.dart:82:30)
E/flutter (17289): <asynchronous suspension>
E/flutter (17289): #4      BoxBaseImpl.initialize (package:hive/src/box/box_base_impl.dart:90:20)
E/flutter (17289): #5      HiveImpl._openBox (package:hive/src/hive_impl.dart:106:22)
E/flutter (17289): <asynchronous suspension>
E/flutter (17289): #6      HiveImpl.openBox (package:hive/src/hive_impl.dart:135:18)
E/flutter (17289): #7      _SplashScreenState.initializations (package:alp/screens/splash/splash_screen.dart:60:27)
E/flutter (17289): <asynchronous suspension>
E/flutter (17289): #8      _SplashScreenState.afterFirstLayout (package:alp/screens/splash/splash_screen.dart:224:5)
E/flutter (17289): #9      AfterLayoutMixin.initState.<anonymous closure> (package:alp/helpers/marker_generator.dart:109:38)
E/flutter (17289): #10     SchedulerBinding._invokeFrameCallback (package:flutter/src/scheduler/binding.dart:1117:15)
E/flutter (17289): #11     SchedulerBinding.handleDrawFrame (package:flutter/src/scheduler/binding.dart:1063:9)
E/flutter (17289): #12     SchedulerBinding.scheduleWarmUpFrame.<anonymous closure> (package:flutter/src/scheduler/binding.dart:864:7)
E/flutter (17289): #13     _rootRun (dart:async/zone.dart:1182:47)
E/flutter (17289): #14     _CustomZone.run (dart:async/zone.dart:1093:19)
E/flutter (17289): #15     _CustomZone.runGuarded (dart:async/zone.dart:997:7)
E/flutter (17289): #16     _CustomZone.bindCallbackGuarded.<anonymous closure> (dart:async/zone.dart:1037:23)
E/flutter (17289): #17     _rootRun (dart:async/zone.dart:1190:13)
E/flutter (17289): #18     _CustomZone.run (dart:async/zone.dart:1093:19)
E/flutter (17289): #19     _CustomZone.bindCallback.<anonymous closure> (dart:async/zone.dart:1021:23)
E/flutter (17289): #20     Timer._createTimer.<anonymous closure> (dart:async-patch/timer_patch.dart:18:15)
E/flutter (17289): #21     _Timer._runTimers (dart:isolate-patch/timer_impl.dart:397:19)
E/flutter (17289): #22     _Timer._handleMessage (dart:isolate-patch/timer_impl.dart:428:5)
E/flutter (17289): #23     _RawReceivePortImpl._handleMessage (dart:isolate-patch/isolate_patch.dart:168:12)
E/flutter (17289): 

@kmcgill88
Copy link

After upgrading to flutter 2 this started to happen again. I wasn't using hive_flutter but doing init myself. Turns out I was calling init more than once for the app! Once I moved the hive init to my main to be called once app-wide, that fixed it.

Flutter 2
hive: ^1.6.0-nullsafety.2

@ch-muhammad-adil
Copy link

After upgrading to flutter 2 this started to happen again. I wasn't using hive_flutter but doing init myself. Turns out I was calling init more than once for the app! Once I moved the hive init to my main to be called once app-wide, that fixed it.

Flutter 2
hive: ^1.6.0-nullsafety.2

It has helped..

@VJag
Copy link

VJag commented Dec 9, 2021

Hi All,

We are facing the same issue. i.e Unhandled Exception: HiveError: This should not happen. Please open an issue on GitHub.

May I propose the following changes in the implementation so that crash recovery can be leveraged?

Proposed changes:

Change 1. Introduce a new Error specific to Box corruption that extends HiveError and this will have a field to set the offset of the invalid frame, and the value for this should be the same as recoveryOffset in StorageBackendVm

class HiveBoxCorruptionError extends HiveError {

// offSet of the frame that caused the error
final int offset;

/// Create a new Hive error (internal)
HiveBoxCorruptionError(this.message, this.offset) {
super(message);
}
}

Change 2. Changes in "StorageBackendVm" to catch HiveBoxCorruptionError and make use of the offset

try {

  if (!lazy) {
    recoveryOffset =
    await _frameHelper.framesFromFile(path, keystore, registry, _cipher);
  } else {
    recoveryOffset = await _frameHelper.keysFromFile(path, keystore, _cipher);
  }

} on HiveBoxCorruptionError catch(error) {
     if(_crashRecovery) {
         recoveryOffset = error.offSet;
      } else {
         rethrow;
      }
}

Please let me know your thoughts?

Change 3: Let BinaryReaderImpl throw HiveBoxCorruptionError when frameLength is < 8 etc..

Note: Please don't give too much attention to the syntax. I hope the core idea is clear.

@yakubmoris
Copy link

yakubmoris commented Feb 1, 2023

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?

@neiljaywarner
Copy link

@yakubmoris what did you end up doing?

@iosephmagno
Copy link

We get this issue as well. How can we at least know the name of the corrupted box?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests