diff --git a/packages/jaspr/CHANGELOG.md b/packages/jaspr/CHANGELOG.md index e9c2ce9c6..22885e1b8 100644 --- a/packages/jaspr/CHANGELOG.md +++ b/packages/jaspr/CHANGELOG.md @@ -4,6 +4,8 @@ - Fixed a server built with `dart build cli` on Linux with Dart 3.14 or later not finding its `web` directory and failing with "Could not resolve project directory containing pubspec.yaml" when no `pubspec.yaml` is above it. Since Dart 3.14 such a bundle keeps the snapshot in `lib/libdartaot.so`, which `Platform.script` points at. - Fixed `jaspr serve` with Flutter embedding or plugins failing on Flutter 3.50, which moved the precompiled DDC SDK to `kernel/ddc/canary` and `kernel/ddc/stable`. +- Fixed `jaspr build` and `jaspr serve` stopping a build daemon that another Jaspr process in the same directory started, which left that process serving empty 503 responses. +- Fixed `jaspr serve` not noticing when its build daemon exits without a shutdown notification. It now stops with an error instead of answering every request with an empty 503. ## 0.24.0-beta.1 diff --git a/packages/jaspr_cli/lib/src/dev/client_runner.dart b/packages/jaspr_cli/lib/src/dev/client_runner.dart index 7632274a7..71ce7d907 100644 --- a/packages/jaspr_cli/lib/src/dev/client_runner.dart +++ b/packages/jaspr_cli/lib/src/dev/client_runner.dart @@ -23,7 +23,7 @@ class ClientRunner { void startBuild() => client.startBuild(); - ClientRunner._(this.client, this.daemonPort, this.logger, [this.daemonPids = const []]) { + ClientRunner._(this.client, this.daemonPort, this.logger, this.daemonPids, {required this.ownsDaemon}) { client.shutdownNotifications.listen((data) { logger.write(data.message, tag: Tag.builder, level: Level.warning); if (data.failureType == 75) { @@ -31,8 +31,24 @@ class ClientRunner { } shutDown(data.failureType); }); + // A daemon that is killed (e.g. by another process) sends no shutdown + // notification, the connection just closes. Without this, the proxy would + // keep answering every request with an empty 503. + client.finished.whenComplete(() { + if (_isShutDown) return; + logger.write( + 'The build daemon exited unexpectedly. Please restart Jaspr.', + tag: Tag.builder, + level: Level.critical, + ); + shutDown(1); + }).ignore(); } + /// Whether this process started the build daemon. A daemon that was already + /// running belongs to another Jaspr process and must not be stopped by us. + final bool ownsDaemon; + /// Starts the build daemon for development watching. static Future start( List buildOptions, @@ -40,26 +56,41 @@ class ClientRunner { void Function(FutureOr Function()) guard, ) async { var cancelled = false; + var ownsDaemon = false; + ClientRunner? runner; late final CancelableOperation op; guard(() async { cancelled = true; if (!op.isCompleted) { op.cancel(); } - await util.stopDaemon(Directory.current.path); + if (runner != null) { + // Shut the runner down before its daemon goes away, so that the + // closing connection is not reported as an unexpected exit. + await runner!.shutDown(); + } else if (ownsDaemon) { + await util.stopDaemon(Directory.current.path); + } }); op = CancelableOperation.fromFuture( Future(() async { final workingDirectory = Directory.current.path; - final client = await util.startBuildDaemon(workingDirectory, buildOptions, logger); - if (client == null) return null; + ownsDaemon = !await util.isDaemonRunning(workingDirectory); + final (:client, :foreignDaemon) = await util.startBuildDaemon(workingDirectory, buildOptions, logger); + if (client == null) { + // A daemon that was still starting up when we checked. + if (foreignDaemon) ownsDaemon = false; + return null; + } if (cancelled) { try { await client.close(); } catch (_) {} - await util.stopDaemon(workingDirectory); + if (ownsDaemon) { + await util.stopDaemon(workingDirectory); + } return null; } @@ -76,9 +107,7 @@ class ClientRunner { final daemonPort = util.daemonPort(workingDirectory); final daemonPids = await util.findPidsForPort(daemonPort); - final runner = ClientRunner._(client, daemonPort, logger, daemonPids); - guard(() => runner.shutDown()); - return runner; + return runner = ClientRunner._(client, daemonPort, logger, daemonPids, ownsDaemon: ownsDaemon); }), ); @@ -93,9 +122,16 @@ class ClientRunner { void Function(FutureOr Function())? guard, }) async { final workingDirectory = Directory.current.path; - final client = await util.startBuildDaemon(workingDirectory, buildOptions, logger); + // E.g. the daemon of a running `jaspr serve`. If its options don't match, + // connecting fails, and stopping it would break that other process. + final ownsDaemon = !await util.isDaemonRunning(workingDirectory); + final (:client, :foreignDaemon) = await util.startBuildDaemon(workingDirectory, buildOptions, logger); if (client == null) { - await util.stopDaemon(workingDirectory); + // `foreignDaemon` also covers a daemon that was still starting up when + // we checked. + if (ownsDaemon && !foreignDaemon) { + await util.stopDaemon(workingDirectory); + } return 1; } @@ -106,7 +142,9 @@ class ClientRunner { try { await client.close(); } catch (_) {} - await util.stopDaemon(workingDirectory, knownPids: daemonPids); + if (ownsDaemon) { + await util.stopDaemon(workingDirectory, knownPids: daemonPids); + } } guard?.call(cleanUp); @@ -164,7 +202,10 @@ class ClientRunner { try { await client.close(); } catch (_) {} - await util.stopDaemon(Directory.current.path, knownPids: daemonPids); + // A shared daemon stops on its own once its last client disconnects. + if (ownsDaemon) { + await util.stopDaemon(Directory.current.path, knownPids: daemonPids); + } if (!_doneCompleter.isCompleted) _doneCompleter.complete(code); } } diff --git a/packages/jaspr_cli/lib/src/dev/dev_workflow.dart b/packages/jaspr_cli/lib/src/dev/dev_workflow.dart index 398354c63..620ed7398 100644 --- a/packages/jaspr_cli/lib/src/dev/dev_workflow.dart +++ b/packages/jaspr_cli/lib/src/dev/dev_workflow.dart @@ -345,6 +345,16 @@ class DevWorkflow { } }); + // Also stop waiting when the build daemon goes away before the initial + // build completes. The listener that propagates `clientRunner.done` to the + // workflow is only installed after this wait. + clientRunner.done.then((_) { + if (!initialBuildCompleter.isCompleted) { + aborted = true; + initialBuildCompleter.completeError(Object()); + } + }); + clientRunner.startBuild(); final BuildResult initialResult; diff --git a/packages/jaspr_cli/lib/src/dev/util.dart b/packages/jaspr_cli/lib/src/dev/util.dart index 91d37e413..68c35a690 100644 --- a/packages/jaspr_cli/lib/src/dev/util.dart +++ b/packages/jaspr_cli/lib/src/dev/util.dart @@ -12,12 +12,21 @@ import '../process_runner.dart'; import '../project.dart'; /// Connects to the `build_runner` daemon. -Future startBuildDaemon(String workingDirectory, List buildOptions, Logger logger) async { +/// +/// [DaemonConnection.client] is `null` if connecting failed. +/// [DaemonConnection.foreignDaemon] is `true` if it failed because another +/// daemon with different options or another version is running in +/// [workingDirectory]. That daemon belongs to another process and must not be +/// stopped, even if it was still starting up when [isDaemonRunning] was checked. +Future startBuildDaemon(String workingDirectory, List buildOptions, Logger logger) async { logger.write('Connecting to the build daemon...', tag: Tag.builder, progress: ProgressState.running); final args = [dartExecutable, 'run', 'build_runner', 'daemon', ...buildOptions]; if (ProcessRunner.isDefault) { try { - return await BuildDaemonClient.connect(workingDirectory, args, logHandler: logger.writeServerLog); + return ( + client: await BuildDaemonClient.connect(workingDirectory, args, logHandler: logger.writeServerLog), + foreignDaemon: false, + ); } on OptionsSkew catch (e) { logger.write( 'Incompatible options with current running build daemon: ${e.details}\n\n' @@ -26,7 +35,7 @@ Future startBuildDaemon(String workingDirectory, List startBuildDaemon(String workingDirectory, List startBuildDaemon(String workingDirectory, List startBuildDaemon(String workingDirectory, List isDaemonRunning(String workingDirectory) async { + final port = getDaemonPort(workingDirectory); + if (port == null) return false; + try { + final socket = await Socket.connect(InternetAddress.loopbackIPv4, port, timeout: const Duration(seconds: 2)); + socket.destroy(); + return true; + } on SocketException { + return false; } } diff --git a/packages/jaspr_cli/test/helpers/daemon_process_test.dart b/packages/jaspr_cli/test/helpers/daemon_process_test.dart index 43d729703..a56614843 100644 --- a/packages/jaspr_cli/test/helpers/daemon_process_test.dart +++ b/packages/jaspr_cli/test/helpers/daemon_process_test.dart @@ -59,5 +59,24 @@ void main() { tempDir.deleteSync(recursive: true); } }); + + test('isDaemonRunning only reports a daemon that is listening on its port', () async { + final tempDir = Directory.systemTemp.createTempSync('daemon_running_test_'); + final server = await HttpServer.bind(InternetAddress.loopbackIPv4, 0); + final port = server.port; + try { + expect(await isDaemonRunning(tempDir.path), isFalse, reason: 'no port file'); + + Directory(daemonWorkspace(tempDir.path)).createSync(recursive: true); + File(portFilePath(tempDir.path)).writeAsStringSync('$port\ntoken'); + expect(await isDaemonRunning(tempDir.path), isTrue); + + await server.close(force: true); + expect(await isDaemonRunning(tempDir.path), isFalse, reason: 'leftover port file'); + } finally { + await server.close(force: true); + tempDir.deleteSync(recursive: true); + } + }); }); } diff --git a/packages/jaspr_cli/test/src/commands/serve_command_test.dart b/packages/jaspr_cli/test/src/commands/serve_command_test.dart index 0c305b26f..9658996fe 100644 --- a/packages/jaspr_cli/test/src/commands/serve_command_test.dart +++ b/packages/jaspr_cli/test/src/commands/serve_command_test.dart @@ -1,7 +1,9 @@ import 'dart:convert'; import 'dart:io'; +import 'package:build_daemon/data/build_request.dart'; import 'package:build_daemon/data/build_status.dart'; +import 'package:build_daemon/data/build_target_request.dart'; import 'package:jaspr_cli/src/command_runner.dart'; import 'package:jaspr_cli/src/project.dart'; import 'package:mocktail/mocktail.dart'; @@ -74,6 +76,152 @@ void main() { }); }); + test('stops with an error when the build daemon exits without notice', () async { + await io.runZoned(() async { + io.setupFakeProject('myapp', mode: 'client'); + io.stubDartSDK(); + + final buildDaemon = io.setupFakeBuildDaemon(verifyArgs: buildRunnerDartArgs); + + final serveResult = runner.run(['serve', '--verbose']); + + await expectLater( + io.stdout.queue, + emitsInOrderWithTimeout([ + 'Starting myapp in client rendering mode.', + '[BUILDER] Starting web compilers...', + '[BUILDER] Connecting to the build daemon...', + '[BUILDER] Starting initial build...', + ]), + ); + + await io.runInitialBuild(buildDaemon); + + await expectLater( + io.stdout.queue, + emitsInOrderWithTimeout([ + '[BUILDER] Done building web assets.', + '[LOG] Serving at http://localhost:8080', + ]), + ); + + // E.g. killed by another Jaspr process: no ShutdownNotification, the + // connection just closes. + await buildDaemon.close(); + + await expectLater( + io.stderr.queue, + emitsInOrderWithTimeout([ + '[BUILDER] [CRITICAL] The build daemon exited unexpectedly. Please restart Jaspr.', + ]), + ); + await expectLater( + io.stdout.queue, + emitsInOrderWithTimeout([ + 'Stopping web compilers...', + ]), + ); + + expect(await serveResult, equals(1)); + }); + }); + + test('stops with an error when the build daemon exits during the initial build', () async { + await io.runZoned(() async { + io.setupFakeProject('myapp', mode: 'client'); + io.stubDartSDK(); + + final buildDaemon = io.setupFakeBuildDaemon(verifyArgs: buildRunnerDartArgs); + + final serveResult = runner.run(['serve', '--verbose']); + + await expectLater( + io.stdout.queue, + emitsInOrderWithTimeout([ + 'Starting myapp in client rendering mode.', + '[BUILDER] Starting web compilers...', + '[BUILDER] Connecting to the build daemon...', + '[BUILDER] Starting initial build...', + ]), + ); + + await expectLater( + buildDaemon.messages, + emitsInOrderWithTimeout([ + isA(), + isA(), + ]), + ); + + // Gone before the first build result, without a notification. + await buildDaemon.close(); + + await expectLater( + io.stderr.queue, + emitsInOrderWithTimeout([ + '[BUILDER] [CRITICAL] The build daemon exited unexpectedly. Please restart Jaspr.', + ]), + ); + expect(await serveResult.timeout(const Duration(seconds: 10)), equals(1)); + }); + }); + + test('does not report the build daemon as exited unexpectedly when stopping after a failed build', () async { + await io.runZoned(() async { + io.setupFakeProject('myapp', mode: 'client'); + io.stubDartSDK(); + + final buildDaemon = io.setupFakeBuildDaemon(verifyArgs: buildRunnerDartArgs); + + // Stopping the daemon closes its connection, as a real daemon process + // that is killed would. The pid is never in use. + when(() => io.process.run('lsof', ['-tiTCP:1234', '-sTCP:LISTEN'])).thenAnswer((_) async { + await buildDaemon.close(); + return ProcessResult(0, 0, '2147483647\n', ''); + }); + + final serveResult = runner.run(['serve', '--verbose']); + + await expectLater( + io.stdout.queue, + emitsInOrderWithTimeout([ + 'Starting myapp in client rendering mode.', + '[BUILDER] Starting web compilers...', + '[BUILDER] Connecting to the build daemon...', + '[BUILDER] Starting initial build...', + ]), + ); + + await expectLater( + buildDaemon.messages, + emitsInOrderWithTimeout([ + isA(), + isA(), + ]), + ); + buildDaemon.sendMessage( + BuildResults((b) { + b.results.add( + DefaultBuildResult((b) { + b.target = 'web'; + b.status = BuildStatus.failed; + }), + ); + }), + ); + + // Stops through the guards, like Ctrl+C. + expect(await serveResult, equals(1)); + + final errors = []; + while (await io.stderr.queue.hasNext.timeout(const Duration(milliseconds: 300), onTimeout: () => false)) { + errors.add(await io.stderr.queue.next); + } + expect(errors, contains('[BUILDER] [ERROR] Could not start dev server due to build errors.')); + expect(errors, isNot(contains(contains('exited unexpectedly')))); + }); + }); + test('handles keyboard shortcuts (r, R, d, l)', () async { await io.runZoned(() async { io.stdin.hasTerminalOverride = true; diff --git a/packages/jaspr_cli/test/src/dev/client_runner_test.dart b/packages/jaspr_cli/test/src/dev/client_runner_test.dart new file mode 100644 index 000000000..3c23a3893 --- /dev/null +++ b/packages/jaspr_cli/test/src/dev/client_runner_test.dart @@ -0,0 +1,57 @@ +import 'dart:convert'; +import 'dart:io'; + +import 'package:build_daemon/constants.dart'; +import 'package:jaspr_cli/src/dev/client_runner.dart'; +import 'package:jaspr_cli/src/dev/util.dart'; +import 'package:jaspr_cli/src/logging.dart'; +import 'package:path/path.dart' as p; +import 'package:test/test.dart'; + +void main() { + group('ClientRunner', () { + // Regression test for https://github.com/schultek/jaspr/issues/997. + // + // Uses real processes on purpose: the fakes connect through + // `connectUnchecked`, so `startBuildDaemon` never returns `null` there. + test('build does not stop a build daemon that another process started', () async { + final tempDir = Directory.systemTemp.createTempSync('client_runner_test_'); + + // Stands in for the build daemon of a running `jaspr serve`: a separate + // process listening on the port from the daemon's port file. + final script = File(p.join(tempDir.path, 'other_daemon.dart')) + ..writeAsStringSync(''' +import 'dart:io'; + +void main() async { + final server = await ServerSocket.bind(InternetAddress.loopbackIPv4, 0); + print(server.port); +} +'''); + final otherDaemon = await Process.start(Platform.resolvedExecutable, [script.path]); + addTearDown(() async { + otherDaemon.kill(); + await otherDaemon.exitCode; + tempDir.deleteSync(recursive: true); + }); + final port = int.parse( + await otherDaemon.stdout.transform(utf8.decoder).transform(const LineSplitter()).first, + ); + + Directory(daemonWorkspace(tempDir.path)).createSync(recursive: true); + File(portFilePath(tempDir.path)).writeAsStringSync('$port\ntoken'); + expect(await isDaemonRunning(tempDir.path), isTrue); + + // Connecting fails in the empty directory, which takes the same + // `client == null` path as an options skew with the other daemon. + final exitCode = await IOOverrides.runZoned( + () => ClientRunner.build(buildOptions: [], outputDir: 'build', logger: Logger(false)), + getCurrentDirectory: () => tempDir, + ); + + expect(exitCode, equals(1)); + expect(await isDaemonRunning(tempDir.path), isTrue, reason: 'the other daemon must keep running'); + expect(File(portFilePath(tempDir.path)).existsSync(), isTrue, reason: 'its port file must be kept'); + }); + }); +}