Issue #2343 has been updated by Hongli Lai.


> Why inside?

My code performs several things within the timeout block. Depending on where exactly the timeout occurred, it must perform some cleanup before re-throwing the exception. The code looks a little bit like this:

---------------

# The *total* time of spawning daemon + waiting for it to become initialized
# must take no longer than 5 seconds.
Timeout.timeout(5) do
   spawn_daemon_process
   wait_for_daemon_to_become_initialized
end

def spawn_daemon_process
   begin
      pid = ...some code here to spawn a process...
      ...wait until 'pid' has daemonized into the background...
   rescue Timeout::Error
      # The daemon failed to daemonize within 5 seconds. Kill it
      # before rethrowing Timeout::Error.
      Process.kill('KILL', pid)
      raise
   end
end

# Here we don't need to perform any cleanup, so we don't catch Timeout::Error.
def wait_for_daemon_to_become_initialized
   done = false
   # The daemon is initialized when we can connect to it. Wait
   # until that happens, or until we time out
   while !done
      begin
         TCPSocket.new('localhost', 1234)
         done = true
      rescue SystemCallError
         # Daemon not yet initialized.
      end
   end
end

---------------
----------------------------------------
http://redmine.ruby-lang.org/issues/show/2343

----------------------------------------
http://redmine.ruby-lang.org