Skip to content

Comments

ThreadPool concurrency refactoring (#2220)#3

Open
MitchLewis930 wants to merge 1 commit intopr_053_beforefrom
pr_053_after
Open

ThreadPool concurrency refactoring (#2220)#3
MitchLewis930 wants to merge 1 commit intopr_053_beforefrom
pr_053_after

Conversation

@MitchLewis930
Copy link

PR_053

- Wait for threads to enter waiting loop on ThreadPool startup
- Simplify #spawn_thread inner threadpool loop
- Refactor TestThreadPool to make tests faster and more stable

Co-authored-by: Nate Berkopec <nate.berkopec@gmail.com>
@MitchLewis930 MitchLewis930 requested a review from Copilot January 31, 2026 01:30
Copy link

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

This PR refactors ThreadPool concurrency management to fix race conditions and improve synchronization, particularly for non-GVL Ruby implementations.

Changes:

  • Introduces MutexPool test helper class that wraps ThreadPool with mutex-based synchronization for deterministic test execution
  • Refactors thread lifecycle management in ThreadPool to use Thread.exit instead of break statements and moves cleanup logic
  • Adds with_mutex helper method to prevent potential deadlocks when mutex is already owned

Reviewed changes

Copilot reviewed 3 out of 3 changed files in this pull request and generated 2 comments.

File Description
test/test_thread_pool.rb Adds MutexPool helper class and refactors tests to use it, removing sleep-based synchronization and platform-specific skips
lib/puma/thread_pool.rb Refactors thread spawn/exit logic, adds with_mutex helper, and improves trim request handling during shutdown
History.md Documents the concurrency bug fixes and refactoring in the changelog

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment on lines +109 to +111
@spawned -= 1
@workers.delete th
Thread.exit
Copy link

Copilot AI Jan 31, 2026

Choose a reason for hiding this comment

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

Thread cleanup (decrementing @Spawned and removing from @Workers) happens inside the mutex before Thread.exit, but the original code performed cleanup after the thread loop ended. This change means if Thread.exit fails or is interrupted, the cleanup has already occurred, potentially leaving @Spawned and @Workers in an inconsistent state. Consider whether cleanup should remain after the thread exits to ensure accurate accounting.

Copilot uses AI. Check for mistakes.
Comment on lines +217 to +218
free = @waiting - @todo.size
if (force or free > 0) and @spawned - @trim_requested > @min
Copy link

Copilot AI Jan 31, 2026

Choose a reason for hiding this comment

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

The variable name 'free' is ambiguous. Consider renaming it to 'free_threads' or 'idle_threads' to clarify that it represents the number of threads available to handle new work.

Suggested change
free = @waiting - @todo.size
if (force or free > 0) and @spawned - @trim_requested > @min
free_threads = @waiting - @todo.size
if (force or free_threads > 0) and @spawned - @trim_requested > @min

Copilot uses AI. Check for mistakes.
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

Successfully merging this pull request may close these issues.

2 participants