isar / isar/hive

Unable to delete corrupt box

Open
#989 4 comments 4 reactions 0 assignees View on GitHub
problem
Dominant language
Dart
Stars
4.4k
Forks
449
PR merge metrics
No merged PRs in 30d

Description

**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 {
@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
('box');
await box.put('foo', A(1));
await box.close();

await Hive.openBox('box').onError((error, stackTrace) async {
await Hive.deleteBoxFromDisk('box');
return Hive.openBox
('box');
});
});

test('LazyBox', () async {
final box = await Hive.openLazyBox
('lazyBox');
await box.put('foo', A(1));
await box.close();

await Hive.openLazyBox('lazyBox').onError((error, stackTrace) async {
await Hive.deleteBoxFromDisk('lazyBox');
return Hive.openLazyBox
('lazyBox');
});
});
}

```

**Version**
- Platform: n/a (running unit tests on Linux)
- Flutter version: 3.0.1
- Hive version: 2.2.1

Contributor guide

No contributing guide indexed for this repository

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.