felangel / bloc

A predictable state management library that helps implement the BLoC design pattern
https://bloclibrary.dev
MIT License
11.82k stars 3.39k forks source link

fix: [hydrated_bloc] hydrated_box.lock The process cannot access the file because another process has locked a portion of the file. #3518

Open kateile opened 2 years ago

kateile commented 2 years ago

Description I can't run two apps simultaneously on windows which both use hydrated_bloc. I think this is because I use the default storage HydratedStorage which under the hood utilize hive box named hydrated_box and maybe these two apps can't write and read simultaneously using the same box.

I think best way to solve this is to pass custom hive box name on HydratedStorage.build(...) with something like

  static Future<HydratedStorage> build({
    required Directory storageDirectory,
    HydratedCipher? encryptionCipher,
    String? boxName,
  })

Steps To Reproduce Run two apps which both use HydratedStorage. some thing like

void main() async {
  WidgetsFlutterBinding.ensureInitialized();
  HydratedBloc.storage = await HydratedStorage.build(
    storageDirectory: kIsWeb
        ? HydratedStorage.webStorageDirectory
        : await getTemporaryDirectory(),
  );

  runApp(const App());
}

Expected Behavior User may be able to run many apps simultaneously made separately by lazy developers (who are lazy enough to implement custom storage).

Screenshots no just blank screen

Additional Context This is error log

[ERROR:flutter/lib/ui/ui_dart_state.cc(198)] Unhandled Exception: FileSystemException: lock failed, path = 'C:\Users\TEMBOM~1\AppData\Local\Temp\hydrated_box.lock' (OS Error: The process cannot access the file because another process has locked a portion of the file.

, errno = 33)

0 _RandomAccessFile.lock. (dart:io/file_impl.dart:1002:9)

1 _rootRunUnary (dart:async/zone.dart:1434:47)

2 _CustomZone.runUnary (dart:async/zone.dart:1335:19)

#3 StorageBackendVm.initialize (package:hive/src/backend/vm/storage_backend_vm.dart:81:5) #4 HiveImpl._openBox (package:hive/src/hive_impl.dart:111:9) #5 HiveImpl.openBox (package:hive/src/hive_impl.dart:142:12) #6 HydratedStorage.build. (package:hydrated_bloc/src/hydrated_storage.dart:101:15) #7 BasicLock.synchronized (package:synchronized/src/basic_lock.dart:33:16) #8 startApp (package:graph/utils/start_app.dart:20:26) #9 main (package:online/main.dart:8:3) [ERROR:flutter/lib/ui/ui_dart_state.cc(198)] Unhandled Exception: FileSystemException: lock failed, path = 'C:\Users\TEMBOM~1\AppData\Local\Temp\hydrated_box.lock' (OS Error: The process cannot access the file because another process has locked a portion of the file. , errno = 33) #0 _RandomAccessFile.lock. (dart:io/file_impl.dart:1002:9) #1 _rootRunUnary (dart:async/zone.dart:1434:47) #2 _CustomZone.runUnary (dart:async/zone.dart:1335:19) #3 StorageBackendVm.initialize (package:hive/src/backend/vm/storage_backend_vm.dart:81:5) #4 HiveImpl._openBox (package:hive/src/hive_impl.dart:111:9) #5 HiveImpl.openBox (package:hive/src/hive_impl.dart:142:12) #6 HydratedStorage.build. (package:hydrated_bloc/src/hydrated_storage.dart:101:15) #7 BasicLock.synchronized (package:synchronized/src/basic_lock.dart:33:16) #8 startApp (package:graph/utils/start_app.dart:20:26) #9 main (package:online/main.dart:8:3) [ERROR:flutter/lib/ui/ui_dart_state.cc(198)] Unhandled Exception: FileSystemException: Cannot delete file, path = 'C:\Users\TEMBOM~1\AppData\Local\Temp\hydrated_box.lock' (OS Error: The process cannot access the file because it is being used by another process. , errno = 32) #0 _File._delete. (dart:io/file_impl.dart:283:9) #1 _rootRunUnary (dart:async/zone.dart:1434:47) #2 _CustomZone.runUnary (dart:async/zone.dart:1335:19) #3 StorageBackendVm._closeInternal (package:hive/src/backend/vm/storage_backend_vm.dart:215:5) #4 BoxBaseImpl.close (package:hive/src/box/box_base_impl.dart:173:5)
andrewsheridan commented 11 months ago

I'm also seeing this issue. Figured I would comment to "bump" the issue, since this thread hasn't seen any activity for over a year. Would love to see a resolution for this.

ahmed-osama-saad commented 2 months ago

I get this issue often in development, with a single app. On a physical device when i use the rerun option in android studio.