O que pode causar rubi para travar ao sair?
Pergunta
Eu tenho um script Ruby que faz algumas operações Perforce (através da API scripting) então simplesmente termina:
def foo()
...
end
def bar()
...
end
foo()
bar()
puts __LINE__
exit 0
#end of file
... e enquanto o LINHA irá imprimir, o processo nunca termina, se a saída (0) está lá ou não. Esta é ruby ??1.8.6, principalmente no mac, mas eu estou vendo isso no PC também.
Eu estou fazendo o google habitual bisbilhotando, mas esperava que pode haver uma voz da experiência aqui para banco on. Obrigado.
Solução
Eu não estou familiarizado com a todos forçosamente, mas o culpado pode ser uma at_exit
método que está preso em um loop por algum motivo. Observar o comportamento usando irb
, por exemplo:
$ irb
irb(main):001:0> require 'time'
=> true
irb(main):002:0> at_exit { puts Time.now; sleep 10; puts Time.now }
=> #<Proc:0xb7656f64@(irb):2>
irb(main):003:0> exit
Fri Mar 12 19:46:25 -0500 2010
Fri Mar 12 19:46:35 -0500 2010
Para confirmar se uma função at_exit
está causando o processo para pendurar, você pode tentar ligar para at_exit
. Note que o #{caller}
irá exibir o rastreamento de pilha de quem chamou at_exit
:
def at_exit &block
@at_exit_counter ||= 0
puts "at_exit #{@at_exit_counter} called"
s = "Calling at_exit ##{@at_exit_counter} from #{caller}"
super { puts s; block.call() }
@at_exit_counter += 1
end
at_exit { puts "I'll never return"; sleep 1 while true; }
at_exit { puts 'I am about to leave.' }
def do_cleanup
puts "Cleaning..."
at_exit { puts 'Cleaning up before exit...' }
end
do_cleanup
que saídas:
at_exit 0 called at_exit 1 called Cleaning... at_exit 2 called Calling at_exit #2 from exitcheck.rb:14:in `do_cleanup'exitcheck.rb:18 Cleaning up before exit... Calling at_exit #1 from exitcheck.rb:10 I am about to leave. Calling at_exit #0 from exitcheck.rb:9 I'll never return
e nunca retorna.