-
Notifications
You must be signed in to change notification settings - Fork 2k
C#: Retry logic for dotnet --info when it fails with exit code 143.
#20936
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 2 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -3,6 +3,7 @@ | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| using System.Collections.ObjectModel; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| using System.IO; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| using System.Linq; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| using System.Threading; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| using Newtonsoft.Json.Linq; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| using Semmle.Util; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
@@ -36,12 +37,29 @@ private DotNet(ILogger logger, string? dotNetPath, TemporaryDirectory tempWorkin | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| public static IDotNet Make(ILogger logger, string? dotNetPath, TemporaryDirectory tempWorkingDirectory, DependabotProxy? dependabotProxy) => new DotNet(logger, dotNetPath, tempWorkingDirectory, dependabotProxy); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| private static void HandleRetryExitCode143(string dotnet, int attempt, ILogger logger) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| logger.LogWarning($"Running '{dotnet} --info' failed with exit code 143. Retrying..."); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| var sleep = Math.Pow(2, attempt) * 1000; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Thread.Sleep((int)sleep); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| private void Info() | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| var res = dotnetCliInvoker.RunCommand("--info", silent: false); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if (!res) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| // Allow up to three retry attempts to run `dotnet --info`, to mitigate transient issues | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| for (int attempt = 0; attempt < 4; attempt++) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| throw new Exception($"{dotnetCliInvoker.Exec} --info failed."); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| var exitCode = dotnetCliInvoker.RunCommandExitCode("--info", silent: false); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| switch (exitCode) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| case 0: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| return; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| case 143 when attempt < 3: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| HandleRetryExitCode143(dotnetCliInvoker.Exec, attempt, logger); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| break; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Shouldn't this be a
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ah, yes, it is for the |
||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| default: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| throw new Exception($"{dotnetCliInvoker.Exec} --info failed with exit code {exitCode}."); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
@@ -193,6 +211,34 @@ private static BuildScript DownloadDotNet(IBuildActions actions, ILogger logger, | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| return BuildScript.Failure; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| /// <summary> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| /// Returns a script for running `dotnet --info`, with retries on exit code 143. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| /// </summary> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| public static BuildScript InfoScript(IBuildActions actions, string dotnet, IDictionary<string, string>? environment, ILogger logger) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| var info = new CommandBuilder(actions, null, environment). | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| RunCommand(dotnet). | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Argument("--info"); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| var script = info.Script; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| for (int attempt = 0; attempt < 4; attempt++) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| script = BuildScript.Bind(script, ret => | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| switch (ret) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| case 0: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| return BuildScript.Success; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| case 143 when attempt < 3: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| HandleRetryExitCode143(dotnet, attempt, logger); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| return info.Script; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| default: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| return BuildScript.Failure; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| }); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| script = BuildScript.Bind(script, ret => | |
| { | |
| switch (ret) | |
| { | |
| case 0: | |
| return BuildScript.Success; | |
| case 143 when attempt < 3: | |
| HandleRetryExitCode143(dotnet, attempt, logger); | |
| return info.Script; | |
| default: | |
| return BuildScript.Failure; | |
| } | |
| }); | |
| var attemptCopy = attempt; // Capture in local variable | |
| script = BuildScript.Bind(script, ret => | |
| { | |
| switch (ret) | |
| { | |
| case 0: | |
| return BuildScript.Success; | |
| case 143 when attemptCopy < 3: | |
| HandleRetryExitCode143(dotnet, attemptCopy, logger); | |
| return info.Script; | |
| default: | |
| return BuildScript.Failure; | |
| } | |
| }); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Damn, you got me there CoPilot!!
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,9 +1,7 @@ | ||
| import pytest | ||
|
|
||
| @pytest.mark.skip(reason=".NET 10 info command crashes") | ||
| def test1(codeql, csharp): | ||
| codeql.database.create() | ||
|
|
||
| @pytest.mark.skip(reason=".NET 10 info command crashes") | ||
| def test2(codeql, csharp): | ||
| codeql.database.create(build_mode="none") |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The comment says "Allow up to three retry attempts" but the loop runs 4 times (attempt = 0, 1, 2, 3). This means there are up to 3 retries after the initial attempt, for a total of 4 attempts. Consider updating the comment to clarify this is "up to 4 attempts (with 3 retries)" or adjust the loop to match the comment.