ThreadPool concurrency refactoring (#2220)#3
Open
MitchLewis930 wants to merge 1 commit intopr_053_beforefrom
Open
ThreadPool concurrency refactoring (#2220)#3MitchLewis930 wants to merge 1 commit intopr_053_beforefrom
MitchLewis930 wants to merge 1 commit intopr_053_beforefrom
Conversation
- 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>
Greptile OverviewGreptile SummaryThis PR refactors the ThreadPool implementation to fix concurrency bugs affecting non-GVL Ruby implementations (JRuby, TruffleRuby). Key Changes:
Impact: Confidence Score: 4/5
Important Files Changed
Sequence DiagramsequenceDiagram
participant Main as Main Thread
participant TP as ThreadPool
participant WT as Worker Thread
participant Mutex as @mutex
Note over Main,TP: Initialization (lines 56-61)
Main->>+TP: initialize(min, max)
loop min times
TP->>Mutex: synchronize
TP->>+WT: spawn_thread
WT->>WT: Setup local vars
WT->>Mutex: synchronize
WT->>WT: @waiting += 1
WT->>Main: signal @not_full
WT->>WT: wait @not_empty
Note over Main,WT: Wait ensures worker is ready
Main->>Main: @not_full.wait(@mutex)
Main->>TP: Continue initialization
end
Note over Main,WT: Adding Work (<<)
Main->>+TP: << (work)
TP->>Mutex: with_mutex
alt mutex already owned
TP->>TP: yield (no re-lock)
else mutex not owned
TP->>Mutex: synchronize
end
TP->>TP: @todo << work
TP->>WT: signal @not_empty
WT->>-WT: Wake up, grab work
Note over TP,WT: Trim Request (lines 107-111)
Main->>TP: trim
TP->>Mutex: with_mutex
TP->>TP: @trim_requested += 1
TP->>WT: signal @not_empty
WT->>Mutex: synchronize
WT->>WT: Check @trim_requested > 0
WT->>WT: @trim_requested -= 1
WT->>WT: @spawned -= 1
WT->>WT: @workers.delete(self)
WT->>WT: Thread.exit
Note over WT: Cleanup moved inside mutex
Note over Main,WT: Shutdown (line 284)
Main->>TP: shutdown
TP->>Mutex: with_mutex
TP->>TP: @shutdown = true
TP->>TP: @trim_requested = @spawned
TP->>WT: broadcast @not_empty
WT->>WT: Exit via trim logic
|
Comment on lines
+143
to
+144
| @mutex.owned? ? | ||
| yield : |
There was a problem hiding this comment.
Ternary operator spans lines without clear continuation. Consider reformatting for clarity:
Suggested change
| @mutex.owned? ? | |
| yield : | |
| @mutex.owned? ? yield : @mutex.synchronize(&block) |
Note: If this suggestion doesn't match your team's coding style, reply to this and let me know. I'll remember it for next time!
Prompt To Fix With AI
This is a comment left during a code review.
Path: lib/puma/thread_pool.rb
Line: 143:144
Comment:
Ternary operator spans lines without clear continuation. Consider reformatting for clarity:
```suggestion
@mutex.owned? ? yield : @mutex.synchronize(&block)
```
<sub>Note: If this suggestion doesn't match your team's coding style, reply to this and let me know. I'll remember it for next time!</sub>
How can I resolve this? If you propose a fix, please make it concise.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
PR_053