Skip to content

Task counts can miss a just-posted task on JRuby #1121

Open

Description

@sampokuokkanen

On JRuby, a task that was just posted can be missing from both scheduled_task_count and completed_task_count. For a short time the two counts are equal even though the task hasn't run yet. On CRuby the counts are exact.

JavaThreadPoolExecutor returns Java's getTaskCount and getCompletedTaskCount, which Java documents as approximate. A task handed to an idle worker that hasn't started it yet isn't in the queue and isn't counted as running, so neither count includes it. RubyThreadPoolExecutor counts a task when it is posted, so it doesn't have this gap. The docs here don't mention the difference.

Reproduction

require "concurrent"

pool = Concurrent::CachedThreadPool.new
pool.post { } # start a worker, so the next task goes to an idle thread
sleep 0.1

missing = 0
1000.times do
  done = Concurrent::Event.new
  pool.post { done.set }
  scheduled = pool.scheduled_task_count
  completed = pool.completed_task_count
  missing += 1 if scheduled == completed && !done.set?
  done.wait
  sleep 0.001 until pool.completed_task_count == pool.scheduled_task_count
end

puts "posted task missing from both counts: #{missing}/1000"
  • JRuby 10.1.2.0, concurrent-ruby 1.3.8: 952/1000
  • CRuby 4.0.7, concurrent-ruby 1.3.8: 0/1000

Why it matters

Code that waits for a pool to finish by comparing the two counts can stop waiting too early on JRuby. Rails' Action Cable tests do this, and one of them fails now and then on JRuby because of it.

JavaThreadPoolExecutor could count posted and finished tasks itself, as RubyThreadPoolExecutor does. Or the docs could say the counts are approximate on JRuby.

Activity

  1. changed the title [-]scheduled_task_count and completed_task_count miss a just-posted task on JRuby[/-] [+]Task counts can miss a just-posted task on JRuby[/+] on Nov 11, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions