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

[Bug]: Easypost.api_key is not threadsafe #183

Closed
andychongyz opened this issue Jun 20, 2022 · 5 comments
Closed

[Bug]: Easypost.api_key is not threadsafe #183

andychongyz opened this issue Jun 20, 2022 · 5 comments
Labels
breaking change Introduces a breaking change enhancement Introduces a new feature or improvement triage Issue is being researched

Comments

@andychongyz
Copy link
Contributor

andychongyz commented Jun 20, 2022

Software Version

4.0.3

Language Version

3.0.3

Operating System

mac os monterey

What happened?

The Easypost.api_key attribute is not threadsafe.

Running the code below in a rails console can easily show that the attribute is not threadsafe.

EasyPost.api_key = "EASYPOST_MAIN_API_KEY"

def with_account(account_api_key)
  old_api_key = EasyPost.api_key
  puts "old_api_key: #{old_api_key}"
  EasyPost.api_key = account_api_key
  yield
  puts "done"
ensure
  EasyPost.api_key = old_api_key
end

(1..5).each do |i|
  Thread.new do
    puts "Thread #{i} started"
    with_account("test_api_key_#{i}") do
      sleep(rand(2..5))
    end
  end
end

What was expected?

EasyPost.api_key should be threadsafe since I believe that a lot of Rails app is running on puma with a multi-thread setup.

Sample Code

No response

Relevant logs

No response

@andychongyz andychongyz added the triage Issue is being researched label Jun 20, 2022
@andychongyz
Copy link
Contributor Author

andychongyz commented Jun 20, 2022

Here is how ActiveResource deals with threadsafe class attributes.
https://github.com/rails/activeresource/blob/main/lib/active_resource/threadsafe_attributes.rb

@nwithan8
Copy link
Member

Thank you for opening this issue and providing the documentation link.

We are aware about the lack of thread-safety in our client libraries, and are actively working to rectify this.

@andychongyz
Copy link
Contributor Author

andychongyz commented Jun 21, 2022

Here's how I patch the code, for now, to make it threadsafe. Hope it helps.

EasyPost.module_eval do
  class << self
    include ThreadsafeAttributes

    threadsafe_attribute :api_key
  end
end

@nwithan8 nwithan8 added enhancement Introduces a new feature or improvement breaking change Introduces a breaking change labels Jan 17, 2023
@Justintime50
Copy link
Member

Wanted to provide an update, we're beginning the work for a new major release (v5) of this library that will contain a new client object which will enable this library to be thread safe. The work will happen over the coming weeks. We'll update everyone here when v5 is released and ultimately, when this library is thread safe which is the primary concern of the new major version.

@Justintime50
Copy link
Member

We will be releasing v5.0.0 which is a thread-safe rewrite of this library within the next couple hours. We'd encourage anyone who needs thread-safety to upgrade.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
breaking change Introduces a breaking change enhancement Introduces a new feature or improvement triage Issue is being researched
Projects
None yet
Development

No branches or pull requests

3 participants