Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Initialization error #6

Closed
patricknicolosi opened this issue Feb 14, 2022 · 17 comments
Closed

Initialization error #6

patricknicolosi opened this issue Feb 14, 2022 · 17 comments

Comments

@patricknicolosi
Copy link
Contributor

During VerificaC19.initialize(), It happens to have this error HiveError: This should not happen. Please open an issue on Github

@mastro993
Copy link
Owner

Hi @patricknicolosi,

can you share a full stacktrace of the error? Thank you.

@patricknicolosi
Copy link
Contributor Author

patricknicolosi commented Feb 14, 2022

Unfortunately I can't reproduce the error, this is the only message I got at the moment. I'll try again later

@mastro993
Copy link
Owner

This is probably an error caused by a bad state of the Hive data when the app is closed/arrested. I'll add a check to avoid this error.

@patricknicolosi
Copy link
Contributor Author

patricknicolosi commented Feb 14, 2022

A more specific error

flutter : [ERROR:flutter/lib/ui/ui_dart_state.cc(209)] Unhandled Exception: HiveError: This should not happen. Please open an issue on GitHub.
flutter : #0 BinaryReaderImpl.readFrame (package:hive/src/binary/binary_reader_impl.dart:250)
flutter : #1 FrameHelper.framesFromBytes (package:hive/src/binary/frame_helper.dart:21)
flutter : #2 FrameIoHelper.framesFromFile (package:hive/src/io/frame_io_helper.dart:42)
flutter :
flutter : #3 StorageBackendVm.initialize (package:hive/src/backend/vm/storage_backend_vm.dart:86)
flutter :
flutter : #4 HiveImpl._openBox (package:hive/src/hive_impl.dart:111)
flutter :
flutter : #5 HiveImpl.openBox (package:hive/src/hive_impl.dart:139)
flutter :
flutter : #6 LocalRepositoryImpl.create (package:verificac19/src/data/local/local_repository_impl.dart:25)
flutter :
flutter : #7 $initGetIt (package:verificac19/src/di/injection.config.dart:36)
flutter :
flutter : #8 configureInjection (package:verificac19/src/di/injection.dart:10)
flutter :
flutter : #9 VerificaC19Impl.initialize (package:verificac19/src/verificac19_impl.dart:11)

@mastro993
Copy link
Owner

Hi @patricknicolosi, I added a workaround for this problem in the 1.3.3 release.
Note that this problem may be caused by an access to the library function from different isolates. The library used for local rules storages, Hive, does not support shared memory between isolates and this may cause data corruption.
Look here: isar/hive#676 (comment)

If this problem is not resolved I'll look to switch to another local storage solution.

@patricknicolosi
Copy link
Contributor Author

Purtroppo si è verificato ancora questo problema

@mastro993
Copy link
Owner

Ciao @patricknicolosi. Sapresti dirmi come poter ricreare il problema? Hai modo di capire quali sono le azioni che portano al problema oppure è casuale/si verifica in situazioni differenti?
Il problema sembrerebbe relativo alla corruzione dei dati dell'internal storage.

@mastro993 mastro993 reopened this Feb 21, 2022
@patricknicolosi
Copy link
Contributor Author

patricknicolosi commented Feb 21, 2022

Probabilmente questo succede perchè effettuo l'aggiornamento in background con workmanager da un altro isolato. Anche se questo errore è imprevedibile/casuale. A volte facendo l'aggiornamento in background infatti non riscontro alcun errore.

Utilizzando shared_preferences si potrebbe risolvere questo errore?

@mastro993
Copy link
Owner

Per lo storage della CRL è necessario usare un DB efficiente. Fare una ricerca di una valore all'interno di una lista di decine di migliaia di risultati salvati sulle Shared Preferences è altamente inefficiente, in più non ti garantiscono che il dato sia realmente persistente nel tempo.

L'errore che riscontri è dovuto al fatto che accedi allo storage da differenti isolati durante operazioni di scrittura. Non c'è problema nel fare l'aggiornamento in background, però assicurati che non vi siano accessi simultanei!

Usa dei semafori/mutex. Dovresti risolvere il problema.
Nel mentre l'ascio l'issue aperto e cerco di trovare soluzioni/workaround da implementare direttamente nel package.

@patricknicolosi
Copy link
Contributor Author

Un'altra idea non potrebbe essere quella di avere un metodo del tipo resetVerificaC19(). In modo da avere nella schermata di inizializzazione qualcosa del tipo:

Schermata di inizializzazione

bool verificaC19Error = false;

try{
  VerificaC19.initialize();
}catch(e){
  verificaC19Error = true;
}

Schermata di scansione

initState(){
  if(verificaC19Error){
     resetVerificaC19().then((_){
       VerificaC19.initialize();
     })
  }  
}

Questo metodo dovrebbe resettare tutte le regole del db, in modo da permettere di chiamare VerificaC19.initialize() e di effettuare un aggiornamento.

@mastro993
Copy link
Owner

mastro993 commented Feb 21, 2022

Un metodo per il teardown forzato è una buona idea e ci si potrebbe lavorare.

Fare un try/catch ti risolve l'errore all'apparenza, perché andresti in contro alla necessità scaricare le regole più e più volte e il concetto di cache andrebbe a farsi benedire! 🤣

Guarda l'esempio che ho fatto qui: https://github.com/mastro993/greenguard-app
Ho creato una semplicissima classe wrapper che mi consente di mantenere lo stato e sapere se il package è inizializzato e pronto alla verifica, e tutto sullo stesso isolate.
VerificaC19 è inizializzato nel main e non viene più richiamato. L'update è gestito in HomeBloc.
Ho usato BLoC ma puoi avere lo stesso risultato con ChangeNotifier/Provider, RiverPod o con MobX.

@patricknicolosi
Copy link
Contributor Author

patricknicolosi commented Feb 21, 2022

Era un esempio stupidissimo solo per capire l'utilità di quella funzione 🤣. Comunque grazie mille ! Un metodo di teardown sarebbe comunque una cosa molto comoda!

@patricknicolosi
Copy link
Contributor Author

patricknicolosi commented Feb 26, 2022

Non so se sia utile, in ogni caso ho trovato un altro errore, anche questo in maniera casuale. Non credo che l'errore possa essere causato dalla mia app, in ogni caso ciò che faccio è semplicemente

FutureBuilder<DateTime?>(
                          future: VerificaC19.getLastUpdateTime(),
                          builder: (context, snapshot) {
                            return snapshot.hasData
                                ? Text(
                                    "Ultimo aggiornamento: ${DateFormat("dd-MM-yyyy kk:mm").format(snapshot.data ?? DateTime.now())}",
                                    style: GoogleFonts.rubik())
                                : Text(
                                    "Aggiornamento disponibile",
                                    style: GoogleFonts.rubik(
                                      color: Colors.teal,
                                    ),
                                  );
                          },
                        )

Questo l'errore che ottengo

02-26 19:17:33.069 16111 16137 E flutter : [ERROR:flutter/lib/ui/ui_dart_state.cc(209)] Unhandled Exception: Null check operator used on a null value
02-26 19:17:33.069 16111 16137 E flutter : #0 _GetItImplementation._findFactoryByNameAndType (package:get_it/get_it_impl.dart:380)
02-26 19:17:33.069 16111 16137 E flutter : #1 _GetItImplementation.get (package:get_it/get_it_impl.dart:393)
02-26 19:17:33.069 16111 16137 E flutter : #2 _GetItImplementation.call (package:get_it/get_it_impl.dart:430)
02-26 19:17:33.069 16111 16137 E flutter : #3 VerificaC19Impl.needsUpdate (package:verificac19/src/verificac19_impl.dart:16)
02-26 19:17:33.069 16111 16137 E flutter : #4 VerificaC19Updater.showVerificaC19MaterialBanner (package:greenpassreader/models/verifica_c19_updater.dart:11)
02-26 19:17:33.069 16111 16137 E flutter : #5 _CodeScannerScreenState._verificaC19update. (package:greenpassreader/screens/code_scanner_screen.dart:998)
02-26 19:17:33.069 16111 16137 E flutter : #6 new Future.delayed. (dart:async/future.dart:393)
02-26 19:17:33.069 16111 16137 E flutter : #7 _rootRun (dart:async/zone.dart:1418)
02-26 19:17:33.069 16111 16137 E flutter : #8 _CustomZone.run (dart:async/zone.dart:1328)
02-26 19:17:33.069 16111 16137 E flutter : #9 _CustomZone.runGuarded (dart:async/zone.dart:1236)
02-26 19:17:33.069 16111 16137 E flutter : #10 _CustomZone.bindCallbackGuarded. (dart:async/zone.dart:1276)
02-26 19:17:33.069 16111 16137 E flutter : #11 _rootRun (dart:async/zone.dart:1426)
02-26 19:17:33.069 16111 16137 E flutter : #12 _CustomZone.run (dart:async/zone.dart:1328)
02-26 19:17:33.069 16111 16137 E flutter : #13 _CustomZone.bindCallback. (dart:async/zone.dart:1260)
02-26 19:17:33.069 16111 16137 E flutter : #14 Timer._createTimer. (dart:async-patch/timer_patch.dart:18)
02-26 19:17:33.069 16111 16137 E flutter : #15 _Timer._runTimers (dart:isolate-patch/timer_impl.dart:395)
02-26 19:17:33.069 16111 16137 E flutter : #16 _Timer._handleMessage (dart:isolate-patch/timer_impl.dart:426)
02-26 19:17:33.069 16111 16137 E flutter : #17 _RawReceivePortImpl._handleMessage (dart:isolate-patch/isolate_patch.dart:192)

@mastro993
Copy link
Owner

Ciao @patricknicolosi, l'errore è causato dal metodo needsUpdate() che probabilmente viene chiamato prima dell'inizializzazione.
Controlla che nella tua app non ci siano problemi di concorrenza. Questo problema e quello precedente mi fanno pensare a questo.

@mastro993
Copy link
Owner

E' disponibile la versione 1.5.0-dev.1
E' stato sostituito Hive con Isar, che supporta l'accesso da differenti isolates.
Attualmente è in pre-release in attesa di ulteriori test! :)

@patricknicolosi
Copy link
Contributor Author

E' disponibile la versione 1.5.0-dev.1
E' stato sostituito Hive con Isar, che supporta l'accesso da differenti isolates.
Attualmente è in pre-release in attesa di ulteriori test! :)

Un'ottima notizia! Farò sicuramente dei test e grazie tante!

@mastro993
Copy link
Owner

Dagli ultimi test non mi risultano problemi. Chiudo l'issue, nel caso dovesse ricapitare si riapre :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants