Skip to content

Commit

Permalink
Lock before changing field of backends (#843)
Browse files Browse the repository at this point in the history
* Lock before changing field of backends

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.
  • Loading branch information
chrsmith authored and brandur-stripe committed May 6, 2019
1 parent 4b31909 commit e880983
Showing 1 changed file with 3 additions and 0 deletions.
3 changes: 3 additions & 0 deletions stripe.go
Original file line number Diff line number Diff line change
Expand Up @@ -814,6 +814,9 @@ func SetAppInfo(info *AppInfo) {

// SetBackend sets the backend used in the binding.
func SetBackend(backend SupportedBackend, b Backend) {
backends.mu.Lock()
defer backends.mu.Unlock()

switch backend {
case APIBackend:
backends.API = b
Expand Down

0 comments on commit e880983

Please sign in to comment.