Skip to content

[Plugins android_alarm_manager] Invoking oneShot() from within a oneShot callback creates infinite threads #26813

@eligt

Description

@eligt

The following code will end up creating an ever-increasing amount of threads:

void alarmServiceHandler() {
  final DateTime now = DateTime.now();
  final int isolateId = Isolate.current.hashCode;

  print("[$now] alarmServiceHandler isolate=${isolateId} function='$alarmServiceHandler'");

  AndroidAlarmManager.initialize().then((bool ready) {
    print("AndroidAlarmManager");
    if (ready) {
      print("AndroidAlarmManager.oneShot");
      final int alarmID = 1;
      AndroidAlarmManager.oneShot(const Duration(seconds: 1), alarmID, alarmServiceHandler, exact: true);
    }
  });

  print("[$now] alarmServiceHandler FINISH");
}

void main() {
  AndroidAlarmManager.initialize().then((bool ready) {
    print("AndroidAlarmManager");
    if (ready) {
      print("AndroidAlarmManager.oneShot");
      final int alarmID = 1;
      AndroidAlarmManager.oneShot(const Duration(seconds: 1), alarmID, alarmServiceHandler, exact: true);
    }
  });

  runApp(MyApp());
}

After execution of the function and new oneShot alarm is registered, I'd expect the thread to finish and get freed but that doesn't seem to happen. Note that the Isolate hash stays the same, but a new thread is created each time oneShot is called (and never released).

Metadata

Metadata

Assignees

No one assigned

    Labels

    P3Issues that are less important to the Flutter projectp: android_alarm_managerThe Android background execution pluginpackageflutter/packages repository. See also p: labels.platform-androidAndroid applications specifically

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions