hive
hive copied to clipboard
Unable to delete corrupt box
Steps to Reproduce
If a call to Hive.openBox
fails (due to eg. file corruption, buggy adapters, etc.) then it is not possible to delete the box.
Hive.deleteBoxFromDisk
fails with FileSystemException: Cannot delete file, path = 'XXX.lock' (OS Error: No such file or directory, errno = 2)
.
This seems to be due to a race condition as when using Hive.openLazyBox
, the box deletes successfully even if opening fails.
PS It also seems like deleteBoxFromDisk
doesn't remove the box from it's internal collection of boxes (HiveImpl._boxes
and HiveImpl._openingBoxes
). It seems like it really should do that, or am I missing something?
Code sample
Here are two tests that demonstrate the problem:
import 'dart:io';
import 'package:hive/hive.dart';
import 'package:test/test.dart';
class A {
final int v;
A(this.v);
}
class ABrokenAdapter extends TypeAdapter<A> {
@override
final int typeId = 0;
@override
A read(BinaryReader reader) => A(reader.readInt());
@override
void write(BinaryWriter writer, A obj) {};
}
void main() async {
setUpAll(() {
final dir = Directory('hive_test');
if (dir.existsSync()) dir.deleteSync(recursive: true);
Hive
..init('hive_test')
..registerAdapter(ABrokenAdapter());
});
test('Box', () async {
final box = await Hive.openBox<A>('box');
await box.put('foo', A(1));
await box.close();
await Hive.openBox<A>('box').onError((error, stackTrace) async {
await Hive.deleteBoxFromDisk('box');
return Hive.openBox<A>('box');
});
});
test('LazyBox', () async {
final box = await Hive.openLazyBox<A>('lazyBox');
await box.put('foo', A(1));
await box.close();
await Hive.openLazyBox<A>('lazyBox').onError((error, stackTrace) async {
await Hive.deleteBoxFromDisk('lazyBox');
return Hive.openLazyBox<A>('lazyBox');
});
});
}
Version
- Platform: n/a (running unit tests on Linux)
- Flutter version: 3.0.1
- Hive version: 2.2.1
Any Updates on this?
I just ran into this, it's a big issue I think