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

RawDatagramSocket stopped working on iOS 17.4.1 #55564

Closed
an-mediola opened this issue Apr 25, 2024 · 4 comments
Closed

RawDatagramSocket stopped working on iOS 17.4.1 #55564

an-mediola opened this issue Apr 25, 2024 · 4 comments

Comments

@an-mediola
Copy link

The RawDatagramSocket stopped working on iOS 17.4.1.
To reproduce, I've made a simple app and run it on 4 different iphones.

import 'dart:io';

import 'package:flutter/material.dart';

void main() async {
  runApp(const MaterialApp(home: UdpTest()));
}


class UdpTest extends StatefulWidget {
  const UdpTest({super.key});

  @override
  State<UdpTest> createState() => _UdpTestState();
}

class _UdpTestState extends State<UdpTest> {
  late UdpCommunication _udpCommunication;

  final List _messages = [];

  @override
  void initState() {
    _udpCommunication = UdpCommunication();
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: SafeArea(
        child: Column(
          children: [
            ElevatedButton(
                child: const Text('Init'),
                onPressed: () async {
                  await _udpCommunication.initializeSocket();
                  _udpCommunication.listenForMessages((message) => setState(() {
                        _messages.add(message);
                      }));
                }),
            ElevatedButton(
              child: const Text('Send broadcast message'),
              onPressed: () {
                _udpCommunication.sendMessage('GET\n', InternetAddress('239.255.255.250'), 1901);
                _udpCommunication.sendMessage('GET\n', InternetAddress('255.255.255.255'), 1901);
              },
            ),
            ListView.builder(
                shrinkWrap: true,
                itemBuilder: (context, index) => ListTile(
                      title: Text(_messages[index]),
                    ),
                itemCount: _messages.length),
          ],
        ),
      ),
    );
  }
}

class UdpCommunication {
  late RawDatagramSocket udpSocket;

  Future<void> initializeSocket() async {
    udpSocket = await RawDatagramSocket.bind(InternetAddress.anyIPv4, 0, reuseAddress: true, reusePort: true);
    print('socket bound to ${udpSocket.address}:${udpSocket.port}');

    udpSocket.broadcastEnabled = true;

    // join multicast group
    udpSocket.joinMulticast(InternetAddress('239.255.255.250'));

    // on error log it
    udpSocket.handleError((error) {
      print('Error: $error');
    });
  }

  void sendMessage(String message, InternetAddress address, int port) {
    udpSocket.send(message.codeUnits, address, port);
  }

  void listenForMessages(Function(String) onMessageReceived) {
    udpSocket.listen((RawSocketEvent event) {
      print(event);

      if (event == RawSocketEvent.read) {
        Datagram? datagram = udpSocket.receive();
        if (datagram != null) {
          String message = String.fromCharCodes(datagram.data);
          onMessageReceived(message);
        }
      }
    });
  }
}

Error on iphone 17.4.1:

flutter: socket bound to InternetAddress('0.0.0.0', IPv4):59136
flutter: RawSocketEvent.write
[ERROR:flutter/runtime/dart_vm_initializer.cc(41)] Unhandled Exception: SocketException: Send failed (OS Error: No route to host, errno = 65), address = 0.0.0.0, port = 59136
#0      _NativeSocket.send (dart:io-patch/socket_patch.dart:1269:34)
#1      _RawDatagramSocket.send (dart:io-patch/socket_patch.dart:2582:15)
#2      UdpCommunication.sendMessage (package:mediolaconnect/upd_socket_test.dart:76:15)
#3      _UdpTestState.build.<anonymous closure> (package:mediolaconnect/upd_socket_test.dart:40:35)
#4      _InkResponseState.handleTap (package:flutter/src/material/ink_well.dart:1183:21)
#5      GestureRecognizer.invokeCallback (package:flutter/src/gestures/recognizer.dart:315:24)
#6      TapGestureRecognizer.handleTapUp (package:flutter/src/gestures/tap.dart:652:11)
#7      BaseTapGestureRecognizer._checkUp (package:flutter/src/gestures/tap.dart:309:5)
#8      BaseTapGestureRecognizer.handlePrimaryPointer (package:flutter/src/gestures/tap.dart:242:7)
#9      PrimaryPointerGestureRecognizer.handleEvent (package:flutter/src/gestures/recognizer.dart:670:9)
#10     PointerRouter._dispatch (package:flutter/src/gestures/pointer_router.dart:98:12)
#11     PointerRouter._dispatchEventToRoutes.<anonymous closure> (package:flutter/src/gestures/pointer_router.dart:143:9)
#12     _LinkedHashMapMixin.forEach (dart:collection-patch/compact_hash.dart:633:13)
#13     PointerRouter._dispatchEventToRoutes (package:flutter/src/gestures/pointer_router.dart:141:18)
#14     PointerRouter.route (package:flutter/src/gestures/pointer_router.dart:127:7)
#15     GestureBinding.handleEvent (package:flutter/src/gestures/binding.dart:495:19)
#16     GestureBinding.dispatchEvent (package:flutter/src/gestures/binding.dart:475:22)
#17     RendererBinding.dispatchEvent (package:flutter/src/rendering/binding.dart:430:11)
#18     GestureBinding._handlePointerEventImmediately (package:flutter/src/gestures/binding.dart:420:7)
#19     GestureBinding.handlePointerEvent (package:flutter/src/gestures/binding.dart:383:5)
#20     GestureBinding._flushPointerEventQueue (package:flutter/src/gestures/binding.dart:330:7)
#21     GestureBinding._handlePointerDataPacket (package:flutter/src/gestures/binding.dart:299:9)

Flutter doctor

Doctor summary (to see all details, run flutter doctor -v):
[✓] Flutter (Channel stable, 3.19.6, on macOS 13.6 22G120 darwin-arm64 (Rosetta), locale en)
[✓] Android toolchain - develop for Android devices (Android SDK version 33.0.1)
[✓] Xcode - develop for iOS and macOS (Xcode 15.0)
[✓] Chrome - develop for the web
[✓] Android Studio (version 2023.1)
[✓] VS Code (version 1.88.1)
[✓] Connected device (5 available)
[✓] Network resources

• No issues found!

I'd greatly appreciate assistance and guidance from someone on the core team in narrowing down the scope of debugging.

maybe related to #45824

@an-mediola
Copy link
Author

an-mediola commented Apr 26, 2024

Restarting the iPhone fixed it!
For now it isn't reproducible.

@indeedshaw
Copy link

Have you found the cause of the problem? I can reproducible this issue.

@an-mediola
Copy link
Author

Have you found the cause of the problem? I can reproducible this issue.

unfortunately no, it seems related to OS not dart. restarting the phone didn't help?

@indeedshaw
Copy link

Yes, restarting the phone socket to send data will return to normal (socket.send returns > 0), but I want to know what causes this so that we can prevent it from happening

@an-mediola
Copy link
Author

an-mediola commented Jun 17, 2024

Yes, restarting the phone socket to send data will return to normal (socket.send returns > 0), but I want to know what causes this so that we can prevent it from happening

Then if you know or you can find a reliable way to reproduce it, you can write it here for others and then we will reopen it.
I couldn't reproduce it after restarting the phone and then is not possible for others to investigate the reason.
Maybe somebody from core team can help us.

@indeedshaw
Copy link

indeedshaw commented Jun 17, 2024

You can try to see if this step can reproduce this problem.Here's my repeat step: iOS17.5.1, denies the local network permission that the software initially requested, then turns it back on in Settings, and no matter how I try to send a broadcast, the length of the broadcast is 0, and the socket only receives a close event, no error occurred.
At the moment I think the software was initially not granted local network permissions, subsequent permissions were granted to no avail, and a reboot was needed to achieve some kind of reset. But I am not sure if this is the reason, I may need to ask the relevant Apple engineers. I'm planning to do that.

@E2-Veera
Copy link

E2-Veera commented Aug 2, 2024

I'm experiencing the same issue in both Android and iOS devices. check my ticket dart-lang/http#1278

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

3 participants