hive
hive copied to clipboard
' (OS Error: The process cannot access the file because another process has locked a portion of the file. , errno = 33)
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:
- Check if the box is legacy, maybe the contents are incompatible with newer versions of the app.
- I've wrapped the open box method with a try-catch
- The catch is triggered since the box is now incompatible
- 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<void> main() async {
Directory directory = await pathProvider.getApplicationDocumentsDirectory();
final logsAppPath = Directory('${directory.path}/logsApp');
Hive.init(logsAppPath.path);
Hive.registerAdapter(LogAdapter());
try {
await Hive.openBox<Log>('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<Log>('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]
any updates on this? I'm facing exactly the same issue
If you look at the stack trace, there are a lot of asynchronous gaps.
I think dart throws the FileSystemException
later and the catch block is somehow lost in the loop.
Making this change in lib\src\backend\vm\storage_backend_vm.dart
, line 82, catches the FileSystemException
:
try {
await lockRaf.lock();
} on FileSystemException catch (e) {
/// handle e
}
same issue ... would be nice to have the possibility to react on this error
please update on this.
Please help, I'm facing the same issue.
async* resolved my issue. It will handle multiple process.