Rails rails3 ThreadError: Attempt to unlock a mutex which is locked by another thread

jarorwar · 2013年07月08日 · 最后由 jarorwar 回复于 2013年07月08日 · 4401 次阅读

在 congtroller 中执行了一个从远程借口获取信息的请求,结果远程接口挂了。这个请求就阻塞在了这个 controller 的 method 里。于是乎。所有的请求都无法处理了!请问怎么办啊~

环境:rails3 ,ruby1.9.3 dev 模式。

按 ctrl+c 结果就出现了控制台的一下异常:

^CExiting
[2013-07-08 15:21:11] ERROR ThreadError: Attempt to unlock a mutex which is locked by another thread
    /home/minxr/.rvm/gems/ruby-1.9.3-p392/gems/rack-1.4.5/lib/rack/lock.rb:20:in `unlock'
    /home/minxr/.rvm/gems/ruby-1.9.3-p392/gems/rack-1.4.5/lib/rack/lock.rb:20:in `ensure in call'
    /home/minxr/.rvm/gems/ruby-1.9.3-p392/gems/rack-1.4.5/lib/rack/lock.rb:21:in `call'
    /home/minxr/.rvm/gems/ruby-1.9.3-p392/gems/actionpack-3.2.13/lib/action_dispatch/middleware/static.rb:63:in `call'
    /home/minxr/.rvm/gems/ruby-1.9.3-p392/gems/railties-3.2.13/lib/rails/engine.rb:479:in `call'
    /home/minxr/.rvm/gems/ruby-1.9.3-p392/gems/railties-3.2.13/lib/rails/application.rb:223:in `call'
    /home/minxr/.rvm/gems/ruby-1.9.3-p392/gems/rack-1.4.5/lib/rack/content_length.rb:14:in `call'
    /home/minxr/.rvm/gems/ruby-1.9.3-p392/gems/railties-3.2.13/lib/rails/rack/log_tailer.rb:17:in `call'
    /home/minxr/.rvm/gems/ruby-1.9.3-p392/gems/rack-1.4.5/lib/rack/handler/webrick.rb:59:in `service'
    /home/minxr/.rvm/rubies/ruby-1.9.3-p392/lib/ruby/1.9.1/webrick/httpserver.rb:138:in `service'
    /home/minxr/.rvm/rubies/ruby-1.9.3-p392/lib/ruby/1.9.1/webrick/httpserver.rb:94:in `run'
    /home/minxr/.rvm/rubies/ruby-1.9.3-p392/lib/ruby/1.9.1/webrick/server.rb:191:in `block in start_thread'
[2013-07-08 15:21:11] ERROR ThreadError: Attempt to unlock a mutex which is locked by another thread
    /home/minxr/.rvm/gems/ruby-1.9.3-p392/gems/rack-1.4.5/lib/rack/lock.rb:20:in `unlock'
    /home/minxr/.rvm/gems/ruby-1.9.3-p392/gems/rack-1.4.5/lib/rack/lock.rb:20:in `ensure in call'
    /home/minxr/.rvm/gems/ruby-1.9.3-p392/gems/rack-1.4.5/lib/rack/lock.rb:21:in `call'
    /home/minxr/.rvm/gems/ruby-1.9.3-p392/gems/actionpack-3.2.13/lib/action_dispatch/middleware/static.rb:63:in `call'
    /home/minxr/.rvm/gems/ruby-1.9.3-p392/gems/railties-3.2.13/lib/rails/engine.rb:479:in `call'
    /home/minxr/.rvm/gems/ruby-1.9.3-p392/gems/railties-3.2.13/lib/rails/application.rb:223:in `call'
    /home/minxr/.rvm/gems/ruby-1.9.3-p392/gems/rack-1.4.5/lib/rack/content_length.rb:14:in `call'
    /home/minxr/.rvm/gems/ruby-1.9.3-p392/gems/railties-3.2.13/lib/rails/rack/log_tailer.rb:17:in `call'
    /home/minxr/.rvm/gems/ruby-1.9.3-p392/gems/rack-1.4.5/lib/rack/handler/webrick.rb:59:in `service'
    /home/minxr/.rvm/rubies/ruby-1.9.3-p392/lib/ruby/1.9.1/webrick/httpserver.rb:138:in `service'
    /home/minxr/.rvm/rubies/ruby-1.9.3-p392/lib/ruby/1.9.1/webrick/httpserver.rb:94:in `run'
    /home/minxr/.rvm/rubies/ruby-1.9.3-p392/lib/ruby/1.9.1/webrick/server.rb:191:in `block in start_thread'
/home/minxr/.rvm/gems/ruby-1.9.3-p392/gems/rack-1.4.5/lib/rack/handler/webrick.rb:24:in `shutdown': undefined method `shutdown' for nil:NilClass (NoMethodError)
    from /home/minxr/.rvm/gems/ruby-1.9.3-p392/gems/rack-1.4.5/lib/rack/server.rb:262:in `block in start'
    from /home/minxr/.rvm/rubies/ruby-1.9.3-p392/lib/ruby/1.9.1/webrick/server.rb:121:in `call'
    from /home/minxr/.rvm/rubies/ruby-1.9.3-p392/lib/ruby/1.9.1/webrick/server.rb:121:in `join'
    from /home/minxr/.rvm/rubies/ruby-1.9.3-p392/lib/ruby/1.9.1/webrick/server.rb:121:in `block (2 levels) in start'
    from /home/minxr/.rvm/rubies/ruby-1.9.3-p392/lib/ruby/1.9.1/webrick/server.rb:121:in `each'
    from /home/minxr/.rvm/rubies/ruby-1.9.3-p392/lib/ruby/1.9.1/webrick/server.rb:121:in `block in start'
    from /home/minxr/.rvm/rubies/ruby-1.9.3-p392/lib/ruby/1.9.1/webrick/server.rb:22:in `start'
    from /home/minxr/.rvm/rubies/ruby-1.9.3-p392/lib/ruby/1.9.1/webrick/server.rb:89:in `start'
    from /home/minxr/.rvm/gems/ruby-1.9.3-p392/gems/rack-1.4.5/lib/rack/handler/webrick.rb:13:in `run'
    from /home/minxr/.rvm/gems/ruby-1.9.3-p392/gems/rack-1.4.5/lib/rack/server.rb:268:in `start'
    from /home/minxr/.rvm/gems/ruby-1.9.3-p392/gems/railties-3.2.13/lib/rails/commands/server.rb:70:in `start'
    from /home/minxr/.rvm/gems/ruby-1.9.3-p392/gems/railties-3.2.13/lib/rails/commands.rb:55:in `block in <top (required)>'
    from /home/minxr/.rvm/gems/ruby-1.9.3-p392/gems/railties-3.2.13/lib/rails/commands.rb:50:in `tap'
    from /home/minxr/.rvm/gems/ruby-1.9.3-p392/gems/railties-3.2.13/lib/rails/commands.rb:50:in `<top (required)>'
    from script/rails:6:in `require'
    from script/rails:6:in `<main>'

设置下超时

#1 楼 @heliang7 我觉得这不是超时的问题啊。我感觉,rails 内部就只有一个进程(线程)在处理请求,如果这个进程阻塞了。别的都访问不了了。例如,我访问:http://localhost:3000/get_remote_urlpending 的状态。我在访问 http://localhost:3000/users/1 这个页面也是等待状态~这个请求一直在

#1 楼 @heliang7 再举个例子:例如我访问http://localhost:3000/users 的时候,我把网线拔了(因要访问数据库,所以可以模拟超时),我在访问http://localhost:3000/products 还是 pending 的状态。这个里面其实没有访问数据库的操作

走过路过的能不能帮忙点个 ‘喜欢’ 啊。帮我推到第一页。让大大们帮忙看看啊

不要沉啊。。

因为你是在 dev 模式,所以 rails s 是阻塞的:一个进程卡住,就不服务其他了。

要是你用了 unicorn 之类的多进程服务器的话,应该就不会有这个问题了。

#6 楼 @alsotang 哦。。如果真是这样我就放心了。我们是从 java 转 ruby,搞了一部分,打起架子来,发现了这个问题。真是吓死我了。谢谢了~

#2 楼 @jarorwar 你说的很对,rails 3 就是单进程(线程)的,卡死了就卡死了。

#6 楼 @alsotang 只是可以开多一些进程,但原理还是单进程(线程)的

#8 楼 @heliang7 是不是每一个请求一个进程(线程)呢?如果是这样,那就没问题。如果不是这样。那就有问题了啊

#9 楼 @jarorwar 你猜对了,是单线程

#10 楼 @jjym 哎。真吓人啊。这玩意儿。。多亏各位大大在这里解释啊。谢谢了,@jjym @heliang7 @alsotang

需要 登录 后方可回复, 如果你还没有账号请点击这里 注册