Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion packages/multicast_dns/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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
Expand Down
26 changes: 20 additions & 6 deletions packages/multicast_dns/lib/multicast_dns.dart
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ class MDnsClient {
/// Create a new [MDnsClient].
MDnsClient({this._rawDatagramSocketFactory = RawDatagramSocket.bind});

bool _starting = false;
Future<void>? _starting;
bool _started = false;
RawDatagramSocket? _incomingIPv4;
final List<RawDatagramSocket> _ipv6InterfaceSockets = <RawDatagramSocket>[];
Expand Down Expand Up @@ -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<void> start({
InternetAddress? listenAddress,
NetworkInterfacesFactory? interfacesFactory,
Expand All @@ -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<void> _start({
required InternetAddress listenAddress,
required NetworkInterfacesFactory interfacesFactory,
required int mDnsPort,
required InternetAddress? mDnsAddress,
required Function? onError,
}) async {
final int selectedMDnsPort = _mDnsPort = mDnsPort;
_mDnsAddress = mDnsAddress;

Expand Down Expand Up @@ -162,15 +177,14 @@ class MDnsClient {
}
incoming.listen((RawSocketEvent event) => _handleIncoming(event, incoming), onError: onError);
_started = true;
_starting = false;
}

/// Stop the client and close any associated sockets.
void stop() {
if (!_started) {
return;
}
if (_starting) {
if (_starting != null) {
throw StateError('Cannot stop mDNS client while it is starting.');
}
Comment on lines 183 to 189

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

high

In the current implementation of stop(), the check if (!_started) is performed before if (_starting != null). Since _started is only set to true at the very end of the start process, _started is false while the client is starting. This means calling stop() while the client is starting will return early and silently ignore the call, rather than throwing the expected StateError as intended. Swapping the order of these checks ensures that attempting to stop the client while it is starting correctly throws a StateError.

Suggested change
void stop() {
if (!_started) {
return;
}
if (_starting) {
if (_starting != null) {
throw StateError('Cannot stop mDNS client while it is starting.');
}
void stop() {
if (_starting != null) {
throw StateError('Cannot stop mDNS client while it is starting.');
}
if (!_started) {
return;
}


Expand Down
2 changes: 1 addition & 1 deletion packages/multicast_dns/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
53 changes: 53 additions & 0 deletions packages/multicast_dns/test/client_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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<RawDatagramSocket>();
final client = MDnsClient(
rawDatagramSocketFactory:
(dynamic host, int port, {bool reuseAddress = true, bool reusePort = true, int ttl = 1}) {
return socketCompleter.future;
},
);

final Future<void> firstStart = client.start(
interfacesFactory: (InternetAddressType type) async => <NetworkInterface>[],
);
var secondStartCompleted = false;
final Future<void> 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 => <NetworkInterface>[]),
throwsA(isA<SocketException>()),
);
await client.start(interfacesFactory: (InternetAddressType type) async => <NetworkInterface>[]);

expect(bindAttempts, 2);
});

// group('Bind a single socket to ANY IPv4 and more than one when IPv6', () {
// final testCases = <Map<String, Object>>[
// <String, Object>{
Expand Down
Loading