Skip to content

Commit

Permalink
Timeout and retry calls to xcodebuild (#876)
Browse files Browse the repository at this point in the history
The xcodebuild command occasionally times out. Subsequent runs behave as
expected.
  • Loading branch information
zanderso authored Jul 18, 2024
1 parent d28312a commit 3279062
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 2 deletions.
25 changes: 24 additions & 1 deletion build/config/ios/ios_sdk.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down Expand Up @@ -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)
Expand Down
24 changes: 23 additions & 1 deletion build/mac/find_sdk.py
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 3279062

Please sign in to comment.