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

ensure we *always* reset current_scope to previous_scope #22389

Closed
wants to merge 1 commit into from
Closed

ensure we *always* reset current_scope to previous_scope #22389

wants to merge 1 commit into from

Conversation

osheroff
Copy link

There previously existed a very small chance that the line self.current_scope = scope would execute without the ensure block executing. In Zendesk's case, a Timeout occurred after the Thread.local set happened but before we entered the begin block.

In this (admittedly extremely rare) case, the current_scope got stuck; it looked like this:

Model.find(5) # Timeout occurs, current_scope remains set
Model.find(10)
 -> <Model id:5>
Model.find(15)
 -> <Model id:5>

Which was disastrous and led to data corruption.

The fix is to set current_scope inside the begin block, so that we may never set current_scope without the ensure block also firing.

There previously existed a very small chance that the line
`self.current_scope = scope` would execute without the `ensure` block
executing.  In Zendesk's case, a Timeout occurred after the
`Thread.local` set happened but before we enterered the `begin` block.

In this extremely rare case, the current_scope got stuck; it
looked like this:

Model.find(5) # Timeout occurs, current_scope remains set
Model.find(10)
 -> <Model id:5>
Model.find(15)
 -> <Model id:5>

Which was disastrous and led to data corruption.

The fix is to set current_scope inside the `begin` block, so that we may
never set current_scope without the `ensure` block also happening.
@rails-bot
Copy link

Thanks for the pull request, and welcome! The Rails team is excited to review your changes, and you should hear from @schneems (or someone else) soon.

If any changes to this PR are deemed necessary, please add them as extra commits. This ensures that the reviewer can see what has changed since they last reviewed the code. Due to the way GitHub handles out-of-date commits, this should also make it reasonably obvious what issues have or haven't been addressed. Large or tricky changes may require several passes of review and changes.

Please see the contribution instructions for more information.

@rails-bot
Copy link

warning Warning warning

  • Pull requests are usually filed against the master branch for this repo, but this one is against 3-2-stable. Please double check that you specified the right target!

@rafaelfranca
Copy link
Member

Thank you for the pull request but Rails 3.2 is out of maintainance for almost 2 years now and don't receive any commit unless it is a critical security fix.

Does this problem still happen on Rails 4.1+?

@osheroff
Copy link
Author

@rafaelfranca yeah, figured as much. This was a reasonably large security issue for us (everyone suddenly had the same identity, more or less), but no, as far as I can tell rails 4.1 is clean. This can just stand as a subtle warning to put all the things inside the ensure block.

@osheroff osheroff closed this Nov 24, 2015
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.

None yet

4 participants