Bug #12342


DRb.stop_service doesn't kill sleeping TimerIdConv threads

Added by jrafanie (Joe Rafaniello) about 6 years ago. Updated almost 6 years ago.

Target version:
ruby -v:
ruby 2.3.1p112 (2016-04-26 revision 54768) [x86_64-darwin15]


According to this commit[1], it's a good idea for DRb.stop_service to kill threads it creates.

My problem is I have a workflow that creates a new DRb server and provides it a TimerIdConv for keeping drb objects alive until it's not accessed for X seconds. It then stops the service.
This workflow can happen many times.

The call creates a new thread each time that never exits.
There isn't a public interface to kill these threads so I have to use instance variables to get the thread so it can be killed.
Also, you can't reuse the TimerIdConv because you don't know how long it has been sleeping and there is not a way to reset it to ensure it sleeps the full timeout.

I would prefer if I could tell DRb.start_service to use a :timer_idconv => 5, have it create the TimerIdConv and be responsible for killing it when I call stop_service.

See code below. I have tested with ruby 2.3.1 and the code below slowly continues to grow in thread count.

[1] Commit: c474ecb0dfe0dd0d5e6b2b41f09eaf251d0c7079 or

require 'drb'
require 'drb/timeridconv'

100.times do
  # setup the drb server with a one second timer
  # creates a new thread that never dies
  timer =

  service = DRb.start_service("druby://", nil, :idconv => timer)

  # ... some code that uses the DRb service

  # stop the drb server now that we're done with it

  # Below is ugly code that kills the timer thread.  Without the code below, the
  # thread stays alive forever.  This should have a public API.
  # It would be nice if DRb.start_service
  # accepted a :timer_idconv => 5 option that would create the timer thread itself
  # and was responsible for killing this thread when calling stop_service.

  # holder = timer.instance_variable_get(:@holder)
  # keeper = holder.instance_variable_get(:@keeper) if holder
  # keeper.kill if keeper
  puts Thread.list.length
  sleep 2

Also available in: Atom PDF