Revert "Remove race conditions involving finding available ports (#18488)" (#18521)

This reverts commit 7750872251.
This commit is contained in:
Ian Hickson
2018-06-15 15:37:20 -07:00
committed by GitHub
parent 7750872251
commit 0fa5ba43a1
22 changed files with 242 additions and 226 deletions

View File

@@ -28,8 +28,9 @@ Future<Map<String, dynamic>> runTask(String taskName, { bool silent = false }) a
if (!file(taskExecutable).existsSync())
throw 'Executable Dart file not found: $taskExecutable';
final int vmServicePort = await findAvailablePort();
final Process runner = await startProcess(dartBin, <String>[
'--enable-vm-service=0',
'--enable-vm-service=$vmServicePort',
'--no-pause-isolates-on-exit',
taskExecutable,
]);
@@ -40,17 +41,10 @@ Future<Map<String, dynamic>> runTask(String taskName, { bool silent = false }) a
runnerFinished = true;
});
final Completer<int> port = new Completer<int>();
final StreamSubscription<String> stdoutSub = runner.stdout
.transform(const Utf8Decoder())
.transform(const LineSplitter())
.listen((String line) {
if (!port.isCompleted) {
final int portValue = parseServicePort(line, prefix: 'Observatory listening on ');
if (portValue != null)
port.complete(portValue);
}
if (!silent) {
stdout.writeln('[$taskName] [STDOUT] $line');
}
@@ -65,7 +59,7 @@ Future<Map<String, dynamic>> runTask(String taskName, { bool silent = false }) a
String waitingFor = 'connection';
try {
final VMIsolateRef isolate = await _connectToRunnerIsolate(await port.future);
final VMIsolateRef isolate = await _connectToRunnerIsolate(vmServicePort);
waitingFor = 'task completion';
final Map<String, dynamic> taskResult =
await isolate.invokeExtension('ext.cocoonRunTask').timeout(taskTimeoutWithGracePeriod);

View File

@@ -480,6 +480,21 @@ Future<Null> runAndCaptureAsyncStacks(Future<Null> callback()) {
return completer.future;
}
/// Return an unused TCP port number.
Future<int> findAvailablePort() async {
int port = 20000;
while (true) {
try {
final ServerSocket socket =
await ServerSocket.bind(InternetAddress.LOOPBACK_IP_V4, port); // ignore: deprecated_member_use
await socket.close();
return port;
} catch (_) {
port++;
}
}
}
bool canRun(String path) => _processManager.canRun(path);
String extractCloudAuthTokenArg(List<String> rawArgs) {
@@ -502,20 +517,13 @@ String extractCloudAuthTokenArg(List<String> rawArgs) {
return token;
}
/// Tries to extract a port from the string.
///
/// The `prefix`, if specified, is a regular expression pattern and must not contain groups.
///
/// The `multiLine` flag should be set to true if `line` is actually a buffer of many lines.
int parseServicePort(String line, {
String prefix = 'An Observatory debugger .* is available at: ',
bool multiLine = false,
}) {
// e.g. "An Observatory debugger and profiler on ... is available at: http://127.0.0.1:8100/"
final RegExp pattern = new RegExp('$prefix(\\S+:(\\d+)/\\S*)\$', multiLine: multiLine);
final Match match = pattern.firstMatch(line);
print(pattern);
print(match);
// "An Observatory debugger and profiler on ... is available at: http://127.0.0.1:8100/"
final RegExp _kObservatoryRegExp = new RegExp(r'An Observatory debugger .* is available at: (\S+:(\d+))');
bool lineContainsServicePort(String line) => line.contains(_kObservatoryRegExp);
int parseServicePort(String line) {
final Match match = _kObservatoryRegExp.firstMatch(line);
return match == null ? null : int.parse(match.group(2));
}