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

Global State Race #221

Open
sethgrid opened this issue Dec 28, 2017 · 2 comments
Open

Global State Race #221

sethgrid opened this issue Dec 28, 2017 · 2 comments

Comments

@sethgrid
Copy link

The global state here is a problem and causes data races if multiple implementing servers are spun up at the same time, as can be done in testing:
https://github.com/rcrowley/go-metrics/blob/master/runtime.go#L11

In my code, I start a server like such:

type Server struct {
	config config.Configuration
	metricsPublisher *pfmetrics.Publisher
	metricsRegistry  metrics.Registry
...

In my New method, I set the values like such:

       // srv is a &Server{} and c is config
	srv.metricsRegistry = metrics.NewRegistry()
	srv.metricsPublisher = pfmetrics.NewPublisher(fmt.Sprintf("%s:%s", c.CarbonHost, c.CarbonPort), c.CarbonPrefix, srv.metricsRegistry)

In my tests, I want to run parallel servers. This results in multiple calls to the following in my server's serve method:

	go srv.metricsPublisher.Start()
	defer srv.metricsPublisher.Stop()

Even though the Start and Stop methods are on my srv instance, they affect the global scoped metrics in the library, leading to the following data races.

==================
==================
WARNING: DATA RACE
Read at 0x000001a52df8 by goroutine 61:
  github.com/sendgrid/mta/vendor/github.com/rcrowley/go-metrics.CaptureRuntimeMemStatsOnce()
      /Users/sethammons/workspace/go/src/github.com/sendgrid/mta/vendor/github.com/rcrowley/go-metrics/runtime.go:137 +0xa12
  github.com/sendgrid/mta/vendor/github.com/sendgrid/platformlib/pfmetrics.(*Publisher).Start()
      /Users/sethammons/workspace/go/src/github.com/sendgrid/mta/vendor/github.com/sendgrid/platformlib/pfmetrics/publisher.go:104 +0x469
Previous write at 0x000001a52df8 by goroutine 14:
  github.com/sendgrid/mta/vendor/github.com/rcrowley/go-metrics.CaptureRuntimeMemStatsOnce()
      /Users/sethammons/workspace/go/src/github.com/sendgrid/mta/vendor/github.com/rcrowley/go-metrics/runtime.go:138 +0xa4e
  github.com/sendgrid/mta/vendor/github.com/sendgrid/platformlib/pfmetrics.(*Publisher).Start()
      /Users/sethammons/workspace/go/src/github.com/sendgrid/mta/vendor/github.com/sendgrid/platformlib/pfmetrics/publisher.go:104 +0x469
@eranharel
Copy link
Contributor

I sent a PR for #252 that should fix this:
#253

@sethgrid
Copy link
Author

Looked over the PR, seems like it should do it from my perspective! Thanks

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

No branches or pull requests

2 participants