Ruby:open导致死锁

时间:2016-06-17 13:37:24

标签: ruby timeout deadlock open-uri


首先,我是Ruby的初学者,并不熟悉Ruby管理代码的方式并使其工作,所以我确实认为问题是我不知道如何正确使用ruby。
我遇到的问题是死锁,但我没有在我的程序中使用任何线程。此外,错误仅发生在每1000到1500次函数调用一次,因此很难精确定位和纠正 发生问题时,这是完整的错误消息:

/usr/lib/ruby/2.3.0/timeout.rb:95:in `join': No live threads left. Deadlock? (fatal)
    from /usr/lib/ruby/2.3.0/timeout.rb:95:in `ensure in block in timeout'
    from /usr/lib/ruby/2.3.0/timeout.rb:95:in `block in timeout'
    from /usr/lib/ruby/2.3.0/timeout.rb:101:in `timeout'
    from /usr/lib/ruby/2.3.0/net/http.rb:878:in `connect'
    from /usr/lib/ruby/2.3.0/net/http.rb:863:in `do_start'
    from /usr/lib/ruby/2.3.0/net/http.rb:852:in `start'
    from /usr/lib/ruby/2.3.0/open-uri.rb:319:in `open_http'
    from /usr/lib/ruby/2.3.0/open-uri.rb:737:in `buffer_open'
    from /usr/lib/ruby/2.3.0/open-uri.rb:212:in `block in open_loop'
    from /usr/lib/ruby/2.3.0/open-uri.rb:210:in `catch'
    from /usr/lib/ruby/2.3.0/open-uri.rb:210:in `open_loop'
    from /usr/lib/ruby/2.3.0/open-uri.rb:151:in `open_uri'
    from /usr/lib/ruby/2.3.0/open-uri.rb:717:in `open'
    from /usr/lib/ruby/2.3.0/open-uri.rb:35:in `open'
    from /home/mat/travail_perso/RUBY/MangaScrapp_github/sources/utils.rb:85:in `get_pic'
    from /home/mat/travail_perso/RUBY/MangaScrapp_github/mangafox/MF_download.rb:87:in `page_link'
    from /home/mat/travail_perso/RUBY/MangaScrapp_github/mangafox/MF_download.rb:116:in `chapter_link'
    from /home/mat/travail_perso/RUBY/MangaScrapp_github/mangafox/MF_download.rb:142:in `chapter'
    from /home/mat/travail_perso/RUBY/MangaScrapp_github/mangafox/MF_update.rb:57:in `block in MF_manga_missing_chapters'
    from /home/mat/travail_perso/RUBY/MangaScrapp_github/mangafox/MF_update.rb:45:in `reverse_each'
    from /home/mat/travail_perso/RUBY/MangaScrapp_github/mangafox/MF_update.rb:45:in `MF_manga_missing_chapters'
    from /home/mat/travail_perso/RUBY/MangaScrapp_github/mangafox/MF_update.rb:80:in `MF_update'
    from /home/mat/travail_perso/RUBY/MangaScrapp_github/sources/update.rb:5:in `update_manga'
    from /home/mat/travail_perso/RUBY/MangaScrapp_github/sources/update.rb:15:in `block in update_all'
    from /home/mat/travail_perso/RUBY/MangaScrapp_github/sources/update.rb:14:in `each'
    from /home/mat/travail_perso/RUBY/MangaScrapp_github/sources/update.rb:14:in `update_all'
    from /home/mat/travail_perso/RUBY/MangaScrapp_github/sources/update.rb:22:in `update'
    from ./MangaScrap.rb:28:in `<main>'

完整计划的链接在这里:https://github.com/Hellfire01/MangaScrap
问题发生在使用open的3种不同方法中,这次是崩溃的方法:

# conect to link and download picture
def get_pic(link)
  safe_link = link.gsub(/[\[\]]/) { '%%%s' % $&.ord.to_s(16) }
  tries ||= 20
  begin
    page = open(safe_link, "User-Agent" => "Ruby/#{RUBY_VERSION}")
  rescue URI::InvalidURIError => error
    puts "Warning : bad url"
    puts link
    puts "message is : " + error.message
    return nil
  rescue => error
    if tries > 0
    tries -= 1
    sleep(0.2)
    retry
    else
      puts 'could not get picture ' + safe_link + ' after ' + $nb_tries.to_s + ' tries'
      puts "message is : " + error.message
      return nil
    end
  end
  sleep(0.2)
  return page
end

以下是文件的链接:https://github.com/Hellfire01/MangaScrap/blob/master/sources/utils.rb

我想知道的是:
- 我该如何解决这个错误?
- 如果我无法修复此错误,是否可以使用open-uri的替代方法?
我们非常欢迎任何帮助

1 个答案:

答案 0 :(得分:2)

你没有在这里发现所有例外情况。如果在rescue之后未指定任何内容,则表示您正在捕获StandardError,这不是异常层次结构的根。

如果您想确保捕获所有例外并重试打开网址(或您想要的任何行为),您要做的是: rescue Exception => error