Skip to content

Commit ce46d2f

Browse files
committed
fix: show error screen instead of black screen when second instance launched
1 parent 243015b commit ce46d2f

File tree

1 file changed

+25
-2
lines changed

1 file changed

+25
-2
lines changed

lib/main.dart

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -178,8 +178,31 @@ void main(List<String> args) async {
178178
(await StackFileSystem.applicationHiveDirectory()).path,
179179
);
180180

181-
await DB.instance.hive.openBox<dynamic>(DB.boxNameDBInfo);
182-
await DB.instance.hive.openBox<dynamic>(DB.boxNamePrefs);
181+
try {
182+
await DB.instance.hive.openBox<dynamic>(DB.boxNameDBInfo);
183+
await DB.instance.hive.openBox<dynamic>(DB.boxNamePrefs);
184+
} on FileSystemException catch (e) {
185+
if (e.osError?.errorCode == 11 || e.message.contains('lock failed')) {
186+
// Another instance of the app already holds the Hive database lock.
187+
// Show a simple error screen rather than crashing to a black screen.
188+
runApp(
189+
MaterialApp(
190+
debugShowCheckedModeBanner: false,
191+
home: Scaffold(
192+
body: Center(
193+
child: Text(
194+
'${AppConfig.appName} is already running.\n'
195+
'Close the other window and try again.',
196+
textAlign: TextAlign.center,
197+
),
198+
),
199+
),
200+
),
201+
);
202+
return;
203+
}
204+
rethrow;
205+
}
183206
await Prefs.instance.init();
184207

185208
await Logging.instance.initialize(

0 commit comments

Comments
 (0)