shamblett / coap

A Coap package for dart
Other
16 stars 13 forks source link

Killing network during requests causes uncatchable exception #110

Closed JosefWN closed 1 year ago

JosefWN commented 1 year ago

We don't seem to check if the socket is bound (send's return value is typically ignored, and it's always -1?), but perhaps this is okay since we have retries anyway. Should we ignore any SocketExceptions thrown by send as well then?

Once this issue is fixed I need to remember to test if the previously bound socket is still usable, or if we need to re-bind after some downtime. Need to think about error handling for observe as well, on re-bind...

Example, turning my wifi off during this loop (testing what happens if I drop off a shaky network temporarily):

FutureOr<void> main() async {
  final conf = CoapConfig();
  final uri = Uri(
    scheme: 'coap',
    host: 'californium.eclipseprojects.io',
    port: conf.defaultPort,
  );
  final client = CoapClient(uri, conf);

  try {
    for (var i = 0; i < 10; i++) {
      print('Sending request $i');
      final resp = await client.get('test');
      if (resp.code != CoapCode.content) {
        print('Request failed!');
      }
      await Future<void>.delayed(const Duration(seconds: 5));
    }
  } on Exception catch (e) {
    print('CoAP encountered an exception: $e');
  }

  client.close();
}
Unhandled exception:
SocketException: Send failed (OS Error: Network is unreachable, errno = 51), address = 0.0.0.0, port = 0
#0      _NativeSocket.send (dart:io-patch/socket_patch.dart:1213:34)
#1      _RawDatagramSocket.send (dart:io-patch/socket_patch.dart:2450:15)
#2      CoapNetworkUDP.send (package:coap/src/network/coap_network_udp.dart:55:16)
#3      CoapEndPoint.sendRequest (package:coap/src/net/coap_endpoint.dart:198:21)
#4      CoapStackBottomLayer.sendRequest (package:coap/src/stack/coap_layer_stack.dart:162:30)
#5      CoapNextLayer.sendRequest (package:coap/src/stack/coap_layer_stack.dart:29:10)
#6      CoapAbstractLayer.sendRequest (package:coap/src/stack/coap_abstract_layer.dart:26:15)
#7      CoapReliabilityLayer.sendRequest (package:coap/src/stack/coap_reliability_layer.dart:118:11)
#8      CoapNextLayer.sendRequest (package:coap/src/stack/coap_layer_stack.dart:29:10)
#9      CoapAbstractLayer.sendRequest (package:coap/src/stack/coap_abstract_layer.dart:26:15)
#10     CoapTokenLayer.sendRequest (package:coap/src/stack/coap_token_layer.dart:35:11)
#11     CoapNextLayer.sendRequest (package:coap/src/stack/coap_layer_stack.dart:29:10)
#12     CoapAbstractLayer.sendRequest (package:coap/src/stack/coap_abstract_layer.dart:26:15)
#13     CoapBlockwiseLayer.sendRequest (package:coap/src/stack/coap_blockwise_layer.dart:72:13)
#14     CoapNextLayer.sendRequest (package:coap/src/stack/coap_layer_stack.dart:29:10)
#15     CoapAbstractLayer.sendRequest (package:coap/src/stack/coap_abstract_layer.dart:26:15)
#16     CoapNextLayer.sendRequest (package:coap/src/stack/coap_layer_stack.dart:29:10)
#17     CoapAbstractLayer.sendRequest (package:coap/src/stack/coap_abstract_layer.dart:26:15)
#18     CoapStackTopLayer.sendRequest (package:coap/src/stack/coap_layer_stack.dart:99:11)
#19     CoapLayerStack.sendRequest (package:coap/src/stack/coap_layer_stack.dart:197:18)
#20     CoapEndPoint.sendEpRequest.<anonymous closure> (package:coap/src/net/coap_endpoint.dart:105:37)
#21     _Executor.scheduleTask (package:executor/src/executor_impl.dart:61:29)
<asynchronous suspension>