Integrate request_store to help with Threading issues #340

Merged
merged 1 commit into from Mar 3, 2014

Conversation

Projects
None yet
4 participants
Contributor

steveklabnik commented Dec 17, 2012

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! ❤️ ❤️ ❤️

Collaborator

tiegz commented Dec 17, 2012

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

Owner

binarylogic commented Dec 29, 2012

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.

Contributor

steveklabnik commented Dec 29, 2012

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.

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?

Owner

binarylogic commented Feb 28, 2014

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.

Contributor

nathany commented Feb 28, 2014

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

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.

Contributor

steveklabnik commented Feb 28, 2014

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

And yes, @tiegz, exactly.

Contributor

nathany commented Feb 28, 2014

Thanks @tiegz and @steveklabnik

Contributor

steveklabnik commented Mar 2, 2014

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

Owner

binarylogic commented Mar 3, 2014

sweet thanks

binarylogic merged commit 5bd696a into binarylogic:master Mar 3, 2014

Contributor

steveklabnik commented Mar 3, 2014

😍 👍 🎊

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment