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

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

Closed
Tracked by #77
eligt opened this issue Jan 20, 2019 · 5 comments
Labels
p: android_alarm_manager The Android background execution plugin P3 Issues that are less important to the Flutter project package flutter/packages repository. See also p: labels. platform-android Android applications specifically

Comments

@eligt
Copy link

eligt commented Jan 20, 2019

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).

@Kartik1607
Copy link
Contributor

void alarmServiceHandler() {
...
    AndroidAlarmManager.initialize().then((bool ready) {
        ...
        AndroidAlarmManager.oneShot(const Duration(seconds: 1), alarmID, alarmServiceHandler, exact: true);
    }
....
}

The handler is re-initializing AlarmManager, which in turn again calls up the handler after duration of 1 second. This seems to be a case of recursion.

@zoechi zoechi added plugin p: android_alarm_manager The Android background execution plugin labels Jan 20, 2019
@zoechi zoechi added this to the Goals milestone Jan 20, 2019
@eligt
Copy link
Author

eligt commented Jan 21, 2019

It's not recursion in that the alarm is re-enqueued once the old alarm handler has finished, so my expectation would be for the old thread to be released and a new one to be created for the new alarm - I'm not saying this should occur instantly but at some point the old threads should be released. Instead what I'm noticing is that new threads keep being created and the old ones are never released.

Unless you're saying that the initialize() call holds the thread in some kind of loop? In that case, what's the best way to invoke an alarm handler from inside another alarm handler?

@eligt
Copy link
Author

eligt commented Jan 21, 2019

It looks like just calling AndroidAlarmManager.oneShot from within the alarm handler, without calling initialize first, seems to correct the issue. I'm wondering however, if this is the expected way of re-enqueuing an alarm from within the handler or just a fluke that it happens to work?

@TahaTesser TahaTesser added the platform-android Android applications specifically label Mar 17, 2020
@kf6gpe kf6gpe added the P2 Important issues not at the top of the work list label May 29, 2020
@Hixie Hixie removed this from the None. milestone Aug 17, 2020
@stuartmorgan stuartmorgan added P6 and removed P2 Important issues not at the top of the work list labels Feb 4, 2021
@stuartmorgan
Copy link
Contributor

Closing as android_alarm_manager is deprecated in favor of android_alarm_manager_plus.

engine-flutter-autoroll added a commit to engine-flutter-autoroll/flutter that referenced this issue Jun 21, 2021
engine-flutter-autoroll added a commit to engine-flutter-autoroll/flutter that referenced this issue Jun 22, 2021
engine-flutter-autoroll added a commit to engine-flutter-autoroll/flutter that referenced this issue Jun 22, 2021
engine-flutter-autoroll added a commit to engine-flutter-autoroll/flutter that referenced this issue Jun 22, 2021
engine-flutter-autoroll added a commit to engine-flutter-autoroll/flutter that referenced this issue Jun 22, 2021
@github-actions
Copy link

github-actions bot commented Aug 1, 2021

This thread has been automatically locked since there has not been any recent activity after it was closed. If you are still experiencing a similar issue, please open a new bug, including the output of flutter doctor -v and a minimal reproduction of the issue.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Aug 1, 2021
@flutter-triage-bot flutter-triage-bot bot added P3 Issues that are less important to the Flutter project and removed P6 labels Jun 28, 2023
@flutter-triage-bot flutter-triage-bot bot added the package flutter/packages repository. See also p: labels. label Jul 5, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
p: android_alarm_manager The Android background execution plugin P3 Issues that are less important to the Flutter project package flutter/packages repository. See also p: labels. platform-android Android applications specifically
Projects
None yet
Development

No branches or pull requests

7 participants