2010-03-13 74 views
0

我有一個Ruby腳本,做一些Perforce的操作(通過腳本API)則乾脆結束:什麼可能導致ruby在退出時鎖定?

def foo() 
    ... 
end 

def bar() 
    ... 
end 

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

...並同時LINE將打印出來,過程永遠不會結束,無論是退出(0)是否存在。這是紅寶石1.8.6,主要是在Mac上,但我也在PC上看到這一點。

我正在做平常的谷歌戳,但希望可能有經驗的聲音在這裏銀行。謝謝。

回答

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:[email protected](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 

,永不返回。

相關問題