JRuby (please use github issues at http://bugs.jruby.org)
  1. JRuby (please use github issues at http://bugs.jruby.org)
  2. JRUBY-6275

double locking a mutex could use a better error message, line number

    Details

    • Type: Improvement Improvement
    • Status: Resolved Resolved
    • Priority: Major Major
    • Resolution: Fixed
    • Affects Version/s: None
    • Fix Version/s: JRuby 1.7.0.RC1
    • Component/s: None
    • Labels:
      None
    • Environment:
      jruby 1.6.5 (ruby-1.8.7-p330) (2011-10-25 9dcd388) (Java HotSpot(TM) Client VM 1.6.0_26) [Windows XP-x86-java]
    • Testcase included:
      yes
    • Number of attachments :
      0

      Description

      Currently if you attempt to double lock a mutex, it responds with

      "ThreadError: Mutex is not locked"
      but in reality the problem is that the mutex is locked (I think this may be actually a secondary error, obscuring the real error, because it is also reporting the wrong line number, it appears, compared to MRI).

      require 'thread'
      a = Mutex.new
      
        a.synchronize  { 
          a.synchronize { # this line raises
          }
        }
      

        Activity

        Hide
        Hiro Asari added a comment -

        Mutex#synchronize is unlocking the lock in the finally block, so that the Exception that the inner synchronize block throws is swallowed up, and the outer synchronize block tries to unlock the lock that the current thread doesn't have any longer.

        I pushed a fix to the master branch (e6b9be7). Now the example above throws:

        ThreadError: Mutex relocking by same thread
                 lock at org/jruby/ext/thread/Mutex.java:91
          synchronize at org/jruby/ext/thread/Mutex.java:164
             __file__ at temp/jruby-6275.rb:5
          synchronize at org/jruby/ext/thread/Mutex.java:165
               (root) at temp/jruby-6275.rb:4
        

        The error messages are different in MRI 1.8.x and 1.9.3. Consequently, for now, I am not overly concerned with exactly matching the error message.

        Regrettably, there is no RubySpec for Mutex at all.

        Show
        Hiro Asari added a comment - Mutex#synchronize is unlocking the lock in the finally block, so that the Exception that the inner synchronize block throws is swallowed up, and the outer synchronize block tries to unlock the lock that the current thread doesn't have any longer. I pushed a fix to the master branch (e6b9be7). Now the example above throws: ThreadError: Mutex relocking by same thread lock at org/jruby/ext/thread/Mutex.java:91 synchronize at org/jruby/ext/thread/Mutex.java:164 __file__ at temp/jruby-6275.rb:5 synchronize at org/jruby/ext/thread/Mutex.java:165 (root) at temp/jruby-6275.rb:4 The error messages are different in MRI 1.8.x and 1.9.3. Consequently, for now, I am not overly concerned with exactly matching the error message. Regrettably, there is no RubySpec for Mutex at all.
        Hide
        Hiro Asari added a comment -

        That commit broke many CI tasks, so I reverted it.

        Show
        Hiro Asari added a comment - That commit broke many CI tasks, so I reverted it.
        Hide
        Charles Oliver Nutter added a comment -

        Not sure when this was fixed, but it's working correctly now. the "lock" call for "synchronize" happens outside the try, so if it fails it doesn't incorrectly try to unlock.

        system ~/projects/jruby $ jirb
        irb(main):001:0> require 'thread'
        => false
        irb(main):002:0> a = Mutex.new
        => #<Mutex:0x759d3b52>
        irb(main):003:0> a.synchronize {
        irb(main):004:1* a.synchronize {
        irb(main):005:2* }
        irb(main):006:1> }
        ThreadError: Mutex relocking by same thread
        	from org/jruby/ext/thread/Mutex.java:90:in `lock'
        	from org/jruby/ext/thread/Mutex.java:147:in `synchronize'
        	from (irb):4:in `evaluate'
        	from org/jruby/ext/thread/Mutex.java:149:in `synchronize'
        	from (irb):3:in `evaluate'
        	from org/jruby/RubyKernel.java:1064:in `eval'
        	from org/jruby/RubyKernel.java:1389:in `loop'
        	from org/jruby/RubyKernel.java:1172:in `catch'
        	from org/jruby/RubyKernel.java:1172:in `catch'
        	from /Users/headius/projects/jruby/bin/jirb:13:in `(root)'
        irb(main):007:0> 
        
        Show
        Charles Oliver Nutter added a comment - Not sure when this was fixed, but it's working correctly now. the "lock" call for "synchronize" happens outside the try, so if it fails it doesn't incorrectly try to unlock. system ~/projects/jruby $ jirb irb(main):001:0> require 'thread' => false irb(main):002:0> a = Mutex.new => #<Mutex:0x759d3b52> irb(main):003:0> a.synchronize { irb(main):004:1* a.synchronize { irb(main):005:2* } irb(main):006:1> } ThreadError: Mutex relocking by same thread from org/jruby/ext/thread/Mutex.java:90:in `lock' from org/jruby/ext/thread/Mutex.java:147:in `synchronize' from (irb):4:in `evaluate' from org/jruby/ext/thread/Mutex.java:149:in `synchronize' from (irb):3:in `evaluate' from org/jruby/RubyKernel.java:1064:in `eval' from org/jruby/RubyKernel.java:1389:in `loop' from org/jruby/RubyKernel.java:1172:in `catch' from org/jruby/RubyKernel.java:1172:in `catch' from /Users/headius/projects/jruby/bin/jirb:13:in `(root)' irb(main):007:0>

          People

          • Assignee:
            Charles Oliver Nutter
            Reporter:
            Roger Pack
          • Votes:
            0 Vote for this issue
            Watchers:
            1 Start watching this issue

            Dates

            • Created:
              Updated:
              Resolved: