Skip to content
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

Issue 1739: Fixing null reference exception during configuring runner with invalid repo URL or token #1741

Merged
merged 3 commits into from
Mar 28, 2022
Merged

Issue 1739: Fixing null reference exception during configuring runner with invalid repo URL or token #1741

merged 3 commits into from
Mar 28, 2022

Conversation

ruvceskistefan
Copy link
Contributor

The problem was that the runner crasher with null reference exception during configuring with invalid repository URL or register token.

Closes #1739

After this change, the runner will not crash with a null reference exception but will exit with an appropriate error message.

@ruvceskistefan ruvceskistefan requested a review from a team as a code owner March 9, 2022 15:34
if(response.StatusCode == System.Net.HttpStatusCode.NotFound)
{
_term.WriteError("Invalid repository URL or register token");
return StringUtil.ConvertFromJson<GitHubRunnerRegisterToken>(errorResponse);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ConvertFromJson will return useless data if the errorResponse is not a contract of GitHubRunnerRegisterToken

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I changed this in the new commit.

else
{
_term.WriteError($"Http response code: {response.StatusCode} from 'POST {githubApiUrl}'");
var errorResponse = await response.Content.ReadAsStringAsync();
_term.WriteError(errorResponse);
response.EnsureSuccessStatusCode();

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we just try storing the response code and using that to determine if we should throw or not.

                    var responseStatus = HttpStatusCode.OK;
                    try
                    {
                        var response = await httpClient.PostAsync(githubApiUrl, new StringContent(string.Empty));
                        responseStatus = response.StatusCode;
                        if (response.IsSuccessStatusCode)
                        {
                            Trace.Info($"Http response code: {response.StatusCode} from 'POST {githubApiUrl}'");
                            var jsonResponse = await response.Content.ReadAsStringAsync();
                            return StringUtil.ConvertFromJson<GitHubRunnerRegisterToken>(jsonResponse);
                        }
                        else
                        {
                            _term.WriteError($"Http response code: {response.StatusCode} from 'POST {githubApiUrl}'");
                            var errorResponse = await response.Content.ReadAsStringAsync();
                            _term.WriteError(errorResponse);
                            response.EnsureSuccessStatusCode();
                        }
                    }
                    catch(Exception ex) when (retryCount < 2 && responseStatus != HttpStatusCode.NotFound)
                    {
                        retryCount++;
                        Trace.Error($"Failed to get JIT runner token -- Atempt: {retryCount}");
                        Trace.Error(ex);
                        Trace.Info("Retrying in 5 seconds");
                    }

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We may want to add others to this not retry list, and looking at the response code is the easiest way to do that

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure, makes sense.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I just won't trace delay time there, because it's randomly determined outside of catch.

// Something else bad happened, let's go to our retry logic
response.EnsureSuccessStatusCode();

if(response.StatusCode == System.Net.HttpStatusCode.NotFound)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same as above!

Copy link
Collaborator

@thboop thboop left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lgtm

@thboop thboop merged commit 052ac52 into actions:main Mar 28, 2022
AdamOlech pushed a commit to antmicro/runner that referenced this pull request Jul 26, 2024
… with invalid repo URL or token (actions#1741)

* Fixing null reference exception when configuring runner with invalid repo URL or token

* Throw exception instead of ConvertFromJson

* Storing the response code
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Runner throws a null ref exception during configuration for an invalid token or link to the repository
3 participants