forked from firka/flutter
Stop leaking iproxy processes (#42026)
This commit is contained in:
committed by
GitHub
parent
b770f3443d
commit
ed482c3e09
@@ -12,6 +12,7 @@ import 'application_package.dart';
|
||||
import 'artifacts.dart';
|
||||
import 'base/context.dart';
|
||||
import 'base/file_system.dart';
|
||||
import 'base/io.dart';
|
||||
import 'base/utils.dart';
|
||||
import 'build_info.dart';
|
||||
import 'fuchsia/fuchsia_device.dart';
|
||||
@@ -473,6 +474,11 @@ abstract class Device {
|
||||
static Future<void> printDevices(List<Device> devices) async {
|
||||
await descriptions(devices).forEach(printStatus);
|
||||
}
|
||||
|
||||
/// Clean up resources allocated by device
|
||||
///
|
||||
/// For example log readers or port forwarders.
|
||||
void dispose() {}
|
||||
}
|
||||
|
||||
class DebuggingOptions {
|
||||
@@ -564,6 +570,15 @@ class ForwardedPort {
|
||||
|
||||
@override
|
||||
String toString() => 'ForwardedPort HOST:$hostPort to DEVICE:$devicePort';
|
||||
|
||||
/// Kill subprocess (if present) used in forwarding.
|
||||
void dispose() {
|
||||
final Process process = context;
|
||||
|
||||
if (process != null) {
|
||||
process.kill();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Forward ports from the host machine to the device.
|
||||
@@ -579,6 +594,9 @@ abstract class DevicePortForwarder {
|
||||
|
||||
/// Stops forwarding [forwardedPort].
|
||||
Future<void> unforward(ForwardedPort forwardedPort);
|
||||
|
||||
/// Cleanup allocated resources, like forwardedPorts
|
||||
Future<void> dispose() async { }
|
||||
}
|
||||
|
||||
/// Read the log for a particular device.
|
||||
@@ -593,6 +611,9 @@ abstract class DeviceLogReader {
|
||||
|
||||
/// Process ID of the app on the device.
|
||||
int appPid;
|
||||
|
||||
// Clean up resources allocated by log reader e.g. subprocesses
|
||||
void dispose() { }
|
||||
}
|
||||
|
||||
/// Describes an app running on the device.
|
||||
@@ -614,6 +635,9 @@ class NoOpDeviceLogReader implements DeviceLogReader {
|
||||
|
||||
@override
|
||||
Stream<String> get logLines => const Stream<String>.empty();
|
||||
|
||||
@override
|
||||
void dispose() { }
|
||||
}
|
||||
|
||||
// A portforwarder which does not support forwarding ports.
|
||||
@@ -628,4 +652,7 @@ class NoOpDevicePortForwarder implements DevicePortForwarder {
|
||||
|
||||
@override
|
||||
Future<void> unforward(ForwardedPort forwardedPort) async { }
|
||||
|
||||
@override
|
||||
Future<void> dispose() async { }
|
||||
}
|
||||
|
||||
@@ -14,7 +14,6 @@ import '../base/io.dart';
|
||||
import '../base/logger.dart';
|
||||
import '../base/platform.dart';
|
||||
import '../base/process.dart';
|
||||
import '../base/process_manager.dart';
|
||||
import '../build_info.dart';
|
||||
import '../convert.dart';
|
||||
import '../device.dart';
|
||||
@@ -417,7 +416,7 @@ class IOSDevice extends Device {
|
||||
@override
|
||||
DeviceLogReader getLogReader({ ApplicationPackage app }) {
|
||||
_logReaders ??= <ApplicationPackage, DeviceLogReader>{};
|
||||
return _logReaders.putIfAbsent(app, () => _IOSDeviceLogReader(this, app));
|
||||
return _logReaders.putIfAbsent(app, () => IOSDeviceLogReader(this, app));
|
||||
}
|
||||
|
||||
@visibleForTesting
|
||||
@@ -427,7 +426,7 @@ class IOSDevice extends Device {
|
||||
}
|
||||
|
||||
@override
|
||||
DevicePortForwarder get portForwarder => _portForwarder ??= _IOSDevicePortForwarder(this);
|
||||
DevicePortForwarder get portForwarder => _portForwarder ??= IOSDevicePortForwarder(this);
|
||||
|
||||
@visibleForTesting
|
||||
set portForwarder(DevicePortForwarder forwarder) {
|
||||
@@ -449,6 +448,14 @@ class IOSDevice extends Device {
|
||||
bool isSupportedForProject(FlutterProject flutterProject) {
|
||||
return flutterProject.ios.existsSync();
|
||||
}
|
||||
|
||||
@override
|
||||
void dispose() {
|
||||
_logReaders.forEach((ApplicationPackage application, DeviceLogReader logReader) {
|
||||
logReader.dispose();
|
||||
});
|
||||
_portForwarder?.dispose();
|
||||
}
|
||||
}
|
||||
|
||||
/// Decodes a vis-encoded syslog string to a UTF-8 representation.
|
||||
@@ -511,11 +518,12 @@ String decodeSyslog(String line) {
|
||||
}
|
||||
}
|
||||
|
||||
class _IOSDeviceLogReader extends DeviceLogReader {
|
||||
_IOSDeviceLogReader(this.device, ApplicationPackage app) {
|
||||
@visibleForTesting
|
||||
class IOSDeviceLogReader extends DeviceLogReader {
|
||||
IOSDeviceLogReader(this.device, ApplicationPackage app) {
|
||||
_linesController = StreamController<String>.broadcast(
|
||||
onListen: _start,
|
||||
onCancel: _stop,
|
||||
onCancel: dispose,
|
||||
);
|
||||
|
||||
// Match for lines for the runner in syslog.
|
||||
@@ -538,7 +546,6 @@ class _IOSDeviceLogReader extends DeviceLogReader {
|
||||
RegExp _anyLineRegex;
|
||||
|
||||
StreamController<String> _linesController;
|
||||
Process _process;
|
||||
|
||||
@override
|
||||
Stream<String> get logLines => _linesController.stream;
|
||||
@@ -548,17 +555,22 @@ class _IOSDeviceLogReader extends DeviceLogReader {
|
||||
|
||||
void _start() {
|
||||
iMobileDevice.startLogger(device.id).then<void>((Process process) {
|
||||
_process = process;
|
||||
_process.stdout.transform<String>(utf8.decoder).transform<String>(const LineSplitter()).listen(_newLineHandler());
|
||||
_process.stderr.transform<String>(utf8.decoder).transform<String>(const LineSplitter()).listen(_newLineHandler());
|
||||
_process.exitCode.whenComplete(() {
|
||||
process.stdout.transform<String>(utf8.decoder).transform<String>(const LineSplitter()).listen(_newLineHandler());
|
||||
process.stderr.transform<String>(utf8.decoder).transform<String>(const LineSplitter()).listen(_newLineHandler());
|
||||
process.exitCode.whenComplete(() {
|
||||
if (_linesController.hasListener) {
|
||||
_linesController.close();
|
||||
}
|
||||
});
|
||||
assert(_idevicesyslogProcess == null);
|
||||
_idevicesyslogProcess = process;
|
||||
});
|
||||
}
|
||||
|
||||
@visibleForTesting
|
||||
set idevicesyslogProcess(Process process) => _idevicesyslogProcess = process;
|
||||
Process _idevicesyslogProcess;
|
||||
|
||||
// Returns a stateful line handler to properly capture multi-line output.
|
||||
//
|
||||
// For multi-line log messages, any line after the first is logged without
|
||||
@@ -590,13 +602,15 @@ class _IOSDeviceLogReader extends DeviceLogReader {
|
||||
};
|
||||
}
|
||||
|
||||
void _stop() {
|
||||
_process?.kill();
|
||||
@override
|
||||
void dispose() {
|
||||
_idevicesyslogProcess?.kill();
|
||||
}
|
||||
}
|
||||
|
||||
class _IOSDevicePortForwarder extends DevicePortForwarder {
|
||||
_IOSDevicePortForwarder(this.device) : _forwardedPorts = <ForwardedPort>[];
|
||||
@visibleForTesting
|
||||
class IOSDevicePortForwarder extends DevicePortForwarder {
|
||||
IOSDevicePortForwarder(this.device) : _forwardedPorts = <ForwardedPort>[];
|
||||
|
||||
final IOSDevice device;
|
||||
|
||||
@@ -605,6 +619,11 @@ class _IOSDevicePortForwarder extends DevicePortForwarder {
|
||||
@override
|
||||
List<ForwardedPort> get forwardedPorts => _forwardedPorts;
|
||||
|
||||
@visibleForTesting
|
||||
void addForwardedPorts(List<ForwardedPort> forwardedPorts) {
|
||||
forwardedPorts.forEach(_forwardedPorts.add);
|
||||
}
|
||||
|
||||
static const Duration _kiProxyPortForwardTimeout = Duration(seconds: 1);
|
||||
|
||||
@override
|
||||
@@ -618,7 +637,7 @@ class _IOSDevicePortForwarder extends DevicePortForwarder {
|
||||
|
||||
bool connected = false;
|
||||
while (!connected) {
|
||||
printTrace('attempting to forward device port $devicePort to host port $hostPort');
|
||||
printTrace('Attempting to forward device port $devicePort to host port $hostPort');
|
||||
// Usage: iproxy LOCAL_TCP_PORT DEVICE_TCP_PORT UDID
|
||||
process = await processUtils.start(
|
||||
<String>[
|
||||
@@ -634,6 +653,7 @@ class _IOSDevicePortForwarder extends DevicePortForwarder {
|
||||
// TODO(ianh): This is a flakey race condition, https://github.com/libimobiledevice/libimobiledevice/issues/674
|
||||
connected = !await process.stdout.isEmpty.timeout(_kiProxyPortForwardTimeout, onTimeout: () => false);
|
||||
if (!connected) {
|
||||
process.kill();
|
||||
if (autoselect) {
|
||||
hostPort += 1;
|
||||
if (hostPort > 65535) {
|
||||
@@ -663,13 +683,13 @@ class _IOSDevicePortForwarder extends DevicePortForwarder {
|
||||
}
|
||||
|
||||
printTrace('Unforwarding port $forwardedPort');
|
||||
forwardedPort.dispose();
|
||||
}
|
||||
|
||||
final Process process = forwardedPort.context;
|
||||
|
||||
if (process != null) {
|
||||
processManager.killPid(process.pid);
|
||||
} else {
|
||||
printError('Forwarded port did not have a valid process');
|
||||
@override
|
||||
Future<void> dispose() async {
|
||||
for (ForwardedPort forwardedPort in _forwardedPorts) {
|
||||
forwardedPort.dispose();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -60,6 +60,7 @@ class MDnsObservatoryDiscovery {
|
||||
)
|
||||
.toList();
|
||||
if (pointerRecords.isEmpty) {
|
||||
printTrace('No pointer records found.');
|
||||
return null;
|
||||
}
|
||||
// We have no guarantee that we won't get multiple hits from the same
|
||||
|
||||
@@ -48,7 +48,13 @@ class ProtocolDiscovery {
|
||||
StreamSubscription<String> _deviceLogSubscription;
|
||||
|
||||
/// The discovered service URI.
|
||||
Future<Uri> get uri => _completer.future;
|
||||
///
|
||||
/// Port forwarding is only attempted when this is invoked, in case we never
|
||||
/// need to port forward.
|
||||
Future<Uri> get uri async {
|
||||
final Uri rawUri = await _completer.future;
|
||||
return await _forwardPort(rawUri);
|
||||
}
|
||||
|
||||
Future<void> cancel() => _stopScrapingLogs();
|
||||
|
||||
@@ -74,9 +80,8 @@ class ProtocolDiscovery {
|
||||
if (uri != null) {
|
||||
assert(!_completer.isCompleted);
|
||||
_stopScrapingLogs();
|
||||
_completer.complete(_forwardPort(uri));
|
||||
_completer.complete(uri);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Future<Uri> _forwardPort(Uri deviceUri) async {
|
||||
|
||||
@@ -1031,6 +1031,9 @@ class HotRunner extends ResidentRunner {
|
||||
|
||||
@override
|
||||
Future<void> cleanupAtFinish() async {
|
||||
for (FlutterDevice flutterDevice in flutterDevices) {
|
||||
flutterDevice.device.dispose();
|
||||
}
|
||||
await _cleanupDevFS();
|
||||
await stopEchoingDeviceLog();
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user