fluttercommunity / flutter_workmanager

A Flutter plugin which allows you to execute code in the background on Android and iOS.
862 stars 277 forks source link

🐞[Task is cancelled] #478

Open ralfeus opened 1 year ago

ralfeus commented 1 year ago

Version

Technology Version
Workmanager version 0.5.1
Xcode version
Swift version
iOS deployment target

Describe the error I start the background task, which is supposed to be long running (as long as device is running). At some random point I get the error:

D/FileSaver( 9588): Detached From Engine
I/WM-WorkerWrapper( 9588): Work [ id=68b948a1-2da3-47e1-b322-9438c9163cf1, tags={ be.tramckrijte.workmanager.BackgroundWorker } ] was cancelled
I/WM-WorkerWrapper( 9588): java.util.concurrent.CancellationException: Task was cancelled.
I/WM-WorkerWrapper( 9588):  at androidx.work.impl.utils.futures.AbstractFuture.cancellationExceptionWithCause(AbstractFuture.java:1184)
I/WM-WorkerWrapper( 9588):  at androidx.work.impl.utils.futures.AbstractFuture.getDoneValue(AbstractFuture.java:514)
I/WM-WorkerWrapper( 9588):  at androidx.work.impl.utils.futures.AbstractFuture.get(AbstractFuture.java:475)
I/WM-WorkerWrapper( 9588):  at androidx.work.impl.WorkerWrapper$2.run(WorkerWrapper.java:311)
I/WM-WorkerWrapper( 9588):  at androidx.work.impl.utils.SerialExecutor$Task.run(SerialExecutor.java:91)
I/WM-WorkerWrapper( 9588):  at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1137)
I/WM-WorkerWrapper( 9588):  at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:637)
I/WM-WorkerWrapper( 9588):  at java.lang.Thread.run(Thread.java:1012)
D/EGL_emulation( 9588): eglCreateContext: 0xb40000765a612390: maj 3 min 0 rcv 3
D/EGL_emulation( 9588): eglCreateContext: 0xb40000765a611010: maj 3 min 0 rcv 3
D/HostConnection( 9588): createUnique: call
D/HostConnection( 9588): HostConnection::get() New Host Connection established 0xb40000765a611490, tid 11921
D/HostConnection( 9588): HostComposition ext ANDROID_EMU_CHECKSUM_HELPER_v1 ANDROID_EMU_native_sync_v2 ANDROID_EMU_native_sync_v3 ANDROID_EMU_native_sync_v4 ANDROID_EMU_dma_v1 ANDROID_EMU_direct_mem ANDROID_EMU_host_composition_v1 ANDROID_EMU_host_composition_v2 ANDROID_EMU_vulkan ANDROID_EMU_deferred_vulkan_commands ANDROID_EMU_vulkan_null_optional_strings ANDROID_EMU_vulkan_create_resources_with_requirements ANDROID_EMU_YUV_Cache ANDROID_EMU_vulkan_ignored_handles ANDROID_EMU_has_shared_slots_host_memory_allocator ANDROID_EMU_vulkan_free_memory_sync ANDROID_EMU_vulkan_shader_float16_int8 ANDROID_EMU_vulkan_async_queue_submit ANDROID_EMU_vulkan_queue_submit_with_commands ANDROID_EMU_sync_buffer_data ANDROID_EMU_vulkan_async_qsri ANDROID_EMU_read_color_buffer_dma ANDROID_EMU_hwc_multi_configs GL_OES_EGL_image_external_essl3 GL_OES_vertex_array_object GL_KHR_texture_compression_astc_ldr ANDROID_EMU_host_side_tracing ANDROID_EMU_gles_max_version_3_0
D/EGL_emulation( 9588): eglMakeCurrent: 0xb40000765a611010: ver 3 0 (tinfo 0x7879ff4280) (first time)

That happens both in emulator and physical device. I'm not sure if this error is in main isolate or background one. But having that the application doesn't crash whilst background isolate does I assume it's occurred in the background isolate.

I wrapped whole background entry point to try-catch but this exception isn't caught. And yeah, it seems to be a Java exception so I didn't really expect it to be caught by Dart code.

The background entry point looks like this:

Future<bool> backgroundMain(String task, JSON? inputData) async {
  try {
    Logger.root.level = Level.ALL;
    Logger.root.onRecord.listen((record) => logToConsole(record, true));
    final port = await _initPort('atkrypto-from-background', 'atkrypto-to-background');
    final persistence = _initPersistence(port);
    final futures = _initCommunication(port, await persistence);
    final assetMgr = AssetManager.init(
        await persistence, (await futures).item1, assetFactories,
        port: port, isBackground: true, consensusTimeout: consensusTimeout);
    AppState.init(
        persistence: await persistence,
        shouldCatchUp: false,
        backgroundDuplexPort: port,
        networkManager: (await futures).item2,
        assetManager: assetMgr);

    /// Wait for exit condition
    while (_run) {
      await Future.delayed(Duration(seconds: 5));
    }
    Logger.root.info("Exiting background isolate");
    return true;
  } catch (e, t) {
    Logger.root.shout("An unknown error has occurred in the background");
    Logger.root.shout(null, e, t);
    return false;
  }
}

Output of flutter doctor -v

flutter doctor -v
[βœ“] Flutter (Channel stable, 3.10.1, on macOS 13.3.1 22E772610a darwin-arm64, locale en-CZ)
    β€’ Flutter version 3.10.1 on channel stable at /Library/flutter
    β€’ Upstream repository https://github.com/flutter/flutter.git
    β€’ Framework revision d3d8effc68 (2 weeks ago), 2023-05-16 17:59:05 -0700
    β€’ Engine revision b4fb11214d
    β€’ Dart version 3.0.1
    β€’ DevTools version 2.23.1

[βœ“] Android toolchain - develop for Android devices (Android SDK version 33.0.0)
    β€’ Android SDK at /Users/ralfeus/Library/Android/sdk
    β€’ Platform android-33, build-tools 33.0.0
    β€’ Java binary at: /Applications/Android Studio.app/Contents/jbr/Contents/Home/bin/java
    β€’ Java version OpenJDK Runtime Environment (build 11.0.15+0-b2043.56-8887301)
    β€’ All Android licenses accepted.

[βœ“] Xcode - develop for iOS and macOS (Xcode 14.3)
    β€’ Xcode at /Applications/Xcode.app/Contents/Developer
    β€’ Build 14E222b
    β€’ CocoaPods version 1.11.3

[βœ“] Android Studio (version 2022.1)
    β€’ Android Studio at /Applications/Android Studio.app/Contents
    β€’ Flutter plugin can be installed from:
      πŸ”¨ https://plugins.jetbrains.com/plugin/9212-flutter
    β€’ Dart plugin can be installed from:
      πŸ”¨ https://plugins.jetbrains.com/plugin/6351-dart
    β€’ Java version OpenJDK Runtime Environment (build 11.0.15+0-b2043.56-8887301)

[βœ“] VS Code (version 1.78.2)
    β€’ VS Code at /Applications/Visual Studio Code.app/Contents
    β€’ Flutter extension version 3.64.0

[βœ“] Connected device (3 available)
    β€’ sdk gphone64 arm64 (mobile) β€’ emulator-5554 β€’ android-arm64 β€’ Android 13 (API 33) (emulator)
    β€’ sdk gphone64 arm64 (mobile) β€’ emulator-5556 β€’ android-arm64 β€’ Android 13 (API 33) (emulator)
    β€’ macOS (desktop)             β€’ macos         β€’ darwin-arm64  β€’ macOS 13.3.1 22E772610a darwin-arm64

[βœ“] Network resources
    β€’ All expected network resources are available.

β€’ No issues found!
martirius commented 1 year ago

This happens because this feature is still not supported as stated in the Readme. This feature is called Long running task and need special settings as stated in the official Android doc https://developer.android.com/guide/background/persistent/how-to/long-running

ralfeus commented 1 year ago

Sorry, I haven't found anything about long running tasks in readme. Is it going to be supported? Is there same situation with iOS? I don't run it on iOS at the moment but it definitely will come to picture in the future.

itsSagarBro commented 1 year ago

Has anyone found a solution for this?