Skip to content

Commit ec04707

Browse files
authored
Adds a new helpful tool exit message for SocketExceptions thrown during mdns discovery (#157638)
Addresses flutter/flutter#150131, but doesn't fix it, as there seem to be cases where the steps included in the messages added in this PR don't work.
1 parent 2d2ebbe commit ec04707

File tree

2 files changed

+53
-4
lines changed

2 files changed

+53
-4
lines changed

packages/flutter_tools/lib/src/mdns_discovery.dart

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import 'base/logger.dart';
1515
import 'build_info.dart';
1616
import 'convert.dart';
1717
import 'device.dart';
18+
import 'globals.dart' as globals;
1819
import 'reporting/reporting.dart';
1920

2021
/// A wrapper around [MDnsClient] to find a Dart VM Service instance.
@@ -229,10 +230,28 @@ class MDnsVmServiceDiscovery {
229230
final Set<String> uniqueDomainNamesInResults = <String>{};
230231

231232
// Listen for mDNS connections until timeout.
232-
final Stream<PtrResourceRecord> ptrResourceStream = client.lookup<PtrResourceRecord>(
233-
ResourceRecordQuery.serverPointer(dartVmServiceName),
234-
timeout: timeout
235-
);
233+
final Stream<PtrResourceRecord> ptrResourceStream;
234+
235+
try {
236+
ptrResourceStream = client.lookup<PtrResourceRecord>(
237+
ResourceRecordQuery.serverPointer(dartVmServiceName),
238+
timeout: timeout,
239+
);
240+
} on SocketException catch (e, stacktrace) {
241+
_logger.printError(e.message);
242+
_logger.printTrace(stacktrace.toString());
243+
if (globals.platform.isMacOS) {
244+
throwToolExit(
245+
'You might be having a permissions issue with your IDE. '
246+
'Please try going to '
247+
'System Settings -> Privacy & Security -> Local Network -> '
248+
'[Find your IDE] -> Toggle ON, then restart your phone.'
249+
);
250+
} else {
251+
rethrow;
252+
}
253+
}
254+
236255
await for (final PtrResourceRecord ptr in ptrResourceStream) {
237256
uniqueDomainNames.add(ptr.domainName);
238257

packages/flutter_tools/test/general.shard/mdns_discovery_test.dart

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import 'package:flutter_tools/src/base/io.dart';
77
import 'package:flutter_tools/src/base/logger.dart';
88
import 'package:flutter_tools/src/build_info.dart';
99
import 'package:flutter_tools/src/device_port_forwarder.dart';
10+
import 'package:flutter_tools/src/globals.dart' as globals;
1011
import 'package:flutter_tools/src/ios/devices.dart';
1112
import 'package:flutter_tools/src/mdns_discovery.dart';
1213
import 'package:flutter_tools/src/project.dart';
@@ -577,6 +578,30 @@ void main() {
577578
);
578579
});
579580

581+
test('On macOS, throw tool exit with a helpful message when client throws a SocketException on lookup', () async {
582+
final MDnsClient client = FakeMDnsClient(
583+
<PtrResourceRecord>[], <String, List<SrvResourceRecord>>{},
584+
socketExceptionOnStart: true);
585+
586+
final MDnsVmServiceDiscovery portDiscovery = MDnsVmServiceDiscovery(
587+
mdnsClient: client,
588+
logger: BufferLogger.test(),
589+
flutterUsage: TestUsage(),
590+
analytics: const NoOpAnalytics(),
591+
);
592+
593+
expect(
594+
portDiscovery.firstMatchingVmService(client),
595+
throwsToolExit(
596+
message:
597+
'You might be having a permissions issue with your IDE. '
598+
'Please try going to '
599+
'System Settings -> Privacy & Security -> Local Network -> '
600+
'[Find your IDE] -> Toggle ON, then restart your phone.',
601+
)
602+
);
603+
}, skip: !globals.platform.isMacOS); // [intended] This tool exit message only works for macOS
604+
580605
testWithoutContext('Correctly builds VM Service URI with hostVmservicePort == 0', () async {
581606
final MDnsClient client = FakeMDnsClient(
582607
<PtrResourceRecord>[
@@ -991,13 +1016,15 @@ class FakeMDnsClient extends Fake implements MDnsClient {
9911016
this.txtResponse = const <String, List<TxtResourceRecord>>{},
9921017
this.ipResponse = const <String, List<IPAddressResourceRecord>>{},
9931018
this.osErrorOnStart = false,
1019+
this.socketExceptionOnStart = false
9941020
});
9951021

9961022
final List<PtrResourceRecord> ptrRecords;
9971023
final Map<String, List<SrvResourceRecord>> srvResponse;
9981024
final Map<String, List<TxtResourceRecord>> txtResponse;
9991025
final Map<String, List<IPAddressResourceRecord>> ipResponse;
10001026
final bool osErrorOnStart;
1027+
final bool socketExceptionOnStart;
10011028

10021029
@override
10031030
Future<void> start({
@@ -1016,6 +1043,9 @@ class FakeMDnsClient extends Fake implements MDnsClient {
10161043
ResourceRecordQuery query, {
10171044
Duration timeout = const Duration(seconds: 5),
10181045
}) {
1046+
if (socketExceptionOnStart) {
1047+
throw const SocketException('Socket Exception');
1048+
}
10191049
if (T == PtrResourceRecord && query.fullyQualifiedName == MDnsVmServiceDiscovery.dartVmServiceName) {
10201050
return Stream<PtrResourceRecord>.fromIterable(ptrRecords) as Stream<T>;
10211051
}

0 commit comments

Comments
 (0)