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
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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.

default: ""
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 >>"
steps:
- checkout
- install-golang
Expand Down Expand Up @@ -616,6 +620,7 @@ workflows:
name: "test-api"
test_module: "api"
filters: *backend_test_branches_filter
enable_race_testing: "1"
- test-container:
name: "test-devices"
test_packages: "./devices/..."
Expand Down
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 race condition around headers in API client [[GH-10302](https://github.com/hashicorp/nomad/issues/10302)]
cgbaker marked this conversation as resolved.
Show resolved Hide resolved
* api: Added missing devices block to AllocatedTaskResources [[GH-10064](https://github.com/hashicorp/nomad/pull/10064)]
* cli: Fixed a bug where non-int proxy port would panic CLI [[GH-10072](https://github.com/hashicorp/nomad/issues/10072)]
* cli: Fixed a bug where `nomad operator debug` incorrectly parsed https Consul API URLs. [[GH-10082](https://github.com/hashicorp/nomad/pull/10082)]
Expand Down
4 changes: 2 additions & 2 deletions api/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -685,8 +685,8 @@ func (c *Client) newRequest(method, path string) (*request, error) {
}
}

if c.config.Headers != nil {
r.header = c.config.Headers
for key, values := range c.config.Headers {
r.header[key] = values
}

return r, nil
Expand Down
28 changes: 27 additions & 1 deletion api/api_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,10 @@ import (
"testing"
"time"

"github.com/hashicorp/nomad/api/internal/testutil"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"

"github.com/hashicorp/nomad/api/internal/testutil"
)

type configCallback func(c *Config)
Expand Down Expand Up @@ -538,3 +539,28 @@ func TestCloneHttpClient(t *testing.T) {
})

}

func TestClient_HeaderRaceCondition(t *testing.T) {
cgbaker marked this conversation as resolved.
Show resolved Hide resolved
require := require.New(t)

conf := DefaultConfig()
conf.Headers = map[string][]string{
"test-header": {"a"},
}
client, err := NewClient(conf)
require.NoError(err)

c := make(chan int)

go func() {
req, _ := client.newRequest("GET", "/any/path/will/do")
r, _ := req.toHTTP()
c <- len(r.Header)
}()
req, _ := client.newRequest("GET", "/any/path/will/do")
r, _ := req.toHTTP()

require.Len(r.Header, 2, "local request should have two headers")
require.Equal(2, <-c, "goroutine request should have two headers")
require.Len(conf.Headers, 1, "config headers should not mutate")
}
4 changes: 2 additions & 2 deletions vendor/github.com/hashicorp/nomad/api/api.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.