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

fix potential race condition in API client #10302

Merged
merged 6 commits into from
Apr 7, 2021

Conversation

cgbaker
Copy link
Contributor

@cgbaker cgbaker commented Apr 5, 2021

Resolves #10301

Commit 4e25e8e shows a documenting test, which fails under -race:
https://app.circleci.com/pipelines/github/hashicorp/nomad/15527/workflows/d2032205-895a-4d7b-8ec6-41301a1614cb/jobs/147802

New test passes after 6064867

Copy link
Contributor

@notnoop notnoop left a comment

Choose a reason for hiding this comment

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

LGTM. I suspect the significant violation here is that we are mutating the input header object, resulting into a request contaminating subsequent requests even without concurrency. The concurrency race is a consequence of that.

api/api_test.go Outdated Show resolved Hide resolved
api/api_test.go Show resolved Hide resolved
@cgbaker
Copy link
Contributor Author

cgbaker commented Apr 6, 2021

I suspect the significant violation here is that we are mutating the input header object, resulting into a request contaminating subsequent requests even without concurrency. The concurrency race is a consequence of that.

Correct. It's not necessarily even us... the downstream code in net/http will do this as well, although this test was a short-cut to that which didn't require actually making an HTTP call.

It is possible that an auth token or another field from a request-specific QueryOptions could make it back into the config.Headers, but I wasn't able to trigger that.

enable -race detector for testing api
Copy link
Contributor

@notnoop notnoop left a comment

Choose a reason for hiding this comment

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

Couple of tweaks - but feel free to merge as you see fit.

@@ -293,11 +293,15 @@ jobs:
goarch:
type: string
default: "amd64"
enable_race_testing:
type: string
Copy link
Contributor

Choose a reason for hiding this comment

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

Would this be better as a boolean?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

for the circleci bit, probably. however, downstream usage would require that it be converted to an empty string when false, because of the way that conditionals work in GNU make:
https://github.com/hashicorp/nomad/pull/10302/files#diff-78a8a19706dbd2a4425dd72bdab0502ed7a2cef16365ab7030a5a0588927bf47R304

Copy link
Contributor Author

Choose a reason for hiding this comment

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

okay, i've changed my mind. will do that, at least for the purpose of documentation.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@notnoop , when you have a chance, i'd appreciate your feedback on this approach.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

CHANGELOG.md Outdated Show resolved Hide resolved
@vercel vercel bot temporarily deployed to Preview – nomad April 6, 2021 21:00 Inactive
environment:
GOTEST_PKGS_EXCLUDE: "<< parameters.exclude_packages >>"
GOTEST_PKGS: "<< parameters.test_packages >>"
GOTEST_MOD: "<< parameters.test_module >>"
GOTESTARCH: "<< parameters.goarch >>"
ENABLE_RACE: "<<# parameters.enable_race_testing >>TRUE<</ parameters.enable_race_testing >>"
Copy link
Contributor

Choose a reason for hiding this comment

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

TIL about circleci conditionals!

@@ -17,6 +17,7 @@ IMPROVEMENTS:

BUG FIXES:
* agent: Only allow querying Prometheus formatted metrics if Prometheus is enabled within the config [[GH-10140](https://github.com/hashicorp/nomad/pull/10140)]
* api: Fixed a panic that may occur on concurrent access to an SDK client [[GH-10302](https://github.com/hashicorp/nomad/issues/10302)]
Copy link
Contributor

Choose a reason for hiding this comment

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

💯 This is even better than I was proposing about - thanks!

@github-actions
Copy link

I'm going to lock this pull request because it has been closed for 120 days ⏳. This helps our maintainers find and focus on the active contributions.
If you have found a problem that seems related to this change, please open a new issue and complete the issue template so we can capture all the details necessary to investigate further.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Nov 26, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Race condition in SDK client
2 participants