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

register failed, the sensor listeners size has exceeded the maximum limit 128 #139

Closed
sc00n opened this issue Dec 16, 2020 · 4 comments
Closed

Comments

@sc00n
Copy link
Contributor

sc00n commented Dec 16, 2020

When I use the simple Flutter example app and add the light measure to my CARP study I get the following error (tested on Android phone and emulator):

Failed to open event stream
E/EventChannel#light.eventChannel( 8803): java.lang.IllegalStateException: register failed, the sensor listeners size has exceeded the maximum limit 128
E/EventChannel#light.eventChannel( 8803): 	at android.hardware.SystemSensorManager.registerListenerImpl(SystemSensorManager.java:197)
E/EventChannel#light.eventChannel( 8803): 	at android.hardware.SensorManager.registerListener(SensorManager.java:859)
E/EventChannel#light.eventChannel( 8803): 	at android.hardware.SensorManager.registerListener(SensorManager.java:766)
E/EventChannel#light.eventChannel( 8803): 	at dk.cachet.light.LightPlugin.onListen(LightPlugin.java:54)
E/EventChannel#light.eventChannel( 8803): 	at io.flutter.plugin.common.EventChannel$IncomingStreamRequestHandler.onListen(EventChannel.java:188)
E/EventChannel#light.eventChannel( 8803): 	at io.flutter.plugin.common.EventChannel$IncomingStreamRequestHandler.onMessage(EventChannel.java:167)
E/EventChannel#light.eventChannel( 8803): 	at io.flutter.embedding.engine.dart.DartMessenger.handleMessageFromDart(DartMessenger.java:85)
E/EventChannel#light.eventChannel( 8803): 	at io.flutter.embedding.engine.FlutterJNI.handlePlatformMessage(FlutterJNI.java:692)
E/EventChannel#light.eventChannel( 8803): 	at android.os.MessageQueue.nativePollOnce(Native Method)
E/EventChannel#light.eventChannel( 8803): 	at android.os.MessageQueue.next(MessageQueue.java:326)
E/EventChannel#light.eventChannel( 8803): 	at android.os.Looper.loop(Looper.java:181)
E/EventChannel#light.eventChannel( 8803): 	at android.app.ActivityThread.main(ActivityThread.java:7063)
E/EventChannel#light.eventChannel( 8803): 	at java.lang.reflect.Method.invoke(Native Method)
E/EventChannel#light.eventChannel( 8803): 	at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:494)
E/EventChannel#light.eventChannel( 8803): 	at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:965)

════════ Exception caught by services library ══════════════════════════════════════════════════════
The following PlatformException was thrown while activating platform stream on channel light.eventChannel:
PlatformException(error, register failed, the sensor listeners size has exceeded the maximum limit 128, null, null)

To reproduce you can add the following code:

    PeriodicMeasure lightMeasure = PeriodicMeasure(
      MeasureType(NameSpace.CARP, SensorSamplingPackage.LIGHT),
      name: "Ambient Light",
      frequency: const Duration(seconds: 1),
      duration: const Duration(milliseconds: 100),
    );

After about 128 seconds you get the error. You can also increase the duration in the frequency parameter (e.g. by using SamplingSchema.common()), but in that case you have to wait longer to reproduce the error. I think I get the error because the stream is not correctly cancelled.

When I was playing around myself with foreground services a while ago I had the same problem when I used your light package. I solved it by adapting the sensor package to include a light sensor. This package and code did not seem to have the cancelling problem.

Flutter doctor -v
[√] Flutter (Channel stable, 1.22.4, on Microsoft Windows [Version 10.0.19041.630], locale nl-BE)
    • Flutter version 1.22.4 at C:\src\flutter\flutter
    • Framework revision 1aafb3a8b9 (5 weeks ago), 2020-11-13 09:59:28 -0800
    • Engine revision 2c956a31c0
    • Dart version 2.10.4

[√] Android toolchain - develop for Android devices (Android SDK version 29.0.2)
    • Android SDK at C:\Users\u0084978\AppData\Local\Android\sdk
    • Platform android-30, build-tools 29.0.2
    • Java binary at: C:\Program Files\Android\Android Studio2\jre\bin\java
    • Java version OpenJDK Runtime Environment (build 1.8.0_242-release-1644-b01)
    • All Android licenses accepted.

[!] Android Studio (version 4.1.0)
    • Android Studio at C:\Program Files\Android\Android Studio2
    X Flutter plugin not installed; this adds Flutter specific functionality.
    X Dart plugin not installed; this adds Dart specific functionality.
    • Java version OpenJDK Runtime Environment (build 1.8.0_242-release-1644-b01)

[√] Connected device (2 available)
    • SM A405FN (mobile)           • R58N11LA7QV   • android-arm64 • Android 9 (API 28)
    • AOSP on IA Emulator (mobile) • emulator-5554 • android-x86   • Android 9 (API 28) (emulator)

I use carp_mobile_sensing: ^0.10.1

@koenniem
Copy link

My guess (but it's just a guess) is that the Timer expires before the subscription has started. In BufferingPeriodicStreamProbe:

  Future<void> onResume() async {
    timer = Timer.periodic(frequency, (Timer t) {
      onSamplingStart();
      subscription = bufferingStream?.listen(onSamplingData,
          onError: onError, onDone: onDone);
      Timer(duration, () async {
        await subscription?.cancel();
        onSamplingEnd();
        await getDatum().then((datum) {
          if (datum != null) controller.add(datum);
        }).catchError(
            (error, stacktrace) => controller.addError(error, stacktrace));
      });
    });
  }

The cancel() method called upon subscription is null-safe and it is therefore possible cancel() is called before the subscription has finished (and thus has no effect).

Before issue #114 the stream would have been paused but this would not cause the error.

@sc00n
Copy link
Contributor Author

sc00n commented Dec 16, 2020

I tested if the cancel was actually called by setting the oncancel method in the android code of the light package to

    @Override
    public void onCancel(Object arguments) {
        /// Finish listening to events
		System.out.println("Do the cancellation");
		System.out.println(sensorEventListener);
        sensorManager.unregisterListener(sensorEventListener);
		System.out.println("Done the cancellation");
    }

The method seems to be called (I see 'do the cancellation' and 'done the cancellation') BUT System.out.println(sensorEventListener); gives null

@sc00n
Copy link
Contributor Author

sc00n commented Dec 16, 2020

Ok, I solved the bug. In the onlisten you put

SensorEventListener sensorEventListener = createSensorEventListener(events);
but this should be
sensorEventListener = createSensorEventListener(events);

Otherwise your class variable sensorEventListener stays null.

I will now try to do my very first pull request, as I was not a frequent Githubber before I started using Flutter :)

sc00n added a commit to sc00n/flutter-plugins that referenced this issue Dec 16, 2020
Solving problems of issue cph-cachet/carp.sensing-flutter#139

in onListen the class variable sensorEventListener should be set, not a new variable. Otherwise the onCancel method doesn't work: the sensorEventListener variable is still null in onCancel and nothing is unregistered.
@bardram
Copy link
Contributor

bardram commented Dec 16, 2020

Thanks. Has merged your PR now. The light plugin is released as v. 1.0.1. This will be updated in CAMS release v. 0.11.0.

@bardram bardram closed this as completed Dec 16, 2020
@bardram bardram reopened this Dec 16, 2020
@bardram bardram closed this as completed Dec 17, 2020
koenniem pushed a commit to koenniem/flutter-plugins that referenced this issue Mar 25, 2021
Solving problems of issue cph-cachet/carp.sensing-flutter#139

in onListen the class variable sensorEventListener should be set, not a new variable. Otherwise the onCancel method doesn't work: the sensorEventListener variable is still null in onCancel and nothing is unregistered.
hibitness-quan-nguyen2 pushed a commit to hibitness/flutter-plugins that referenced this issue Dec 18, 2023
Solving problems of issue cph-cachet/carp.sensing-flutter#139

in onListen the class variable sensorEventListener should be set, not a new variable. Otherwise the onCancel method doesn't work: the sensorEventListener variable is still null in onCancel and nothing is unregistered.
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

3 participants