hive icon indicating copy to clipboard operation
hive copied to clipboard

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

Open bossbeagle1509 opened this issue 3 years ago • 6 comments

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<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]

bossbeagle1509 avatar Sep 10 '21 09:09 bossbeagle1509

any updates on this? I'm facing exactly the same issue

umang-sinha avatar Jan 24 '22 20:01 umang-sinha

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
    }

dhruvd0 avatar Apr 07 '22 14:04 dhruvd0

same issue ... would be nice to have the possibility to react on this error

filly82 avatar Oct 25 '22 21:10 filly82

please update on this.

selvam920 avatar Jul 13 '23 03:07 selvam920

Please help, I'm facing the same issue.

imshobhitbajpai avatar Aug 26 '23 14:08 imshobhitbajpai

async* resolved my issue. It will handle multiple process.

benevolent13 avatar Sep 14 '23 05:09 benevolent13