From 8c290ebf614497d97700eee991ffbc5fcee8a239 Mon Sep 17 00:00:00 2001 From: Mohammad Rabiee Nasri <76831461+mohammad-nasri@users.noreply.github.com> Date: Tue, 17 Nov 2026 22:28:46 +0400 Subject: [PATCH] [multicast_dns] Fix start returning early while another start is in progress A second call to MDnsClient.start made while the first was still running returned at once, so a caller could await start and then get a StateError from lookup. A failed start also left the client marked as starting, so later calls did nothing. Share the in-progress start between callers and clear it when it completes, so a failed start can be retried. --- packages/multicast_dns/CHANGELOG.md | 4 +- packages/multicast_dns/lib/multicast_dns.dart | 26 ++++++--- packages/multicast_dns/pubspec.yaml | 2 +- packages/multicast_dns/test/client_test.dart | 53 +++++++++++++++++++ 4 files changed, 77 insertions(+), 8 deletions(-) diff --git a/packages/multicast_dns/CHANGELOG.md b/packages/multicast_dns/CHANGELOG.md index 419f6c0c..186baa21 100644 --- a/packages/multicast_dns/CHANGELOG.md +++ b/packages/multicast_dns/CHANGELOG.md @@ -1,5 +1,7 @@ -## NEXT +## 0.3.3+2 +* Fixes `MDnsClient.start` completing before the client is started when another call to `start` is still in progress. +* Fixes `MDnsClient.start` being ignored after an earlier call to `start` failed. * Updates minimum supported SDK version to Flutter 3.44/Dart 3.12. ## 0.3.3+1 diff --git a/packages/multicast_dns/lib/multicast_dns.dart b/packages/multicast_dns/lib/multicast_dns.dart index e986e870..b5fbd790 100644 --- a/packages/multicast_dns/lib/multicast_dns.dart +++ b/packages/multicast_dns/lib/multicast_dns.dart @@ -49,7 +49,7 @@ class MDnsClient { /// Create a new [MDnsClient]. MDnsClient({this._rawDatagramSocketFactory = RawDatagramSocket.bind}); - bool _starting = false; + Future? _starting; bool _started = false; RawDatagramSocket? _incomingIPv4; final List _ipv6InterfaceSockets = []; @@ -87,7 +87,8 @@ class MDnsClient { /// passed to the current [Zone]'s error handler. /// /// Subsequent calls to this method are ignored while the mDNS client is in - /// started state. + /// started state. If the client is still starting, the returned future + /// completes when that start completes. Future start({ InternetAddress? listenAddress, NetworkInterfacesFactory? interfacesFactory, @@ -103,11 +104,25 @@ class MDnsClient { listenAddress.address == InternetAddress.anyIPv6.address, ); - if (_started || _starting) { + if (_started) { return; } - _starting = true; + return _starting ??= _start( + listenAddress: listenAddress, + interfacesFactory: interfacesFactory, + mDnsPort: mDnsPort, + mDnsAddress: mDnsAddress, + onError: onError, + ).whenComplete(() => _starting = null); + } + Future _start({ + required InternetAddress listenAddress, + required NetworkInterfacesFactory interfacesFactory, + required int mDnsPort, + required InternetAddress? mDnsAddress, + required Function? onError, + }) async { final int selectedMDnsPort = _mDnsPort = mDnsPort; _mDnsAddress = mDnsAddress; @@ -162,7 +177,6 @@ class MDnsClient { } incoming.listen((RawSocketEvent event) => _handleIncoming(event, incoming), onError: onError); _started = true; - _starting = false; } /// Stop the client and close any associated sockets. @@ -170,7 +184,7 @@ class MDnsClient { if (!_started) { return; } - if (_starting) { + if (_starting != null) { throw StateError('Cannot stop mDNS client while it is starting.'); } diff --git a/packages/multicast_dns/pubspec.yaml b/packages/multicast_dns/pubspec.yaml index 5f1575cd..6f446eec 100644 --- a/packages/multicast_dns/pubspec.yaml +++ b/packages/multicast_dns/pubspec.yaml @@ -2,7 +2,7 @@ name: multicast_dns description: Dart package for performing mDNS queries (e.g. Bonjour, Avahi). repository: https://github.com/flutter/core-packages/tree/main/packages/multicast_dns issue_tracker: https://github.com/flutter/flutter/issues?q=is%3Aissue+is%3Aopen+label%3A%22p%3A+multicast_dns%22 -version: 0.3.3+1 +version: 0.3.3+2 environment: sdk: ^3.12.0 diff --git a/packages/multicast_dns/test/client_test.dart b/packages/multicast_dns/test/client_test.dart index 12508020..40331ed8 100644 --- a/packages/multicast_dns/test/client_test.dart +++ b/packages/multicast_dns/test/client_test.dart @@ -106,6 +106,59 @@ void main() { await client.lookup(ResourceRecordQuery.serverPointer('_')).toList(); }); + test('start() waits for a start that is already in progress', () async { + final socketCompleter = Completer(); + final client = MDnsClient( + rawDatagramSocketFactory: + (dynamic host, int port, {bool reuseAddress = true, bool reusePort = true, int ttl = 1}) { + return socketCompleter.future; + }, + ); + + final Future firstStart = client.start( + interfacesFactory: (InternetAddressType type) async => [], + ); + var secondStartCompleted = false; + final Future secondStart = client.start().then((_) { + secondStartCompleted = true; + }); + await pumpEventQueue(); + expect(secondStartCompleted, isFalse); + + socketCompleter.complete(FakeRawDatagramSocket()); + await firstStart; + await secondStart; + expect(secondStartCompleted, isTrue); + }); + + test('start() can be called again after a failed start', () async { + var bindAttempts = 0; + final client = MDnsClient( + rawDatagramSocketFactory: + ( + dynamic host, + int port, { + bool reuseAddress = true, + bool reusePort = true, + int ttl = 1, + }) async { + bindAttempts++; + if (bindAttempts == 1) { + throw const SocketException('Failed to bind'); + } + return FakeRawDatagramSocket(); + }, + ); + + await expectLater( + client.start(interfacesFactory: (InternetAddressType type) async => []), + throwsA(isA()), + ); + await client.start(interfacesFactory: (InternetAddressType type) async => []); + + expect(bindAttempts, 2); + }); + // group('Bind a single socket to ANY IPv4 and more than one when IPv6', () { // final testCases = >[ // {