Skip to content

Commit fab33b0

Browse files
authored
Chrome fixes for VS code (#342)
- As suggested by the Chrome docs, always use a tmp dir when passing `--remote-debugging-port` - Add graceful exit handler for daemon command - This ensures that Chrome is properly closed - Temporary pin build_daemon version to fix integration tests - Fix concurrent modification issue Closes #319 Closes #300 This reverts #322 & #316
1 parent cb0df3b commit fab33b0

File tree

6 files changed

+35
-9
lines changed

6 files changed

+35
-9
lines changed

dwds/pubspec.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ dependencies:
2424

2525
dev_dependencies:
2626
args: ^1.0.0
27+
build_daemon: ^0.5.0
2728
build_runner: ^1.0.0
2829
build_web_compilers: '>=1.0.0 <3.0.0'
2930
test: ^1.6.0

dwds/test/test_context.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ class TestContext {
4848
}
4949
printOnFailure(line);
5050
});
51-
await assetReadyCompleter.future;
51+
await assetReadyCompleter.future.timeout(Duration(seconds: 60));
5252
appUrl = 'http://localhost:$port/hello_world/';
5353
var debugPort = await findUnusedPort();
5454
webDriver = await createDriver(desired: {

example/pubspec.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,3 +7,4 @@ environment:
77
dev_dependencies:
88
build_runner: '>=1.3.0 <2.0.0'
99
build_web_compilers: '>=1.0.0 <3.0.0'
10+
build_daemon: ^0.5.0

webdev/lib/src/command/daemon_command.dart

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import 'dart:convert';
77
import 'dart:io';
88

99
import 'package:args/command_runner.dart';
10+
import 'package:async/async.dart';
1011
import 'package:pedantic/pedantic.dart';
1112

1213
import '../daemon/app_domain.dart';
@@ -52,6 +53,14 @@ class DaemonCommand extends Command<int> {
5253
Future<int> run() async {
5354
Daemon daemon;
5455
DevWorkflow workflow;
56+
var cancelCount = 0;
57+
var cancelSub = StreamGroup.merge(
58+
[ProcessSignal.sigint.watch(), ProcessSignal.sigterm.watch()])
59+
.listen((signal) async {
60+
cancelCount++;
61+
daemon?.shutdown();
62+
if (cancelCount > 1) exit(1);
63+
});
5564
try {
5665
daemon = Daemon(_stdinCommandStream, _stdoutCommandResponse);
5766
var daemonDomain = DaemonDomain(daemon);
@@ -76,6 +85,7 @@ class DaemonCommand extends Command<int> {
7685
daemon?.shutdown();
7786
rethrow;
7887
} finally {
88+
unawaited(cancelSub.cancel());
7989
unawaited(workflow?.shutDown());
8090
}
8191
}

webdev/lib/src/serve/chrome.dart

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -46,20 +46,33 @@ var _currentCompleter = Completer<Chrome>();
4646
class Chrome {
4747
final int debugPort;
4848
final Process _process;
49+
final Directory _dataDir;
4950

5051
final ChromeConnection chromeConnection;
5152

5253
Chrome._(
5354
this.debugPort,
5455
this.chromeConnection, {
5556
Process process,
56-
}) : _process = process;
57+
Directory dataDir,
58+
}) : _process = process,
59+
_dataDir = dataDir;
5760

5861
Future<void> close() async {
5962
if (_currentCompleter.isCompleted) _currentCompleter = Completer<Chrome>();
6063
chromeConnection.close();
61-
_process?.kill();
64+
_process?.kill(ProcessSignal.sigkill);
6265
await _process?.exitCode;
66+
try {
67+
// Chrome starts another process as soon as it dies that modifies the
68+
// profile information. Give it some time before attempting to delete
69+
// the directory.
70+
await Future.delayed(Duration(milliseconds: 500));
71+
await _dataDir?.delete(recursive: true);
72+
} catch (_) {
73+
// Silently fail if we can't clean up the profile information.
74+
// It is a system tmp directory so it should get cleaned up eventually.
75+
}
6376
}
6477

6578
/// Connects to an instance of Chrome with an open debug port.
@@ -72,9 +85,7 @@ class Chrome {
7285
///
7386
/// Each url in [urls] will be loaded in a separate tab.
7487
static Future<Chrome> start(List<String> urls, {int port}) async {
75-
var dataDir = Directory(p.joinAll(
76-
[Directory.current.path, '.dart_tool', 'webdev', 'chrome_profile']))
77-
..createSync(recursive: true);
88+
var dataDir = Directory.systemTemp.createTempSync();
7889
port = port == null || port == 0 ? await findUnusedPort() : port;
7990
var args = [
8091
// Using a tmp directory ensures that a new instance of chrome launches
@@ -109,6 +120,7 @@ class Chrome {
109120
port,
110121
ChromeConnection('localhost', port),
111122
process: process,
123+
dataDir: dataDir,
112124
));
113125
}
114126

webdev/lib/src/serve/handlers/dev_handler.dart

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -50,9 +50,11 @@ class DevHandler {
5050

5151
Future<void> close() async {
5252
await _sub.cancel();
53-
for (var connection in _connections) {
54-
await connection.sink.close();
55-
}
53+
// We listen for connections to close and remove them from the connections
54+
// set. Therefore we shouldn't asynchronously iterate through the
55+
// connections.
56+
await Future.wait(
57+
_connections.map((connection) => connection.sink.close()));
5658
await Future.wait(_servicesByAppId.values.map((futureServices) async {
5759
await (await futureServices).close();
5860
}));

0 commit comments

Comments
 (0)