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

Achieve true thread local context #61

Closed
rwdaigle opened this issue May 7, 2014 · 2 comments
Closed

Achieve true thread local context #61

rwdaigle opened this issue May 7, 2014 · 2 comments

Comments

@rwdaigle
Copy link

rwdaigle commented May 7, 2014

In an environment using a Fiber pool (such as an evented Rack app), setting a Scrolls context in a Rack middleware will not then be available to all scopes within that request. This is because in Ruby 2.x Thread.current[]= behavior does not actually set a thread-local variable, but a Fiber-local one.

To achieve true thread locality you need to use thread_variable_get (and set). I believe this is how context should be implemented?

module Scrolls
  module Log

    def context=(h)
      Thread.current.thread_variable_set(:scrolls_context, h || {})
    end

    def context
      Thread.current.thread_variable_get(:scrolls_context)
    end
  end
end

If so, let me know and I can submit a proper PR. One up front question would be your preference in Ruby version detection.

@asenchi
Copy link
Owner

asenchi commented May 11, 2014

@rwdaigle I'll need to read up more, this is definitely interesting and unintended behavior. I'm curious what the effects are on Fiber based environments then? I've not kept up on what is using Fibers, or threads, is there an option here that satisfies each setup? I'm not well versed in the nuances in Threads vs. Fibers in Ruby.

As for version matching, I've always used ENV['RUBY_VERSION'], but am interested in what the current methods are. This has always been the easiest for me, but there may be a more common "practice".

asenchi added a commit that referenced this issue Aug 30, 2017
Fixes #61

In Ruby 2.1 setting thread local values changed. This
updates our usage.
@asenchi
Copy link
Owner

asenchi commented Aug 31, 2017

I've fixed this behavior in my refactor: #71

@asenchi asenchi closed this as completed Aug 31, 2017
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