isar / isar/hive

' (OS Error: The process cannot access the file because another process has locked a portion of the file. , errno = 33)

Open
#782 6 comments 0 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**
Ok, so this is going to be a little tough. The project where I'm running into this issue is open source. Here's the gist:

1) Check if the box is legacy, maybe the contents are incompatible with newer versions of the app.
2) I've wrapped the open box method with a try-catch
3) The catch is triggered since the box is now incompatible
4) I decide to delete the old box and create a new empty one for now. I get an error saying,

"C:\Users\fpson\Documents/logsApp\logs.lock' (OS Error: The process cannot access the file because another process has locked a portion of the file.
, errno = 33))"

What would help is this: when Hive fails to open a box since the `TypeAdapter` finds values that don't exist in the old box, and I use try-catch, will it not work if I try to delete the box within the catch ?

The reason being, I did some poking around and found that even if I try to delete the directory using `Directory('the-directory-path').delete()` where the hive files are stored then I get the exact same error. So it's not in particular a `Hive` error.

Maybe you can give me some insight as to what happens when a box fails to open ?

**Code sample**
```
Future main() async {
Directory directory = await pathProvider.getApplicationDocumentsDirectory();
final logsAppPath = Directory('${directory.path}/logsApp');
Hive.init(logsAppPath.path);
Hive.registerAdapter(LogAdapter());

try {
await Hive.openBox('logs');
} catch (e) {
// Close all open boxes before proceeding
await Hive.close();

// Breaking changes introduced have
// caused the box to fail to load.
// So delete it.

try {
await Hive.deleteBoxFromDisk('logs');
} catch (e) {
// Didn't work !
// Delete all open boxes.
await Hive.deleteFromDisk();
}

// Then create a new one.
await Hive.openBox('logs'); // the debugger stops here and throws the aforementioned error
}

await Hive.openBox('settings');

isDarkModeEnabled.value =
Hive.box('settings').get('darkMode', defaultValue: true);

runApp(const MyApp());
}
```

**Version**
- Platform: Windows
- Flutter version: [Flutter (Channel master, 2.6.0-1.0.pre.191, on Microsoft Windows [Version 10.0.19043.1165], locale en-US) ]
- Hive version: [^2.0.4]

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.