shamblett / mqtt5_client

A server and browser based MQTT 5 client for dart
Other
45 stars 23 forks source link

Would like a way to capture low level socket errors #98

Closed kmiller15211 closed 1 week ago

kmiller15211 commented 1 month ago

I'm running the library in a Flutter application that is utilizing Bluetooth, and becomes backgrounded/paused often.

On iOS, I see the application be placed into the paused state for quite a while (~ an hour). Upon waking, I can see socket errors being emitted from the MQTT socket due to the paused state:

Main: Flutter Platform Dispatcher detected a flutter error: SocketException: Can't assign requested address (OS Error: Can't assign requested address, errno = 49), address = [AWS IoT URL HERE/), port = 49806, stack:

AND

SEVERE - Main: Flutter Platform Dispatcher detected a flutter error: SocketException: Bad file descriptor (OS Error: Bad file descriptor, errno = 9), address = [AWS IoT URL HERE), port = 53656, stack:

This is a perfectly expected error, as I'm sure the heartbeat/pings have expired. My question is - is there any way to catch and handle these exceptions, and prevent them being caught by the default Flutter Platform dispatcher handler, which I register in my main.dart file:

  var formerPlatformDispatcherError = PlatformDispatcher.instance.onError;
  PlatformDispatcher.instance.onError = (error, stack) {
    mainLogger.severe("Flutter Platform Dispatcher detected a "
        "flutter error: ${error.toString()}, stack: ${stack.toString()}");

    formerPlatformDispatcherError?.call(error, stack);
    return true;
  };

Which indicates these are unhandled. I have all of my MQTT calls wrapped in try... catch blocks, which is why I'm guessing this is being thrown outside of my main 'thread' of execution, possibly by some isolate? The stack is always empty here, which I can't really explain.

I'm guessing these are being run in some isolate beneath the MQTT5 package. Just curious if there's any way that we can handle these exceptions.... maybe expose the isolate so that I can do an addErrorListener() to it, or some other way to keep very normal and expected error from bubbling up to the top level unhandled exception handler.

Thank you in advance!

shamblett commented 1 month ago

One way to do this is to run your client code(as much as possible of it anyway) in a zone, see the Dart API here.

This has definitely worked for some users as you can see from the closed issues on this.