Unverified Commit 0cd2ece5 authored by Zachary Anderson's avatar Zachary Anderson Committed by GitHub

[flutter_tools] Hanlde OSError in places where we've seen it thrown (#52491)

parent 183da8f8
...@@ -78,6 +78,7 @@ export 'dart:io' ...@@ -78,6 +78,7 @@ export 'dart:io'
IOSink, IOSink,
// Link NO! Use `file_system.dart` // Link NO! Use `file_system.dart`
// NetworkInterface NO! Use `io.dart` // NetworkInterface NO! Use `io.dart`
OSError,
pid, pid,
// Platform NO! use `platform.dart` // Platform NO! use `platform.dart`
Process, Process,
......
...@@ -319,7 +319,12 @@ class _DevFSHttpWriter { ...@@ -319,7 +319,12 @@ class _DevFSHttpWriter {
onError: (dynamic error) { globals.printTrace('error: $error'); }, onError: (dynamic error) { globals.printTrace('error: $error'); },
cancelOnError: true); cancelOnError: true);
break; break;
} on Exception catch (error, trace) { } catch (error, trace) { // ignore: avoid_catches_without_on_clauses
// We treat OSError as an Exception.
// See: https://github.com/dart-lang/sdk/issues/40934
if (error is! Exception && error is! OSError) {
rethrow;
}
if (!_completer.isCompleted) { if (!_completer.isCompleted) {
globals.printTrace('Error writing "$deviceUri" to DevFS: $error'); globals.printTrace('Error writing "$deviceUri" to DevFS: $error');
if (retry > 0) { if (retry > 0) {
......
...@@ -133,6 +133,11 @@ class MDnsObservatoryDiscovery { ...@@ -133,6 +133,11 @@ class MDnsObservatoryDiscovery {
authCode += '/'; authCode += '/';
} }
return MDnsObservatoryDiscoveryResult(srv.first.port, authCode); return MDnsObservatoryDiscoveryResult(srv.first.port, authCode);
} on OSError catch (e) {
// OSError is neither an Error nor and Exception, so we wrap it in a
// SocketException and rethrow.
// See: https://github.com/dart-lang/sdk/issues/40934
throw SocketException('mdns query failed', osError: e);
} finally { } finally {
client.stop(); client.stop();
} }
......
...@@ -109,7 +109,13 @@ void main() { ...@@ -109,7 +109,13 @@ void main() {
HttpOverrides.global = savedHttpOverrides; HttpOverrides.global = savedHttpOverrides;
}); });
testUsingContext('retry uploads when failure', () async { final List<dynamic> exceptions = <dynamic>[
Exception('Connection resert by peer'),
const OSError('Connection reset by peer'),
];
for (final dynamic exception in exceptions) {
testUsingContext('retry uploads when failure: $exception', () async {
final File file = fs.file(fs.path.join(basePath, filePath)); final File file = fs.file(fs.path.join(basePath, filePath));
await file.parent.create(recursive: true); await file.parent.create(recursive: true);
file.writeAsBytesSync(<int>[1, 2, 3]); file.writeAsBytesSync(<int>[1, 2, 3]);
...@@ -136,7 +142,7 @@ void main() { ...@@ -136,7 +142,7 @@ void main() {
const int kFailedAttempts = 5; const int kFailedAttempts = 5;
when(httpRequest.close()).thenAnswer((Invocation invocation) { when(httpRequest.close()).thenAnswer((Invocation invocation) {
if (nRequest++ < kFailedAttempts) { if (nRequest++ < kFailedAttempts) {
throw Exception('Connection resert by peer'); throw exception;
} }
return Future<HttpClientResponse>.value(httpClientResponse); return Future<HttpClientResponse>.value(httpClientResponse);
}); });
...@@ -162,6 +168,7 @@ void main() { ...@@ -162,6 +168,7 @@ void main() {
HttpClientFactory: () => () => httpClient, HttpClientFactory: () => () => httpClient,
ProcessManager: () => FakeProcessManager.any(), ProcessManager: () => FakeProcessManager.any(),
}); });
}
}); });
group('devfs remote', () { group('devfs remote', () {
......
...@@ -192,6 +192,21 @@ void main() { ...@@ -192,6 +192,21 @@ void main() {
final int port = (await portDiscovery.query(applicationId: 'bar'))?.port; final int port = (await portDiscovery.query(applicationId: 'bar'))?.port;
expect(port, isNull); expect(port, isNull);
}); });
testUsingContext('Throws SocketException when client throws OSError on start', () async {
final MDnsClient client = MockMDnsClient();
when(client.start()).thenAnswer((_) {
throw const OSError('Operation not suppoted on socket', 102);
});
final MDnsObservatoryDiscovery portDiscovery = MDnsObservatoryDiscovery(
mdnsClient: client,
);
expect(
() async => await portDiscovery.query(),
throwsA(isA<SocketException>()),
);
});
}); });
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment