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

Code is sometimes run twice on app start, sometimes there is a crash #10

Open
leaf-node opened this issue Nov 13, 2024 · 3 comments
Open

Comments

@leaf-node
Copy link
Contributor

leaf-node commented Nov 13, 2024

Sometimes when starting the app in debug mode, I see the following messages. Similar to an old firebase issue, I'm seeing a doubling of threads in the call stack in VS Codium:

I/FlutterBootReceiverPlugin(16753): onAttachedToEngine
I/FlutterBackgroundExecutor(16753): Starting BootHandlerService...
I/FlutterBootReceiverPlugin(16753): onAttachedToEngine
Connecting to VM Service at ws://127.0.0.1:39249/1kDvHmBMVCk=/ws
Connected to the VM Service.
I/BootHandlerService(16753): BootHandlerService started!
I/FlutterBackgroundExecutor(16753): Executing Dart callback: -7767485495945997519...
W/BootHandlerService(16753): Attempted to start a duplicate background isolate. Returning...

Screenshot From 2024-11-13 15-55-27

I'm not sure that this is the right way to call this library, so I may experiment with this a bit.

/*
 * SPDX-FileCopyrightText: 2024 Open Alert Viewer authors
 *
 * SPDX-License-Identifier: MIT
 */

import 'dart:async';
import 'dart:io';

import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_boot_receiver/flutter_boot_receiver.dart';
import 'package:yaml/yaml.dart';

import 'app.dart';
import 'app/data_source/database.dart';
import 'background/background.dart';

Future<void> main() async {
  WidgetsFlutterBinding.ensureInitialized();
  if (Platform.isAndroid) {
    await BootReceiver.initialize(startApp);
  }
  await startApp();
}

@pragma('vm:entry-point')
Future<void> startApp() async {
  WidgetsFlutterBinding.ensureInitialized();
  var db = LocalDatabase();
  await db.migrate(showPath: true);
  var bgWorker = BackgroundWorker();
  String appVersion = await getVersion();
  await bgWorker.spawn(appVersion: appVersion);
  runApp(OAVapp(appVersion: appVersion, db: db, bgWorker: bgWorker));
}

Another time, I saw the following error message after stopping and trying to restart the app in debug mode:

Launching lib/main.dart on sdk gphone64 x86 64 in debug mode...
✓ Built build/app/outputs/flutter-apk/app-debug.apk
I/FlutterBootReceiverPlugin(17265): onAttachedToEngine
I/FlutterBackgroundExecutor(17265): Starting BootHandlerService...
I/FlutterBootReceiverPlugin(17265): onAttachedToEngine
Connecting to VM Service at ws://127.0.0.1:46697/N80325QdPXg=/ws
Connected to the VM Service.
W/BootHandlerService(17265): Attempted to start a duplicate background isolate. Returning...
[log] App data directory: /data/user/0/com.example.foo/files/
I/BootHandlerService(17265): BootHandlerService started!
I/FlutterBackgroundExecutor(17265): Executing Dart callback: -7767485495945997519...
[log] App data directory: /data/user/0/com.example.foo/files/
D/AndroidRuntime(17265): Shutting down VM
E/AndroidRuntime(17265): FATAL EXCEPTION: main
E/AndroidRuntime(17265): Process: com.example.foo, PID: 17265
E/AndroidRuntime(17265): android.app.RemoteServiceException$ForegroundServiceDidNotStartInTimeException: Context.startForegroundService() did not then call Service.startForeground(): ServiceRecord{d0fedbe u0 com.example.foo/com.dexterous.flutterlocalnotifications.ForegroundService c:com.example.foo}
E/AndroidRuntime(17265): 	at android.app.ActivityThread.generateForegroundServiceDidNotStartInTimeException(ActivityThread.java:2271)
E/AndroidRuntime(17265): 	at android.app.ActivityThread.throwRemoteServiceException(ActivityThread.java:2239)
E/AndroidRuntime(17265): 	at android.app.ActivityThread.-$$Nest$mthrowRemoteServiceException(Unknown Source:0)
E/AndroidRuntime(17265): 	at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2557)
E/AndroidRuntime(17265): 	at android.os.Handler.dispatchMessage(Handler.java:107)
E/AndroidRuntime(17265): 	at android.os.Looper.loopOnce(Looper.java:232)
E/AndroidRuntime(17265): 	at android.os.Looper.loop(Looper.java:317)
E/AndroidRuntime(17265): 	at android.app.ActivityThread.main(ActivityThread.java:8705)
E/AndroidRuntime(17265): 	at java.lang.reflect.Method.invoke(Native Method)
E/AndroidRuntime(17265): 	at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:580)
E/AndroidRuntime(17265): 	at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:886)
I/Process (17265): Sending signal. PID: 17265 SIG: 9
Lost connection to device.

Exited.

It all happened quickly, but there was a doubling of isolates. One of the background isolates started fine, but another looked like it was changing its isolate names in the call stack view before the process crashed.

@leaf-node
Copy link
Contributor Author

Perhaps this is coming up due to the workaround I'm using in my code for issue #4.

@leaf-node
Copy link
Contributor Author

leaf-node commented Nov 28, 2024

The extra threads on Android 15 may be caused by this issue: https://issuetracker.google.com/issues/381281328

@leaf-node
Copy link
Contributor Author

To see how I'm working around the SDK 35 bug (without your module), see: https://stackoverflow.com/a/79246441/582326. Maybe your module could implement something similar?

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

1 participant