Skip to content

Commit cd29d0c

Browse files
committed
fix(core): prevent race condition in dispose method
Updates the `dispose` method in `AppDependencies` to await the completion of the `_initCompleter` before proceeding with resource cleanup. This change prevents a race condition where `dispose()` could be called while `_initializeDependencies()` is still executing, which would lead to unpredictable errors from tearing down resources that are actively being initialized. The `dispose` method now safely waits for initialization to finish, whether it succeeded or failed, ensuring a clean and predictable shutdown sequence.
1 parent aa55e93 commit cd29d0c

File tree

1 file changed

+12
-0
lines changed

1 file changed

+12
-0
lines changed

lib/src/config/app_dependencies.dart

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -306,6 +306,18 @@ class AppDependencies {
306306
return;
307307
}
308308

309+
// Wait for initialization to complete before disposing resources.
310+
// This prevents a race condition if dispose() is called during init().
311+
try {
312+
await _initCompleter!.future;
313+
} catch (_) {
314+
// Initialization may have failed, but we still proceed to dispose
315+
// any partially initialized resources.
316+
_log.warning(
317+
'Disposing dependencies after a failed initialization attempt.',
318+
);
319+
}
320+
309321
_log.info('Disposing application dependencies...');
310322
await _mongoDbConnectionManager.close();
311323
tokenBlacklistService.dispose();

0 commit comments

Comments
 (0)