[flutter_tools] catch SocketException writing to ios-deploy stdin (#139784)

Fixes https://github.com/flutter/flutter/issues/139709

This adds a static helper method `ProcessUtils.writelnToStdinGuarded()`, which will asynchronously write to a sub-process's STDIN `IOSink` and catch errors.

In talking with Brian, it sounds like this is the best and most reliable way to catch `SocketException`s during these writes *to sub-process file descriptors* specifically (with a "real" hard drive file, the future returned by `.flush()` should complete with the write error).

Also, as I note in the dartdoc to `writelnToStdinGuarded()`, the behavior seems to be different between macOS and linux.

Moving forward, in any place where we want to catch exceptions writing to STDIN, we will want to use this new helper.
This commit is contained in:
Christopher Fujino
2023-12-11 16:32:18 -08:00
committed by GitHub
parent 815dc96e9b
commit fac41dde7f
4 changed files with 139 additions and 20 deletions

View File

@@ -9,12 +9,14 @@ import 'package:file/memory.dart';
import 'package:file_testing/file_testing.dart';
import 'package:flutter_tools/src/artifacts.dart';
import 'package:flutter_tools/src/base/file_system.dart';
import 'package:flutter_tools/src/base/io.dart';
import 'package:flutter_tools/src/base/logger.dart';
import 'package:flutter_tools/src/base/platform.dart';
import 'package:flutter_tools/src/base/process.dart';
import 'package:flutter_tools/src/cache.dart';
import 'package:flutter_tools/src/device.dart';
import 'package:flutter_tools/src/ios/ios_deploy.dart';
import 'package:test/fake.dart';
import '../../src/common.dart';
import '../../src/fake_process_manager.dart';
@@ -386,7 +388,26 @@ void main () {
);
expect(stdin.stream.transform<String>(const Utf8Decoder()), emits('process detach'));
await iosDeployDebugger.launchAndAttach();
iosDeployDebugger.detach();
await iosDeployDebugger.detach();
});
testWithoutContext('detach handles broken pipe', () async {
final StreamSink<List<int>> stdinSink = _ClosedStdinController();
final FakeProcessManager processManager = FakeProcessManager.list(<FakeCommand>[
FakeCommand(
command: const <String>['ios-deploy'],
stdout: '(lldb) run\nsuccess',
stdin: IOSink(stdinSink),
),
]);
final BufferLogger logger = BufferLogger.test();
final IOSDeployDebugger iosDeployDebugger = IOSDeployDebugger.test(
processManager: processManager,
logger: logger,
);
await iosDeployDebugger.launchAndAttach();
await iosDeployDebugger.detach();
expect(logger.traceText, contains('Could not detach from debugger'));
});
testWithoutContext('stop with backtrace', () async {
@@ -399,18 +420,28 @@ void main () {
],
stdout:
'(lldb) run\nsuccess\nLog on attach\n(lldb) Process 6156 stopped\n* thread #1, stop reason = Assertion failed:\n(lldb) Process 6156 detached',
stdin: IOSink(stdin.sink),
stdin: IOSink(stdin),
),
]);
final IOSDeployDebugger iosDeployDebugger = IOSDeployDebugger.test(
processManager: processManager,
);
await iosDeployDebugger.launchAndAttach();
await iosDeployDebugger.stopAndDumpBacktrace();
expect(await stdinStream.take(3).toList(), <String>[
List<String>? stdinLines;
// These two futures will deadlock if await-ed sequentially
await Future.wait(<Future<void>>[
iosDeployDebugger.stopAndDumpBacktrace(),
stdinStream.take(5).toList().then<void>(
(List<String> lines) => stdinLines = lines,
),
]);
expect(stdinLines, const <String>[
'thread backtrace all',
'\n',
'process detach',
'\n',
'process signal SIGSTOP',
]);
});
@@ -616,6 +647,11 @@ process continue
});
}
class _ClosedStdinController extends Fake implements StreamSink<List<int>> {
@override
Future<Object?> addStream(Stream<List<int>> stream) async => throw const SocketException('Bad pipe');
}
IOSDeploy setUpIOSDeploy(ProcessManager processManager, {
Artifacts? artifacts,
}) {

View File

@@ -1001,7 +1001,7 @@ class FakeIOSDeployDebugger extends Fake implements IOSDeployDebugger {
Stream<String> logLines = const Stream<String>.empty();
@override
void detach() {
Future<void> detach() async {
detached = true;
}
}