mysql.dart icon indicating copy to clipboard operation
mysql.dart copied to clipboard

Catching SocketException

Open rebaz94 opened this issue 2 years ago • 9 comments

Hi. Thanks for your library.

Sometime I got SocketException and even if its wrapped in try catch the error will be thrown and can not be handled, this makes not able to return any response correctly. please could you fix it, any workaround? SocketException: Operation timed out (OS Error: Operation timed out, errno = 60), address = ...

rebaz94 avatar Aug 23 '22 23:08 rebaz94

Good day/night. Can you provide some code?

zim32 avatar Aug 23 '22 23:08 zim32

Here the catch block never called

  Future<Record<T>> first<T>(
    String query, {
    required T Function(Map<String, String?> dbResult) toModel,
    Map<String, dynamic>? params,
  }) async {
    try {
      final result = await pool.execute(query, params);
      final value = result.rows.firstOrNull;

      if (value == null) {
        return Record.notExist();
      }

      return Record(toModel(value.assoc()));
    } catch (e, s) {
      logger.e('mysql: select first operation failed', e, s);
      return Record.error(msg: e.toString(), error: e);
    }
  }

I tried to run runZonedGuarded for the server, it will catch the error but no way to return response

await runZonedGuarded(
    () => initServer(args),
    (error, stack) {
      logger.e('runZonedGuarded: ${error.toString()}', error, stack);
    },
  );

Its kind wired, in the log I see two SocketException thrown!

rebaz94 avatar Aug 24 '22 00:08 rebaz94

I think error is happening somewhere outside of your try catch block. Hard to tell right now

zim32 avatar Aug 24 '22 00:08 zim32

No, I tested a lot. the error coming from mysql and the address of mysql server will be printed in the exception SocketException: Operation timed out (OS Error: Operation timed out, errno = 60), address = server address

just to reproduce same error:

  1. Sent a normal query, just to be sure there is one connection in the pool
  2. Turn off the WiFi
  3. Send another query and wait, it will throw SocketException
  4. Send another query, this time it throw exception for host lockup, but it will catches without problem.

if try to add timeout to the request, when your WiFi is off, it will timeout then throw SocketException

rebaz94 avatar Aug 24 '22 00:08 rebaz94

I mean because of asynchronous nature of this library, exception is thrown outside execute method. I need more time to test this behaviour

zim32 avatar Aug 24 '22 10:08 zim32

I mean every request is multistep process, where request is send, and then asynchronously response is received.

zim32 avatar Aug 24 '22 10:08 zim32

Thank you for the time you put on checking this issue.

I found there is some bug in HttpClient on handling SocketException. According to this comment. You need to handle exception in onError callback. Maybe it help

rebaz94 avatar Aug 24 '22 10:08 rebaz94

Yes. I think this is the only way to handle such async errors. Library should be refactored a little to accept onError callback

zim32 avatar Aug 24 '22 11:08 zim32

Anyway this is something that can not be fixed in five minutes, so please if you can make some workaround temporary, provide your current solution here

zim32 avatar Aug 24 '22 11:08 zim32