Skip to content

Conversation

@joshuay03
Copy link
Contributor

@joshuay03 joshuay03 commented Feb 8, 2025

Closes #1066
Closes #1075

Alternative to #1079

Implementation is based on the discussion in the linked issues.

@joshuay03 joshuay03 force-pushed the better-ruby-thread-pool-executor-pruning branch 15 times, most recently from b6e5656 to f90d46d Compare February 9, 2025 04:23
@joshuay03 joshuay03 force-pushed the better-ruby-thread-pool-executor-pruning branch 14 times, most recently from c5eca7d to 89b67f4 Compare February 14, 2025 15:54
@joshuay03 joshuay03 force-pushed the better-ruby-thread-pool-executor-pruning branch 7 times, most recently from fd02056 to 6cedac3 Compare March 17, 2025 08:52
Copy link
Member

@eregon eregon left a comment

Choose a reason for hiding this comment

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

Finally taking a look at this, sorry for the delay

@joshuay03 joshuay03 force-pushed the better-ruby-thread-pool-executor-pruning branch 4 times, most recently from defd0f7 to 9739e9a Compare March 26, 2025 09:34
@joshuay03 joshuay03 force-pushed the better-ruby-thread-pool-executor-pruning branch from 9739e9a to 6aa4ee5 Compare March 26, 2025 09:40
Copy link
Member

@eregon eregon left a comment

Choose a reason for hiding this comment

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

The Queue stuff looks ready to me, i.e. I reviewed and approve that part.

The pool changes it's hard for me to tell because I haven't had time to understand the implementation in details (and likely won't have time for that soon).
It would be great if another concurrent-ruby maintainer could help review that (and they should feel free to merge this PR after they approve)

@joshuay03 joshuay03 force-pushed the better-ruby-thread-pool-executor-pruning branch 3 times, most recently from e4f5794 to 4d088a0 Compare April 2, 2025 13:45
@eregon
Copy link
Member

eregon commented Nov 19, 2025

@bensheldon Could you review this?
Or any other concurrent-ruby maintainer more familiar with RubyThreadPoolExecutor of course :)

@bensheldon
Copy link
Contributor

@eregon yes, I will review.

Copy link
Contributor

@bensheldon bensheldon left a comment

Choose a reason for hiding this comment

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

I went through this and I think it looks good. I'm very excited about this new behavior 🙇🏻

  • The Ruby TimeoutQueue implementation looks solid and resilient to idle wakeups and matches the interface of the native Ruby Queue-with-timeout.
  • The pruning behavior in RubyThreadPoolExecutor looks correct. I spent a lot of time trying to understand the prunable behavior, but I think I get it now; I wasn't able to think up a scenario where it wouldn't result in unintended behavior.
  • Test cleanup looks defensible 👍🏻

def prune_worker(worker)
synchronize do
if ns_prunable_capacity > 0
remove_worker worker
Copy link
Contributor

Choose a reason for hiding this comment

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

I think synchronize should be safe to nest, but noting that remove_worker also calls synchronize. Noting this because I've reviewed this twice and each time I've wanted to trace what was happening, which makes me think that having an ns_remove_worker might be clearer, but I don't feel strongly about it.

Copy link
Contributor Author

@joshuay03 joshuay03 Dec 10, 2025

Choose a reason for hiding this comment

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

Yes, MutexLockableObject#synchronize has been implemented to be re-entrant within the same thread:

I decided to reuse remove_worker because of this re-entrancy guarantee, since it's also called independently in the thread loop. I think I'll keep it this way.

@eregon
Copy link
Member

eregon commented Dec 10, 2025

@joshuay03 I'll let you merge this yourself now that you're a maintainer of concurrent-ruby :)

@joshuay03
Copy link
Contributor Author

Thank you both for the reviews!

@joshuay03 joshuay03 merged commit 572d44c into ruby-concurrency:master Dec 10, 2025
16 checks passed
@joshuay03 joshuay03 moved this from In Progress / Pending Review to Done in Open Source Dec 10, 2025
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.

CachedThreadPool does not spin down idle threads Unexpected pruning behaviour with consecutive task batches

3 participants