Skip to content

Commit 84138f9

Browse files
bkonyisuryaKommana2662
authored andcommitted
Replace dynamic free-port probing with OS port-0 binding (flutter#194419)
Replaces dynamic socket free-port probing (`OperatingSystemUtils.findFreePort` and `findAvailablePortAndPossiblyCauseFlakyTests`) with OS port-0 binding across Chrome, ChromeDriver, and web test runners to prevent TOCTOU race conditions and test flakes. Part of flutter#194313
1 parent 0ec21dd commit 84138f9

10 files changed

Lines changed: 188 additions & 173 deletions

File tree

‎dev/bots/browser.dart‎

Lines changed: 8 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -10,26 +10,20 @@ import 'package:shelf/shelf.dart';
1010
import 'package:shelf/shelf_io.dart' as shelf_io;
1111
import 'package:shelf_static/shelf_static.dart';
1212

13-
/// Runs Chrome, opens the given `appUrl`, and returns the result reported by the
14-
/// app.
13+
/// Runs Chrome and returns the result reported by the app.
1514
///
1615
/// The app is served from the `appDirectory`. Typically, the app is built
1716
/// using `flutter build web` and served from `build/web`.
1817
///
1918
/// The launched app is expected to report the result by sending an HTTP POST
2019
/// request to "/test-result" containing result data as plain text body of the
2120
/// request. This function has no opinion about what that string contains.
22-
Future<String> evalTestAppInChrome({
23-
required String appUrl,
24-
required String appDirectory,
25-
int serverPort = 8080,
26-
int browserDebugPort = 8081,
27-
}) async {
21+
Future<String> evalTestAppInChrome({required String appDirectory}) async {
2822
io.HttpServer? server;
2923
Chrome? chrome;
3024
try {
3125
final resultCompleter = Completer<String>();
32-
server = await io.HttpServer.bind('localhost', serverPort);
26+
server = await io.HttpServer.bind('localhost', 0);
3327
final Cascade cascade = Cascade()
3428
.add((Request request) async {
3529
if (request.requestedUri.path.endsWith('/test-result')) {
@@ -46,8 +40,8 @@ Future<String> evalTestAppInChrome({
4640
chrome = await Chrome.launch(
4741
ChromeOptions(
4842
headless: true,
49-
debugPort: browserDebugPort,
50-
url: appUrl,
43+
debugPort: 0,
44+
url: 'http://localhost:${server.port}/index.html',
5145
userDataDirectory: userDataDirectory.path,
5246
windowHeight: 500,
5347
windowWidth: 500,
@@ -67,17 +61,14 @@ class AppServer {
6761
AppServer._(this._server, this.chrome, this.onChromeError);
6862

6963
static Future<AppServer> start({
70-
required String appUrl,
7164
required String appDirectory,
7265
required String cacheControl,
73-
int serverPort = 8080,
74-
int browserDebugPort = 8081,
7566
bool headless = true,
7667
List<Handler>? additionalRequestHandlers,
7768
}) async {
7869
io.HttpServer server;
7970
Chrome chrome;
80-
server = await io.HttpServer.bind('localhost', serverPort);
71+
server = await io.HttpServer.bind('localhost', 0);
8172
final Handler staticHandler = createStaticHandler(appDirectory, defaultDocument: 'index.html');
8273
var cascade = Cascade();
8374
if (additionalRequestHandlers != null) {
@@ -97,8 +88,8 @@ class AppServer {
9788
chrome = await Chrome.launch(
9889
ChromeOptions(
9990
headless: headless,
100-
debugPort: browserDebugPort,
101-
url: appUrl,
91+
debugPort: 0,
92+
url: 'http://localhost:${server.port}/index.html',
10293
userDataDirectory: userDataDirectory.path,
10394
),
10495
onError: chromeErrorCompleter.complete,

‎dev/bots/service_worker_test.dart‎

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -122,14 +122,9 @@ self.addEventListener('fetch', (event) => {
122122
}
123123

124124
Future<AppServer> _startServer({required bool headless}) async {
125-
final int serverPort = await findAvailablePortAndPossiblyCauseFlakyTests();
126-
final int browserDebugPort = await findAvailablePortAndPossiblyCauseFlakyTests();
127125
return AppServer.start(
128126
headless: headless,
129127
appDirectory: _appBuildDirectory,
130-
serverPort: serverPort,
131-
browserDebugPort: browserDebugPort,
132-
appUrl: 'http://localhost:$serverPort/index.html',
133128
cacheControl: 'max-age=0',
134129
additionalRequestHandlers: <Handler>[
135130
(Request request) {

‎dev/bots/suite_runners/run_web_tests.dart‎

Lines changed: 2 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -436,14 +436,7 @@ class WebTestsSuite {
436436
);
437437

438438
// Run the app.
439-
final int serverPort = await findAvailablePortAndPossiblyCauseFlakyTests();
440-
final int browserDebugPort = await findAvailablePortAndPossiblyCauseFlakyTests();
441-
final String result = await evalTestAppInChrome(
442-
appUrl: 'http://localhost:$serverPort/index.html',
443-
appDirectory: appBuildDirectory,
444-
serverPort: serverPort,
445-
browserDebugPort: browserDebugPort,
446-
);
439+
final String result = await evalTestAppInChrome(appDirectory: appBuildDirectory);
447440

448441
if (!result.contains('--- TEST SUCCEEDED ---')) {
449442
foundError(<String>[result, '${red}Web stack trace integration test failed.$reset']);
@@ -529,14 +522,7 @@ class WebTestsSuite {
529522
);
530523

531524
// Run the app.
532-
final int serverPort = await findAvailablePortAndPossiblyCauseFlakyTests();
533-
final int browserDebugPort = await findAvailablePortAndPossiblyCauseFlakyTests();
534-
final String result = await evalTestAppInChrome(
535-
appUrl: 'http://localhost:$serverPort/index.html',
536-
appDirectory: appBuildDirectory,
537-
serverPort: serverPort,
538-
browserDebugPort: browserDebugPort,
539-
);
525+
final String result = await evalTestAppInChrome(appDirectory: appBuildDirectory);
540526

541527
if (!result.contains('--- TEST SUCCEEDED ---')) {
542528
foundError(<String>[result, '${red}Web release mode test failed.$reset']);

‎dev/bots/utils.dart‎

Lines changed: 0 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -318,34 +318,6 @@ void _printLoudly(String message) {
318318
}
319319
}
320320

321-
// THE FOLLOWING CODE IS A VIOLATION OF OUR STYLE GUIDE
322-
// BECAUSE IT INTRODUCES A VERY FLAKY RACE CONDITION
323-
// https://github.com/flutter/flutter/blob/main/docs/contributing/Style-guide-for-Flutter-repo.md#never-check-if-a-port-is-available-before-using-it-never-add-timeouts-and-other-race-conditions
324-
// DO NOT USE THE FOLLOWING FUNCTIONS
325-
// DO NOT WRITE CODE LIKE THE FOLLOWING FUNCTIONS
326-
// https://github.com/flutter/flutter/issues/109474
327-
328-
int _portCounter = 8080;
329-
330-
/// Finds the next available local port.
331-
Future<int> findAvailablePortAndPossiblyCauseFlakyTests() async {
332-
while (!await _isPortAvailable(_portCounter)) {
333-
_portCounter += 1;
334-
}
335-
return _portCounter++;
336-
}
337-
338-
Future<bool> _isPortAvailable(int port) async {
339-
try {
340-
final RawSocket socket = await RawSocket.connect('localhost', port);
341-
socket.shutdown(SocketDirection.both);
342-
await socket.close();
343-
return false;
344-
} on SocketException {
345-
return true;
346-
}
347-
}
348-
349321
String locationInFile(ResolvedUnitResult unit, AstNode node, String workingDirectory) {
350322
return '${path.relative(path.relative(unit.path, from: workingDirectory))}:${unit.lineInfo.getLocation(node.offset).lineNumber}';
351323
}

‎dev/devicelab/lib/framework/browser.dart‎

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,7 @@ class Chrome {
148148
if (jsFlags.isNotEmpty) '--js-flags=$jsFlags',
149149
];
150150

151-
final io.Process chromeProcess = await _spawnChromiumProcess(
151+
final (:io.Process process, :int? remoteDebuggingPort) = await _spawnChromiumProcess(
152152
_findSystemChromeExecutable(),
153153
args,
154154
silent: options.silent,
@@ -157,10 +157,15 @@ class Chrome {
157157

158158
WipConnection? debugConnection;
159159
if (withDebugging) {
160-
debugConnection = await _connectToChromeDebugPort(options.debugPort!, options.url);
160+
final int? port = options.debugPort == 0 ? remoteDebuggingPort : options.debugPort;
161+
if (port == null || port == 0) {
162+
process.kill();
163+
throw StateError('Failed to determine Chrome remote debugging port.');
164+
}
165+
debugConnection = await _connectToChromeDebugPort(port, options.url);
161166
}
162167

163-
return Chrome._(chromeProcess, onError, debugConnection);
168+
return Chrome._(process, onError, debugConnection);
164169
}
165170

166171
/// Connects to an existing Chrome process with the given [options].
@@ -701,7 +706,7 @@ bool _isDbusError(String line) {
701706
line.contains('org.freedesktop.DBus');
702707
}
703708

704-
Future<io.Process> _spawnChromiumProcess(
709+
Future<({io.Process process, int? remoteDebuggingPort})> _spawnChromiumProcess(
705710
String executable,
706711
List<String> args, {
707712
required bool silent,
@@ -726,7 +731,7 @@ Future<io.Process> _spawnChromiumProcess(
726731
// Wait until the DevTools are listening before trying to connect. This is
727732
// only required for flutter_test --platform=chrome and not flutter run.
728733
var hitGlibcBug = false;
729-
await process.stderr
734+
final String devToolsLine = await process.stderr
730735
.transform(utf8.decoder)
731736
.transform(const LineSplitter())
732737
.where((String line) => !_isDbusError(line))
@@ -758,8 +763,16 @@ Future<io.Process> _spawnChromiumProcess(
758763
},
759764
);
760765

766+
int? remoteDebuggingPort;
767+
final RegExpMatch? devToolsMatch = RegExp(r'DevTools listening on (ws://[^\s]+)')
768+
.firstMatch(devToolsLine);
769+
if (devToolsMatch != null) {
770+
final Uri uri = Uri.parse(devToolsMatch.group(1)!);
771+
remoteDebuggingPort = uri.port;
772+
}
773+
761774
if (!hitGlibcBug) {
762-
return process;
775+
return (process: process, remoteDebuggingPort: remoteDebuggingPort);
763776
}
764777

765778
// A precaution that avoids accumulating browser processes, in case the

‎packages/flutter_tools/lib/src/isolated/resident_web_runner.dart‎

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -266,19 +266,16 @@ class ResidentWebRunner extends ResidentRunner {
266266
:bool crossOriginIsolation,
267267
) = debuggingOptions.webConfig;
268268

269-
final WebDevServerConfig originalConfig = devServerConfig ?? const WebDevServerConfig();
270-
271-
final int resolvedPort = await resolvePort(originalConfig.port, osUtils);
272-
273-
final WebDevServerConfig updatedConfig = originalConfig.copyWith(port: resolvedPort);
269+
final WebDevServerConfig serverConfig = devServerConfig ?? const WebDevServerConfig();
270+
validatePort(serverConfig.port);
274271
final ExpressionCompiler? expressionCompiler = enableExpressionEvaluation
275272
? WebExpressionCompiler(flutterDevice!.generator!, fileSystem: fileSystem)
276273
: null;
277274

278275
flutterDevice!.developmentShaderCompiler.configureCompiler(TargetPlatform.web_javascript);
279276

280277
flutterDevice!.devFS = WebDevFS(
281-
webDevServerConfig: updatedConfig,
278+
webDevServerConfig: serverConfig,
282279
packagesFilePath: packagesFilePath,
283280
urlTunneller: _urlTunneller,
284281
useSseForDebugProxy: useSseForDebugProxy,
@@ -308,7 +305,7 @@ class ResidentWebRunner extends ResidentRunner {
308305
webDefines: _webDefines,
309306
);
310307
Uri url = await flutterDevice!.devFS!.create();
311-
if (updatedConfig.https?.certKeyPath != null && updatedConfig.https?.certPath != null) {
308+
if (serverConfig.https?.certKeyPath != null && serverConfig.https?.certPath != null) {
312309
url = url.replace(scheme: 'https');
313310
}
314311
if (debuggingOptions.buildInfo.isDebug && !useWasm) {

0 commit comments

Comments
 (0)