You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
should the thread_local_var_accessor gem be included within the concurrent-ruby gem? #1056
I've written a little gem that makes concurrent-ruby's thread-local variables easy to use, in the same manner of attr_reader, attr_writer, attr_accessor: it is called: thread_local_var_accessors
I've been using it in my company software for some time now. It is an easy way to encourage developers to improve the thread-safetyness of their code, especially code that runs in a multi-threaded environment (eg: sidekiq tasks).
I'm happy to continue maintaining it separately, but I'm also happy to contribute it to the core concurrent-ruby gem, if that's appropriate. In the latter case, since we are already using concurrent-ruby elsewhere, for other reasons, it would mean one gem less to track for us. 😄
Thanks for writing up the proposal.
I don't know how often this is useful, for example if it's a ThreadLocalVar which can be stored e.g. in a constant/globally there is no need for this.
Given this seems mostly convenience on top of ThreadLocalVar to be a bit shorter (e.g. tvar/self.tvar = v vs @tvar.value/@tvar.value = v), I think it's best as its own gem.
And the fact the ThreadLocalVar is created more magically/behind-the-scenes creates the race mentioned above.
Although that could be solved e.g. by requiring to assign the ivar in initialize, and raising an exception on read/write if it's not the case. (or using a global or per-class lock but that would create lots of contention so not a good solution)
However, this is still not a proper test. Short-lived threads are pretty much guaranteed to run to completion without exhibiting races (at least on CRuby). For example, this modified test "proves" that instance variables are thread-safe:
Be aware however that this doesn't really prove or disprove thread-safety of your code, you would need to test it with modified inner workings, by making it possible to switch threads in problematic spots. I, personally, think that it should be safe, but I'm no expert.
Activity
eregon commentedon Aug 1, 2024
Thanks for writing up the proposal.
I don't know how often this is useful, for example if it's a
ThreadLocalVarwhich can be stored e.g. in a constant/globally there is no need for this.I took a quick look at the code in https://github.com/aks/thread_local_var_accessors.
One thing which does not seem thread-safe is https://github.com/aks/thread_local_var_accessors/blob/d5d4fd3418d32c1901f719da603f652b7f1cea17/lib/thread_local_var_accessors.rb#L280-L285 specifically for:
Some threads might print
nilinstead oftruebecause multiple threads might assign@tvarand so overwrite each other.eregon commentedon Aug 1, 2024
Given this seems mostly convenience on top of
ThreadLocalVarto be a bit shorter (e.g.tvar/self.tvar = vvs@tvar.value/@tvar.value = v), I think it's best as its own gem.And the fact the
ThreadLocalVaris created more magically/behind-the-scenes creates the race mentioned above.Although that could be solved e.g. by requiring to assign the ivar in
initialize, and raising an exception on read/write if it's not the case. (or using a global or per-class lock but that would create lots of contention so not a good solution)aks commentedon Sep 20, 2024
I corrected your test example above, and ran it with thousands of threads. At no point was there ever any error.
However, I'm fine keeping the library in a separate gem.
Thanks for your reply and review.
Here's the updated test program and a sample output:
And, here's the output:
% $ time ./tlvtest.rb 100000 Count: 100000 no diffs ./tlvtest.rb 100000 2.11s user 1.47s system 113% cpu 3.145 totaltrinistr commentedon Jul 15, 2025
Apologies for reviving a thread (and for a long post)
@aks Your test program uses
Thread#runwhich joins the thread implicitly, making code in essence sequential. It needs to be something like this:The code does seem to be thread-safe:
However, this is still not a proper test. Short-lived threads are pretty much guaranteed to run to completion without exhibiting races (at least on CRuby). For example, this modified test "proves" that instance variables are thread-safe:
Output:
If the
#foomethod is modified to explicitly allow passing execution to a different thread, suddenly we get problems:Output:
Be aware however that this doesn't really prove or disprove thread-safety of your code, you would need to test it with modified inner workings, by making it possible to switch threads in problematic spots. I, personally, think that it should be safe, but I'm no expert.