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 "concurrent map iteration and map write" while read estransport.M… #397

Merged
merged 1 commit into from Jan 27, 2022

Conversation

mainliufeng
Copy link

"fatal error: concurrent map iteration and map write" while other goroutine reads estransport.Metrics.Responses

func TestTransportPerformAndReadMetricsResponses(t *testing.T) {
	t.Run("Executes", func(t *testing.T) {
		u, _ := url.Parse("https://foo.com/bar")
		tp, _ := New(Config{
			EnableMetrics: true,
			URLs:          []*url.URL{u},
			Transport: &mockTransp{
				RoundTripFunc: func(req *http.Request) (*http.Response, error) { return &http.Response{Status: "MOCK"}, nil },
			}})

		go func() {
			for {
				metrics, _ := tp.Metrics()
				t.Logf("len(responses): %v", len(metrics.Responses))
				for code, num := range metrics.Responses {
					t.Logf("%v(%v)", code, num)
				}
			}
		}()

		for i := 0; i < 100000; i++ {
			req, _ := http.NewRequest("GET", "/abc", nil)
			res, err := tp.Perform(req)
			if err != nil {
				t.Fatalf("Unexpected error: %s", err)
			}

			if res.Status != "MOCK" {
				t.Errorf("Unexpected response: %+v", res)
			}
		}
	})
}

@elasticmachine
Copy link
Collaborator

Since this is a community submitted pull request, a Jenkins build has not been kicked off automatically. Can an Elastic organization member please verify the contents of this patch and then kick off a build manually?

@Anaethelion
Copy link
Contributor

Hi @mainliufeng

Thanks for raising this and the patch that goes with it.
Copying the map to the new structure seems fine to me, do you think you can integrate the test you highlighted in the description to the code so we can prevent regression in the future ?

@mainliufeng
Copy link
Author

@Anaethelion I add a unit test to estransport/estransport_internal_test.go

@Anaethelion
Copy link
Contributor

Jenkins test this please

@Anaethelion
Copy link
Contributor

Failing CI is already fixed in 7.17 branch.
LGTM

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.

None yet

3 participants