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

@MitchLewis930 MitchLewis930 commented Jan 30, 2026

User description

PR_053


PR Type

Enhancement, Bug fix


Description

  • Wait for threads to enter waiting loop on ThreadPool startup

  • Simplify spawn_thread inner threadpool loop and remove continue flag

  • Fix concurrency bugs in trim and shutdown logic

  • Refactor tests with MutexPool wrapper for better stability

  • Add with_mutex helper to handle nested synchronization


Diagram Walkthrough

flowchart LR
  A["ThreadPool Initialization"] -->|"Wait for threads ready"| B["Startup Synchronization"]
  C["spawn_thread Loop"] -->|"Simplify control flow"| D["Remove continue flag"]
  E["Trim Logic"] -->|"Fix free thread calculation"| F["Better thread management"]
  G["Test Suite"] -->|"Add MutexPool wrapper"| H["Stable concurrent tests"]
  I["Mutex Handling"] -->|"Add with_mutex helper"| J["Handle nested locks"]
Loading

File Walkthrough

Relevant files
Enhancement
thread_pool.rb
Refactor ThreadPool concurrency and synchronization logic

lib/puma/thread_pool.rb

  • Wait for threads to enter waiting loop during initialization via
    @not_full.wait(@mutex)
  • Simplify spawn_thread by removing continue flag and handling thread
    exit directly
  • Move thread cleanup (decrement @spawned, delete from @workers) into
    trim logic
  • Add with_mutex helper method to handle nested synchronization safely
  • Fix trim logic to calculate free threads as @waiting - @todo.size
  • Update shutdown to set @trim_requested = @spawned for proper cleanup
+24/-27 
Tests
test_thread_pool.rb
Refactor tests with MutexPool for better stability             

test/test_thread_pool.rb

  • Create MutexPool wrapper class to synchronize work execution for
    deterministic tests
  • Simplify test setup by removing manual mutex/condition variable
    management
  • Replace sleep-based synchronization with @not_full.wait in MutexPool
  • Add test_waiting_on_startup to verify threads wait on initialization
  • Add test_thread_name as separate test for thread naming
  • Remove skip_on directives and pause calls by using MutexPool
    synchronization
  • Simplify test assertions and remove race condition workarounds
+94/-120
Documentation
History.md
Update changelog with ThreadPool improvements                       

History.md

  • Add entry for fixed concurrency bugs in ThreadPool affecting non-GVL
    Rubies
  • Add entry for ThreadPool concurrency refactoring in Refactor section
+2/-0     

- 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>
@qodo-code-review
Copy link

PR Compliance Guide 🔍

Below is a summary of compliance checks for this PR:

Security Compliance
🟢
No security concerns identified No security vulnerabilities detected by AI analysis. Human verification advised for critical code.
Ticket Compliance
🎫 No ticket provided
  • Create ticket/issue
Codebase Duplication Compliance
Codebase context is not defined

Follow the guide to enable codebase context checks.

Custom Compliance
🟢
Generic: Comprehensive Audit Trails

Objective: To create a detailed and reliable record of critical system actions for security analysis
and compliance.

Status: Passed

Learn more about managing compliance generic rules or creating your own custom rules

Generic: Meaningful Naming and Self-Documenting Code

Objective: Ensure all identifiers clearly express their purpose and intent, making code
self-documenting

Status: Passed

Learn more about managing compliance generic rules or creating your own custom rules

Generic: Secure Error Handling

Objective: To prevent the leakage of sensitive system information through error messages while
providing sufficient detail for internal debugging.

Status: Passed

Learn more about managing compliance generic rules or creating your own custom rules

Generic: Secure Logging Practices

Objective: To ensure logs are useful for debugging and auditing without exposing sensitive
information like PII, PHI, or cardholder data.

Status: Passed

Learn more about managing compliance generic rules or creating your own custom rules

Generic: Security-First Input Validation and Data Handling

Objective: Ensure all data inputs are validated, sanitized, and handled securely to prevent
vulnerabilities

Status: Passed

Learn more about managing compliance generic rules or creating your own custom rules

Generic: Robust Error Handling and Edge Case Management

Objective: Ensure comprehensive error handling that provides meaningful context and graceful
degradation

Status:
Unbounded wait risk: The new MutexPool#<< test helper waits on @not_full without a timeout, which can
hang tests indefinitely if the signal is missed or the worker never reaches the waiting
state.

Referred Code
def <<(work, &block)
  work = [work] unless work.is_a?(Array)
  with_mutex do
    work.each {|arg| super arg}
    yield if block_given?
    @not_full.wait(@mutex)
  end
end

Learn more about managing compliance generic rules or creating your own custom rules

Compliance status legend 🟢 - Fully Compliant
🟡 - Partial Compliant
🔴 - Not Compliant
⚪ - Requires Further Human Verification
🏷️ - Compliance label

@qodo-code-review
Copy link

PR Code Suggestions ✨

No code suggestions found for the PR.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants