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 actual thread local for Puma::Server.current
.
#3360
Conversation
f96b510
to
6568209
Compare
Puma::Server.current
.
It's probably worth noting that the current implementation conflicts with |
@nateberkopec can we merge this? |
6568209
to
c4061c3
Compare
Should we test this? Can you show example code how/when this breaks? Just to educate us all further |
6e6e5b5
to
5106182
Compare
Sorry, I should have added a test. Okay, included failing test + fix. |
cc @nateberkopec @dentarg failing test included - fix included - ready for review :) |
Isn't |
You are correct. I don't think it's particularly surprising, and I'm not sure there is a better approach that keeps things simple and efficient. No matter what, thread locals in Ruby have "global visibility". The fact that the name is prefixed While I don't think it should influence our decision here, |
Agreed. Seeing the Rails example and who committed the code... |
(@MSP-Greg as an aside, there was some discussion about introducing |
@@ -131,7 +133,7 @@ def inherit_binder(bind) | |||
class << self | |||
# @!attribute [r] current | |||
def current | |||
Thread.current[THREAD_LOCAL_KEY] | |||
Thread.current.puma_server |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm curious - what's the reason it can't be Thread.current.thread_variable_get
and Thread.current.thread_variable_set
instead? They operate exclusively at the thread level "and do not respect fibers" https://docs.ruby-lang.org/en/3.3/Thread.html#method-i-thread_variable_get
They also seem to support ruby going back pretty far, earlier into the 2 series.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Both approaches support all currently tested versions of Ruby.
As discussed above, Thread.current.puma_server
is more efficient. There was a discussion here about it: rails/rails#43596 (comment)
63d02c7
to
a17539c
Compare
Folks, what's the process for getting this reviewed / merged? |
I did approve this PR on 11-Apr... Question, there's a method def self.set_thread_name(name)
Thread.current.name = "puma #{name}"
# This method was private on Ruby 2.4 but became public on Ruby 2.5+:
Thread.current.singleton_class.send(:attr_accessor, :puma_server)
end There seems to be concern about globally adding an attrribute to Not sure if doing it this adds overhead (possibly creating a singleton class?). This passes the new test (locally on WSL2/Ubuntu 22.04). |
Thanks @MSP-Greg I appreciate your feedback. I'm fine with your proposed change. Modifying singleton classes - however it means that if someone calls Regarding approval / merge - I'm not able to merge it even with your approval.
So I trust that we need someone else to actually decide where the bar for merging is. That is the part that's not clear to me. I think ideally someone is either (1) okay with this PR and merges it or (2) requests changes - so it's clear what needs to happen to move things forward. |
True. But, it's set in the call to Regardless, maybe setting it as a global attribute is best, since it won't throw an error if called from a non-Puma thread... BTW, sorry about the mixup. Before I posted, you pinged @nateberkopec and @dentarg, so I wasn't sure if they'd look at it. Or, yes, I can merge it... |
I don't know the normal process but it would be nice to have confidence that there was consensus on (1) fixing the bug and (2) the proposed fix. In other words, I'd like to see this get merged, but I'd also like to follow whatever process is suitable for the project. |
As far as I know, there is no formal process :) Just people looking when they have time for it. My hunch is that it is good to avoid NoMethodError, but I haven't spent much time thinking about this PR. I also pretty much trust you both @ioquatix @MSP-Greg so if you are in agreement, it is very much fine if Greg merges this. |
Merged. I was in the process of reviewing this, looking for the original reason for the method. The commit (July-2016) and issue were: I tried searching GitHub for Thanks, and sorry for the delay... |
I'm 👍🏼 on dropping this interface in v7 especially if there are no obvious use cases. Maybe we can emit a warning in a v6.9... release. |
* Add test for `Puma::Server.current`. * Use actual thread local for `Puma::Server.current`.
The current implementation of
Puma::Server.current
will break inside fibers, e.g. lazy enumerators because it's fiber local, not thread local. If the intention is to have an actual thread local, this is the best fix.Your checklist for this pull request
[ci skip]
to the title of the PR.#issue
" to the PR description or my commit messages.