2

I'm trying to use Ruby's Timeout::timeout mechanism to abort a hanging ssh command but it doesn't seem to be working. If I slip in an infinite loop before the ssh call though, it will terminate so I know that I'm doing it right. What's the deal? Is it ssh or open3?

The code is simple enough:

require 'timeout'
require 'open3'

begin
  status = Timeout::timeout(5) do
    r = Open3.capture3('ssh root@192.168.1.1')
    puts 'Debug 1'
  end
rescue Timeout::Error
  puts 'Timeout exceeded'
end

If there is any kind of prompt it will remain there indefinitely without printing either 'Timeout exceeded' or 'Debug 1'.

user8897013
  • 443
  • 4
  • 15
  • Please post working code. Calling `timeout` without a block is an error and you have `rescue` with no `begin` which is a syntax error. – Max Mar 13 '18 at 19:23
  • yes, this was from a function with an implicit begin. i will edit. – user8897013 Mar 13 '18 at 20:22
  • Still invalid. Please make a [MCVE](https://stackoverflow.com/help/mcve) – Max Mar 13 '18 at 20:27
  • omg. done. can you please answer the question if you know the answer? thanks. – user8897013 Mar 13 '18 at 20:46
  • this also seems to be happening with backtick kernel execution. so likely ssh is the problem. – user8897013 Mar 13 '18 at 20:47
  • I'm not smart enough to know exactly what is going on here, but this is a fun read on why Timeouts in Ruby can cause serious issues: http://blog.headius.com/2008/02/rubys-threadraise-threadkill-timeoutrb.html – NateSHolland Mar 14 '18 at 02:55
  • Yes, the problem was due to the link @Max posted and the process not ending on the remote machine. Used timeout --kill-after option to make it work. Will post my own answer later or delete this one upon mod suggestion. – user8897013 Mar 16 '18 at 19:53

0 Answers0