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 non thread safe gateway initialization #3216

Merged
Show file tree
Hide file tree
Changes from all 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
9 changes: 7 additions & 2 deletions core/app/models/spree/billing_integration.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,13 @@ class BillingIntegration < PaymentMethod

def gateway
integration_options = options
ActiveMerchant::Billing::Base.integration_mode = integration_options[:server].to_sym
integration_options[:test] = true if integration_options[:test_mode]

# All environments except production considered to be test
test_server = integration_options[:server] != 'production'
test_mode = integration_options[:test_mode]

integration_options[:test] = (test_server || test_mode)

@gateway ||= gateway_class.new(integration_options)
end

Expand Down
10 changes: 7 additions & 3 deletions core/app/models/spree/payment_method.rb
Original file line number Diff line number Diff line change
Expand Up @@ -130,9 +130,13 @@ def find_with_destroyed(*args)
def gateway
gateway_options = options
gateway_options.delete :login if gateway_options.key?(:login) && gateway_options[:login].nil?
if gateway_options[:server]
ActiveMerchant::Billing::Base.mode = gateway_options[:server].to_sym
end

# All environments except production considered to be test
test_server = gateway_options[:server] != 'production'
test_mode = gateway_options[:test_mode]

gateway_options[:test] = (test_server || test_mode)

@gateway ||= gateway_class.new(gateway_options)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Current implementation is not thread safe, but ||= operator isn't thread safe neither: https://tenderlovemaking.com/2011/10/20/connection-management-in-activerecord.html

We should use a mutex in order to make it thread safe: https://lucaguidi.com/2014/03/27/thread-safety-with-ruby/

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wow, the question here is not in ||= operator used to set instance variable :)) but that you can't change the class variable (global) and expect that the gateway instance will use exactly this value. In next call this class variable will be overwritten and all gateway instances will use it, not one from their preference.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh ok, I got it now :) my apologies!

end
alias_method :provider, :gateway
Expand Down