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
Use of Thread.current and ViewContext #390
Comments
|
I've always been slightly weirded out by this code, so yes, I am totally open to changing it. |
|
Related: rails/rails#8517 |
|
|
|
Yes, either use begin/ensure to clear it or reset it at the start of request. |
|
@mperham thanks for chiming in. By begin/ensure you mean in some sort of middleware? Would that be appropriate to push up into Rails proper, especially as we're making Rails 4 be threadsafe by default? |
|
Yeah, middleware, around_filter, etc. or just reset it at start of each request if you want the simplest thing possible. |
|
The way to got about it without breaking stuff is:
|
|
@elado I've pushed a new commit for Draper that relies on my new gem, RequestStore, and I believe that it will fix this issue. Please try out Draper HEAD as soon as you're able, and let me know if this fixes it. Thank you! |
|
@steveklabnik Funny, I wrote the same thing and just didn't push it :) Thanks, I'll check it out soon. |
|
Ha! Sorry :( On Monday, December 17, 2012, Elad Ossadon wrote:
|
|
You shouldn't be! Thanks again. |
|
Worth mentioning: heartcombo/devise#2182 |
|
I'll investigate and coordinate with @josevalim, thank you. |
|
The current RequestStore should not cause any issues with Devise, since it is cleaned at the beginning of the request and not inside ensure. |
|
Since you don't clear the value after app.call it's not going to be a problem. The common approach of middlewares and Thread.current i've seen so far was something like def call(env)
old, Thread.current[:key] = Thread.current[:key], {}
@app.call(env)
ensure
Thread.current[:key] = old
endNot sure how much the reassigning of the old value is necessary, but without it it works because the value is still available after Warden's @app.call. |
|
Yeah, I figured the least invasive thing was to only touch it at the start of the request. |
I think this is pretty serious bug, where view context is kept globally and not per-request:
On a server like
thinorunicorn, theThread.currenthash is not being cleared between requests. It means that this code:in a controller, when executed twice (even from different sessions), will print
nil, 1, and then1, 1.It means that
Thread.current[:current_view_context] ||= build_view_contextwill keep previous value.And
h.current_user, on first hit, will return the current user, and on a different session will return the previous user because theThread.current[:current_view_context]contains the old context.I think that the solution is to clear the
Thread.current[:current_view_context]in a Middleware, before the request, so it gets fetched on every request.See more discussion and conclusions here:
http://stackoverflow.com/questions/13829257/rails-how-to-handle-thread-current-data-under-a-single-threaded-server-like-thi
Thoughts?
The text was updated successfully, but these errors were encountered: