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

Catching SocketException #38

Open
rebaz94 opened this issue Aug 23, 2022 · 9 comments
Open

Catching SocketException #38

rebaz94 opened this issue Aug 23, 2022 · 9 comments

Comments

@rebaz94
Copy link

rebaz94 commented Aug 23, 2022

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
Copy link
Owner

zim32 commented Aug 23, 2022

Good day/night. Can you provide some code?

@rebaz94
Copy link
Author

rebaz94 commented Aug 24, 2022

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
Copy link
Owner

zim32 commented Aug 24, 2022

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

@rebaz94
Copy link
Author

rebaz94 commented Aug 24, 2022

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
Copy link
Owner

zim32 commented Aug 24, 2022

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

@zim32
Copy link
Owner

zim32 commented Aug 24, 2022

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

@rebaz94
Copy link
Author

rebaz94 commented Aug 24, 2022

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
Copy link
Owner

zim32 commented Aug 24, 2022

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

@zim32
Copy link
Owner

zim32 commented Aug 24, 2022

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

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

2 participants