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

Lock before changing field of backends #843

Merged
merged 2 commits into from
May 6, 2019
Merged

Lock before changing field of backends #843

merged 2 commits into from
May 6, 2019

Conversation

chrsmith
Copy link
Contributor

@chrsmith chrsmith commented May 3, 2019

Just a minor thing I noticed while trying to update a Stripe client to specify MaxNetworkRetries, since the default is 0. Apologies if this is somehow intentional.


The SetBackend method updates one of the members of the package-level variable backends. Since it's a pointer, it's probably safe on most architectures. But the Backends struct already has a sync.RWMutex field, and earlier in the file the GetBackend function goes through the trouble of explicitly acquiring a read and possibly a write lock. See:

func GetBackend(backendType SupportedBackend) Backend {

So to be safe, and consistent with the rest of the code, it makes sense to acquire a write lock before updating the backends value.

The `SetBackend` method updates one of the members of the package-level variable `backends`. Since it's a pointer, it's probably safe on most architectures. But the `Backends` struct already has a `sync.RWMutex` field, and earlier in the file the `GetBackend` function goes through the trouble of explicitly acquiring a read and possibly a write lock. See:
https://github.com/stripe/stripe-go/blob/d94ce0ed857fb1571b9b30bd24430bdac3d50c0f/stripe.go#L666

So to be safe, and consistent with the rest of the code, it makes sense to acquire a write lock before updating the `backends` value.
@brandur-stripe
Copy link
Contributor

@chrsmith Yep, sounds good to me!

The build is failing because Gofmt needs to be run on your changes. It's not obvious to me from your diff exactly what the problem is though (probably leading or trailing whitespace?).

@chrsmith
Copy link
Contributor Author

chrsmith commented May 4, 2019

@brandur-stripe Sorry about that. I made the change in GitHub's code editor, so it's probably some whitespace issue. Fixed.

@brandur-stripe
Copy link
Contributor

Awesome, thanks!

@brandur-stripe brandur-stripe merged commit e880983 into stripe:master May 6, 2019
@brandur-stripe
Copy link
Contributor

Released as 60.12.2.

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.

2 participants