-
Notifications
You must be signed in to change notification settings - Fork 408
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
Unhandled Exception: HiveError: This should not happen. Please open an issue on GitHub. #263
Comments
When this error happend even i restart app the error still exist |
I run into the same error, also with hive: ^1.4.1+1
|
For the moment, I do this as a temporary workaround. try {
_box = await Hive.openBox(_boxName);
} catch(err) {
/// box could not be opened.
bool success = false;
int index = 0;
while(success == false) {
_boxName = _boxName + index.toString();
Error _err;
try {
_box = await Hive.openBox(_boxName);
} catch(err) {
_err = err;
}
if(_err == null) {
print("Hive box opened with name " + _boxName);
success = true;
} else if(index > 100) {
throw("Hive box could not be opened.");
}
index++;
}
} In this provisional way, subfollowing initializations shall find the same new working box name. However, what is the recommended way to unlock and re-open – or delete – the former box file (in the snippet above "_boxName" without added index) ? |
Thanks to @Mr-Martini I can now reproduce the issue. The issue is caused after using key on unencrypted box or not using key on encrypted box. To fix the issue we might limit I just wonder is there any other cases that might cause this issue rather than not using encryption key or using invalid key? import 'dart:io';
import 'dart:typed_data';
import 'package:hive/hive.dart';
/// Runs some tests
///
/// 1. Opens the box
/// 2. Reads some series of values
/// 3. Writes new values
/// 4. Closes
Future<void> test(int index, [Uint8List key]) async {
var boxName = 'box_$index';
var hasKey = key != null;
var info = '(box = $boxName, hasKey = $hasKey)';
// open
Box box;
try {
box = await Hive.openBox(boxName, encryptionKey: key);
} catch (error) {
return print('Crashed! $info');
}
// read
for (int i = 0; i < 100; i++) {
var value = box.get('item_$i');
if (value != null && value != i) {
print('Item: item_$i Value: $value Expected: $i $info');
}
}
// write
for (int i = 0; i < 100; i++) {
await box.put('item_$i', i);
}
// close
await box.close();
print('OK. $info');
}
void main() async {
final path = Directory.current.path + '/db';
Directory(path)
..deleteSync(recursive: true)
..createSync();
Hive.init(path);
var key = Hive.generateSecureKey();
await test(1, null); // OK
await test(1, null); // OK
await test(1, key); // OK, Recovering corrupted box.
await test(1, null); // Crashes!
} |
I just started using hive for the first time and started getting this issue. Is there any solution to this. I just reinstalled my app and that fixed. |
I did get this exception a few times from users in my crash reporting and I don't use encryption at all. |
I'm getting the same error. I'm using the background_fetch plugin and I'm re-initializing my Hive boxes on it's
|
I have the same error without use encryption to open box. |
I have the same error, but only run the emulator in android 7. When running the emulator in android 9.0 he works very well. Is this anything with the android version? |
I also have the same error, without using encrypted boxes. Happens when I open a box on start of the app. |
This issue has been automatically closed because there has been no response to our request for more information from the original author. With only the information that is currently in the issue, we don't have enough information to take action. Please reach out if you have or find the answers we need so that we can investigate further. |
Hi, I am facing the same issue. It works only for a few initial calls. I am using regular boxes without any encryption. I create a box called 'x' and put a few custom objects for which i have registered the adapters generated by Hive. After saving the data I navigate to another screen, then open the same box, retrieve the data, and it works. But then I navigate back to the initial screen and try to open the box again, and get this: Do you have any idea what might be happening or provide a possible workaround? I am using Hive in a production ready app, and we are adding offline caching for some features. |
First of all, thank you for providing a good library. I have report about this error in Sentry. The difference between my problem and others is that the problem number of typeId is 18. I'm on my last try I've been using Hive for a long time with no problems, but I haven't been able to resolve this issue since it happened and I'm losing hope.
Edited on 2021-11-24 16:32 |
Hey @CreamByul , Have you modified your models before publishing the update? By update I mean have you did one or more of those options:
|
Thanks for the reply @themisir,
Finally, the model with 3 fields added and deleted operations is not a buggy Box. This is the list of versions I use. Thank you for quick response. |
Got the same error.
Or without encryption:
|
In my case I did modify one of the existing id. What I had to do was to wrap the problematic openBox with try/catch and on HiveError I refreshed the box by delete the box from the disk and reopen the box (Do this only if the data persisted in the box are not important). |
We have the same error, it fails randomly only on android devices until now. We do not have isolate or any other background handlers. |
After digging a lot into hive and these corruption errors, it turns out that hive never itself invokes Made a pull request #852 to add a So, why make |
how to recover data from this corrupted box? |
the box being corrupted you can't really. thus you have to prevent corruptions from happening in the first place instead |
Hi, 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 /// Create a new Hive error (internal) Change 2. Changes in "StorageBackendVm" to catch HiveBoxCorruptionError and make use of the offset try {
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. @leisim Please let me know your thoughts? |
App is running fine for around 5 months. |
Same problem here. This (strange) workaround worked for me, thanks. :-) |
Encountered this issues, and it remains even after I rollback my codes to previous versions. Tried different ways above but did not work. Finally I changed the flutter sdk version and it seems working. The 'Type 18' issue and 'this should not happened' issue has not appeared ever since -- I hope it will not appear any more. Call-out: I HAVE NOT MIGRATED TO SOUND NULL-SAFTY YET, that's why I can change the sdk version to lower. in pubyaml.spec, I changed the sdk version from >=2.7.0 to >=2.0.0;
then flutter pub get !!!EDIT: This does not work. My App crashed in internal testing. |
Hi Friends, |
Is this project maintained anymore ?! |
Yeah it is. But you might ask why this issue haven't yet fixed? Well, because we didn't found any root cause. I did spend quite a bit of time to try doing lots of stuff with it but didn't found any way to reproduce. This issue usually occurs when models modified without respecting previous model state, or when device kills app during saving process which in both cases causes database file to get corrupted. I can't fix it if I don't know what really causes this issue, maybe there's something wrong in this library causes this issue. But I myself used hive in production for about 2 year in multiple apps and haven't encountered with this issue. So I really don't know what else can I do for now. If anyone does have any suggestion, I would be happy to work on it. |
I tell you that this is happening to me in production on many phones in the A50, A70 sasumng. |
Hello guys, I made a PR to resolve this issue #914 Let me know if it works. |
Fix published with hive v2.0.6. |
hive version: "1.4.0+1"
This happen when my app crash and restart immediately by android system
The text was updated successfully, but these errors were encountered: