diff --git a/build/config/ios/ios_sdk.py b/build/config/ios/ios_sdk.py index 28eafe5376..607a425ac9 100644 --- a/build/config/ios/ios_sdk.py +++ b/build/config/ios/ios_sdk.py @@ -21,6 +21,29 @@ os.path.dirname(__file__), os.pardir, os.pardir, os.pardir, 'flutter', 'prebuilts', )) + +def run_command_with_retry(command, timeout=10, retries=3): + """ + Runs a command using subprocess.check_output with timeout and retry logic. + + Args: + command: A list representing the command and its arguments. + timeout: The maximum time (in seconds) to wait for each command execution. + retries: The number of times to retry the command if it times out. + + Returns: + The output of the command as a bytes object if successful, otherwise + raises a CalledProcessError. + """ + for attempt in range(1, retries + 1): + try: + result = subprocess.check_output(command, timeout=timeout) + return result.decode('utf-8').strip() + except subprocess.TimeoutExpired: + if attempt >= retries: + raise # Re-raise the TimeoutExpired error after all retries + + def main(argv): parser = argparse.ArgumentParser() parser.add_argument( @@ -76,7 +99,7 @@ def main(argv): sdk, 'Path' ] - sdk_output = subprocess.check_output(command).decode('utf-8').strip() + sdk_output = run_command_with_retry(command, timeout=300) if symlink_path: symlink_target = os.path.join(sdks_path, os.path.basename(sdk_output)) symlink(sdk_output, symlink_target) diff --git a/build/mac/find_sdk.py b/build/mac/find_sdk.py index e21fc5b120..73cf268a6a 100755 --- a/build/mac/find_sdk.py +++ b/build/mac/find_sdk.py @@ -31,6 +31,28 @@ def parse_version(version_str): return [int(x) for x in re.findall(r'(\d+)', version_str)] +def run_command_with_retry(command, timeout=10, retries=3): + """ + Runs a command using subprocess.check_output with timeout and retry logic. + + Args: + command: A list representing the command and its arguments. + timeout: The maximum time (in seconds) to wait for each command execution. + retries: The number of times to retry the command if it times out. + + Returns: + The output of the command as a bytes object if successful, otherwise + raises a CalledProcessError. + """ + for attempt in range(1, retries + 1): + try: + result = subprocess.check_output(command, timeout=timeout) + return result.decode('utf-8').strip() + except subprocess.TimeoutExpired: + if attempt >= retries: + raise # Re-raise the TimeoutExpired error after all retries + + def main(): parser = OptionParser() parser.add_option("--print_sdk_path", @@ -72,7 +94,7 @@ def main(): sdk_command = ['xcodebuild', '-showsdks', '-json'] - sdk_json_output = subprocess.check_output(sdk_command) + sdk_json_output = run_command_with_retry(sdk_command, timeout=300) sdk_json = json.loads(sdk_json_output) best_sdk = None