From 30835d7dac4c2df11c5a8d49dc5a9c2e3cfc5dbe Mon Sep 17 00:00:00 2001 From: Andrew Kolos Date: Tue, 10 Sep 2024 15:24:56 -0700 Subject: [PATCH] handle EAGAIN (macOS) in ErrorHandlingProcessManager (#154306) ~~Fixes~~ Discovered in https://github.com/flutter/flutter/issues/153776. To my knowledge, `Resource temporarily unavailable` when trying to run a process means that some required resource is at capacity. There may be too many processes active, not enough available ports, or some sort of blocking cache is full, etc. Feel free to independently research and see if you come to the same conclusion. Then, it seems safe to catch and handle this within the tool's `ErrorHandlingProcessManager` abstraction, as Flutter cannot realistically do anything to prevent this issue. --- .../lib/src/base/error_handling_io.dart | 12 ++++++++++++ .../base/error_handling_io_test.dart | 19 +++++++++++++++++++ 2 files changed, 31 insertions(+) diff --git a/packages/flutter_tools/lib/src/base/error_handling_io.dart b/packages/flutter_tools/lib/src/base/error_handling_io.dart index 6166ca0e62..41bc27e885 100644 --- a/packages/flutter_tools/lib/src/base/error_handling_io.dart +++ b/packages/flutter_tools/lib/src/base/error_handling_io.dart @@ -777,6 +777,7 @@ void _handlePosixException(Exception e, String? message, int errorCode, String? void _handleMacOSException(Exception e, String? message, int errorCode, String? posixPermissionSuggestion) { // https://github.com/apple/darwin-xnu/blob/main/bsd/dev/dtrace/scripts/errno.d const int ebadarch = 86; + const int eagain = 35; if (errorCode == ebadarch) { final StringBuffer errorBuffer = StringBuffer(); if (message != null) { @@ -787,6 +788,17 @@ void _handleMacOSException(Exception e, String? message, int errorCode, String? errorBuffer.writeln(' sudo softwareupdate --install-rosetta --agree-to-license'); _throwFileSystemException(errorBuffer.toString()); } + if (errorCode == eagain) { + final StringBuffer errorBuffer = StringBuffer(); + if (message != null) { + errorBuffer.writeln('$message.'); + } + errorBuffer.writeln( + 'Your system may be running into its process limits. ' + 'Consider quitting unused apps and trying again.', + ); + throwToolExit(errorBuffer.toString()); + } _handlePosixException(e, message, errorCode, posixPermissionSuggestion); } diff --git a/packages/flutter_tools/test/general.shard/base/error_handling_io_test.dart b/packages/flutter_tools/test/general.shard/base/error_handling_io_test.dart index bc34665541..a43367c341 100644 --- a/packages/flutter_tools/test/general.shard/base/error_handling_io_test.dart +++ b/packages/flutter_tools/test/general.shard/base/error_handling_io_test.dart @@ -1108,6 +1108,7 @@ Please ensure that the SDK and/or project is installed in a location that has re const int enospc = 28; const int eacces = 13; const int ebadarch = 86; + const int eagain = 35; testWithoutContext('when writing to a full device', () { final FakeProcessManager fakeProcessManager = FakeProcessManager.list([ @@ -1190,6 +1191,24 @@ Please ensure that the SDK and/or project is installed in a location that has re expect(() => processManager.runSync(['foo', '--bar']), throwsToolExit(message: expectedMessage)); }); + + testWithoutContext('when up against resource limits (EAGAIN)', () async { + final FakeProcessManager fakeProcessManager = FakeProcessManager.list([ + const FakeCommand(command: ['foo', '--bar'], exception: ProcessException('', [], '', eagain)), + ]); + + final ProcessManager processManager = ErrorHandlingProcessManager( + delegate: fakeProcessManager, + platform: macOSPlatform, + ); + + const String expectedMessage = 'Flutter failed to run "foo --bar".\n' + 'Your system may be running into its process limits. ' + 'Consider quitting unused apps and trying again.'; + + expect(() async => processManager.start(['foo', '--bar']), + throwsToolExit(message: expectedMessage)); + }); }); testWithoutContext('ErrorHandlingProcessManager delegates killPid correctly', () async {