Skip to content

Cannot catch SocketException thrown when sending GRPC request #396

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

Closed
engelen opened this issue Nov 7, 2020 · 5 comments
Closed

Cannot catch SocketException thrown when sending GRPC request #396

engelen opened this issue Nov 7, 2020 · 5 comments

Comments

@engelen
Copy link

engelen commented Nov 7, 2020

When my GRPC server is unavailable, a SocketException is thrown in the http2_connection.dart file when I make a GRPC call; I appear to be unable to catch the error.

grpc-dart: 2.7.0

Repro steps

  1. Start the GRPC server
  2. Open a client channel
  3. Start a client
  4. Stop the GRPC server
  5. Run a GRPC call
import 'package:grpc/grpc.dart';

import 'package:mypackage/api/v1/myservice.pbgrpc.dart' as grpc;

var channel = ClientChannel(
  serverIP,
  port: serverPort,
  options: ChannelOptions(
    credentials: ChannelCredentials.insecure(),
    idleTimeout: Duration(seconds: 10),
  ),
);

var client = grpc.MyClient(channel);

client.myCall().then((response) {
  print('success');
}, onError: (error) {
  print('onError');
}).catchError((error) {
  print('catchError');
});

Expected result: printing 'onError'

Actual result:

Exception has occurred.
SocketException (SocketException: OS Error: Connection refused, errno = 111, address = 10.0.2.2, port = 43316)

on this line:

_socket = await Socket.connect(_host, _port);

Thanks for your help!

@mraleph
Copy link
Member

mraleph commented Nov 12, 2020

I don't seem to be able to reproduce this. I see error correctly propagate. I have tried reproducing this using helloworld example doing two different things:

  • Run client without server. Got

    Caught error: gRPC Error (code: 14, codeName: UNAVAILABLE, message: Error connecting: SocketException: OS Error: Connection refused, errno = 61, address = localhost, port = 62017, details: null)
    
  • Add loop repeating the call every 10s and killed server after the first successful call. Got

    Greeter client received: Hello, world!
    Caught error: gRPC Error (code: 14, codeName: UNAVAILABLE, message: Error connecting: SocketException: OS Error: Connection refused, errno = 61, address = localhost, port = 62066, details: null)
    

Could you provide the full runnable reproduction for the problem?

@mraleph mraleph closed this as completed Nov 12, 2020
@engelen
Copy link
Author

engelen commented Nov 30, 2020

@mraleph Thanks a lot for your help. I'm sorry for not responding earlier; it turns out I had email notifications turned off.

I've downloaded the hello world example and tried to reproduce it, and it turns out the issue does not occur when I run dart from the command line directly, but it does occur when I run dart using VS code. In VS Code, if I untick the checkbox Uncaught Exceptions in the Breakpoints settings screen, the problem does not occur anymore, and the error is nicely caught. (if the checkbox is ticked, I get the problem I described in the issue description)

It seems like VS Code is marking this as an "Uncaught exception", although it is clearly caught. Could this issue be due to the way grpc-dart handles errors, or should I contact VS Code?

@engelen
Copy link
Author

engelen commented Nov 30, 2020

It looks a problem in VS Code / Dart itself:
flutter/flutter#33427
Dart-Code/Dart-Code#2063

@mraleph
Copy link
Member

mraleph commented Nov 30, 2020

Thanks for the context! I would imagine that even when "Uncaught Exceptions" check box is ticked you should be able to resume execution - after which exception should be caught in the right place.

I have pinged dart-lang/sdk#37953 (which is the upstream Dart bug tracking improvements in "pause on unhandled exceptions" functionality). Let's see what we can do about it.

@engelen
Copy link
Author

engelen commented Dec 1, 2020

Excellent, thanks a lot!

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