什么可能导致红宝石在退出时锁定?

时间:2010-03-13 00:37:00

标签: ruby locking exit

我有一个ruby脚本,可以执行一些perforce操作(通过脚本API)然后只是结束:

def foo()
  ...
end

def bar()
  ...
end

foo()
bar()
puts __LINE__
exit 0
#end of file

...当 LINE 打印出来时,进程永远不会结束,退出(0)是否存在。这是ruby 1.8.6,主要在Mac上,但我也在PC上看到了这一点。

我正在做通常的谷歌搜索,但希望可能有一个经验的声音在这里继续。感谢。

1 个答案:

答案 0 :(得分:1)

我对perforce一点也不熟悉,但罪魁祸首可能是at_exit方法因某种原因而陷入循环。使用irb观察行为,例如:

$ 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

要确认at_exit函数是否导致进程挂起,您可以尝试挂钩at_exit。请注意,#{caller}将显示名为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

输出:

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

永远不会回来。