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

Thread safety and #refresh_token #40

Open
tristanm opened this issue Jun 22, 2015 · 1 comment
Open

Thread safety and #refresh_token #40

tristanm opened this issue Jun 22, 2015 · 1 comment

Comments

@tristanm
Copy link

I realise this is a generic question which could apply to many similar situations but I thought I'd bring it up in case anyone thinks it's useful to mention it in createsend-ruby's README.md.

In multi-threaded applications, it is possible to get into an unsafe situation when refreshing tokens, e.g.:

record = SomeModel.find(...)
cs = CreateSend::CreateSent.new(
    record.access_token,
    record.refresh_token
)

begin
  tries = 2
  cs.clients
rescue CreateSend::ExpiredOAuthToken => e
  access_token, expires_in, request_token = cs.refresh_token
  record.access_token = access_token
  record.request_token = request_token
  record.save
  retry if (trues -= 1).zero?
  raise e
end
  1. record.access_token expires
  2. Thread A encounters ExpiredOAuthToken
  3. Thread A calls #refresh_token
  4. Thread B encounters ExpiredOAuthToken
  5. Thread B calls #refresh_token but fails because record.refresh_token is now invalid
  6. Thread A persists new token
  7. Thread A continues

I'm assuming others have encountered this possibility. Anyone have any favoured suggestions for dealing with it?

I've created a question on SO too (modified the code to make it broader than createsend-ruby) if anyone would like some points!

@asadighi
Copy link

Hi,

Thanks for reaching out. My name is Aidin and I am the Technical Lead for Integrations and APIs in Campaign Monitor. I would be more than happy to help out.

Were you able to find a solution for this particular issue?

Thanks,
Aidin

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