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

without_versioning not thread safe #326

Closed
dwbutler opened this issue Feb 10, 2014 · 1 comment
Closed

without_versioning not thread safe #326

dwbutler opened this issue Feb 10, 2014 · 1 comment
Assignees
Milestone

Comments

@dwbutler
Copy link
Contributor

# Executes the given method or block without creating a new version.
def without_versioning(method = nil)
  paper_trail_was_enabled = self.paper_trail_enabled_for_model
  self.class.paper_trail_off
  method ? method.to_proc.call(self) : yield
ensure
  self.class.paper_trail_on if paper_trail_was_enabled
end

If multiple threads call this method at the same time, Bad Things will happen.

For example, versioning could be turned off for all threads, causing no threads to save any versions during the duration of this method call.

Or, one thread could have set self.paper_trail_enabled_for_model to false temporarily. Then, when without_versioning is called, paper_trail_was_enabled will be false. So versioning will be turned off permanently from that point forward.

The solution is probably to use a thread local variable for self.paper_trail_enabled_for_model. This will also cause self.class.paper_trail_off and self.class.paper_trail_on to be thread local.

@dwbutler
Copy link
Contributor Author

To put this into context, I'm running my Rails app in JRuby under Torquebox, so thread safety is a real issue for me.

I'm working on a pull request for this that will use PaperTrail's paper_trail_store to track if Paper Trail is enabled on a particular model.

I believe this will also fix #307 by doing away with setting and unsetting the paper_trail_enabled_for_model class attribute (which apparently isn't thread safe).

@batter batter added this to the 3.0.1 milestone Feb 20, 2014
@batter batter self-assigned this Feb 20, 2014
@batter batter closed this as completed in 0359704 Feb 20, 2014
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