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

Integrate request_store to help with Threading issues #340

Merged
merged 1 commit into from Mar 3, 2014

Conversation

steveklabnik
Copy link
Contributor

Hey there! I ran into issues using Thread.current while working with Draper, and apparently Authlogic has them too. So I wrote a gem to handle it, and I figured you may want to use it too. Here's the Draper discussion: drapergem/draper#390

Fixes #335.

The Issue

If you use Thread.current to store state, and you use one of those fancy threaded servers like Puma or Thin, then what's in Thread.current doesn't get cleared per-request like it would if you were using Webrick or whatever. This causes subtle bugs.

The Fix

Basically, I wrote a gem that gives you 'request local' storage. It quacks like a Hash, just like Thread.current, but a middleware ensures that you get a clean slate every request.

I ran your tests on 1.9.3 and they were passing, but the gem works with basically every modern Ruby ever (and some not-so-modern ones), so no issues there.

Thanks! ❤️ ❤️ ❤️

@tiegz
Copy link
Collaborator

tiegz commented Dec 17, 2012

👍 👍 👍 that was fast, thanks @steveklabnik!!

@binarylogic
Copy link
Owner

I like this, want to look at it more. Not a big fan of creating a new dependency, but not sure I have a choice here.

@steveklabnik
Copy link
Contributor Author

Sure thing. One good thing about the gem is that if you're already using
something that uses it, it only gets included once.... Imagine all the
extra middlewares if every gem added one.

@nathany
Copy link
Contributor

nathany commented Jan 23, 2014

This seems important. Is anyone already using Authlogic with Puma or another thread web-server? Have changes in Puma or Ruby 2 or 2.1 made this unnecessary?

@binarylogic
Copy link
Owner

I'm on board, if someone wants to solve the merge confllcits I'll pull in. Otherwise I'll have to do it this weekend some time.

@nathany
Copy link
Contributor

nathany commented Feb 28, 2014

I've noticed that Rails itself uses Thread.current, so I wonder if this is still necessary?

@tiegz
Copy link
Collaborator

tiegz commented Feb 28, 2014

@nathany yeah, Thread.current is the problem itself. Authlogic sets the controller variable, but doesn't unset it after the request. I ran into an issue on a non-threaded server where activate_authlogic was set after another filter checked the user, so the filter grabbed the previous request's user (luckily it was an innocuous filter). RequestStore will ensure each request gets its own thread-local vars.

@steveklabnik
Copy link
Contributor Author

I can solve the merge conflicts soon if this patch is wanted.

And yes, @tiegz, exactly.

@nathany
Copy link
Contributor

nathany commented Feb 28, 2014

Thanks @tiegz and @steveklabnik

@steveklabnik
Copy link
Contributor Author

I've updated the PR to fix the merge conflicts.

@binarylogic
Copy link
Owner

sweet thanks

binarylogic added a commit that referenced this pull request Mar 3, 2014
Integrate request_store to help with Threading issues
@binarylogic binarylogic merged commit 5bd696a into binarylogic:master Mar 3, 2014
@steveklabnik
Copy link
Contributor Author

😍 👍 🎊

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.

Add after filters in adapters to clear Authlogic::Session::Base.controller?
4 participants