zim32 / mysql.dart

MySQL client for Dart written in Dart
BSD 3-Clause "New" or "Revised" License
64 stars 17 forks source link

Catching SocketException #38

Open rebaz94 opened 1 year ago

rebaz94 commented 1 year ago

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

zim32 commented 1 year ago

Good day/night. Can you provide some code?

rebaz94 commented 1 year ago

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!

zim32 commented 1 year ago

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

rebaz94 commented 1 year ago

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

zim32 commented 1 year ago

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

zim32 commented 1 year ago

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

rebaz94 commented 1 year ago

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

zim32 commented 1 year ago

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

zim32 commented 1 year ago

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