Skip to content

Set Thread.current.puma_server in Thread init code, not every request#3774

Merged
schneems merged 2 commits intopuma:masterfrom
MSP-Greg:00-thread-pool-server
Oct 16, 2025
Merged

Set Thread.current.puma_server in Thread init code, not every request#3774
schneems merged 2 commits intopuma:masterfrom
MSP-Greg:00-thread-pool-server

Conversation

@MSP-Greg
Copy link
Copy Markdown
Member

Description

Currently, the following is set in every call to Server#process_request:

# Advertise this server into the thread
Thread.current.puma_server = self

PR moves code around to set it once in the Thread.new block.

Your checklist for this pull request

  • I have reviewed the guidelines for contributing to this repository.
  • I have added (or updated) appropriate tests if this PR fixes a bug or adds a feature.
  • My pull request is 100 lines added/removed or less so that it can be easily reviewed.
  • If this PR doesn't need tests (docs change), I added [ci skip] to the title of the PR.
  • If this closes any issues, I have added "Closes #issue" to the PR description or my commit messages.
  • I have updated the documentation accordingly.
  • All new and existing tests passed, including Rubocop.

@github-actions github-actions Bot added the waiting-for-review Waiting on review from anyone label Sep 24, 2025
Copy link
Copy Markdown
Collaborator

@joshuay03 joshuay03 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I like the overall idea, just sharing my two cents on the implementation.

Comment thread lib/puma/server.rb Outdated

@status = :run

@options[:puma_server] = self
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we not pass this into the thread pool initialiser? It doesn't feel right to mutate the options at this stage.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think one test file (test_thread_pool.rb) creates a ThreadPool. I suppose we could change the method signature to the below or something similar:

def initialize(name, options = {}, server = nil, &block)

I'll look at changing it.

Comment thread lib/puma/thread_pool.rb Outdated
Comment on lines +41 to +44
# below is for CI
@server = options.respond_to?(:user_options) ?
options.user_options.delete(:puma_server) :
options.delete(:puma_server)
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

My personal opinion is that having to do this negates the micro optimisation gained from the core of this change (I don't think any lib code should exist just to satisfy CI). Could you link to the specific CI code that needs this? Is it just a naming conflict?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

negates the micro optimization gained from the core of this change

I'm not really concerned about code that runs once for each ThreadPool instance. I wanted to add it to options before the call, and delete it, but UserFileDefaultOptions isn't a hash, it just implements some methods of Hash.

Also, something that is currently set on every request is possibly more than a micro-optimization, and removing it (and doing it in the init) mean's one can't be sidetracked by the question "Why is this set in every request?". I really hate it when that happens...

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As an aside: I would like (someone) to remove all options = {} in favor of explicit KWARGS where possible into the future.

@MSP-Greg
Copy link
Copy Markdown
Member Author

Just added a commit changing the ThreadPool init method signature.

Copy link
Copy Markdown
Collaborator

@joshuay03 joshuay03 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice!

Comment thread lib/puma/thread_pool.rb
require_relative 'io_buffer'

module Puma

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit - feel free to ignore if this is your personal preference.

Suggested change

@schneems schneems merged commit 1d3d6b7 into puma:master Oct 16, 2025
74 checks passed
@MSP-Greg MSP-Greg deleted the 00-thread-pool-server branch October 16, 2025 18:06
@dentarg dentarg removed the waiting-for-review Waiting on review from anyone label Oct 26, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants