Lighthouse has a new layout. Prefer the old one? Return to the old layout, and switch back any time from the link at the top of each page.

This project is archived and is in readonly mode.

File#flock can't lock read-only File for exclusive access

#6662

On Solaris and (recently) on JRuby, File#flock will raise EBADF when attempting to acquire an exclusive lock on a File that has only been opened for read.

The case I have found is here: https://github.com/rails/rails/blob/master/activesupport/lib/active...

The filename specified is opened for read only, and then an exclusive lock is attempted.

MRI (all versions) on Solaris will raise EBADF for this case. There may be other platforms that behave the same.

On Java, all platforms will raise an error when attempting to acquire an exclusive lock on a file opened only for read. Up until recently, JRuby was downgrading exclusive locks to shared locks under these circumstances. This was pointed out as a very bad thing to do (giving a shared lock when an exclusive lock was requested), and so we now behave like MRI on Solaris regardless of what platform we run on.

The related JRuby bug is here: http://jira.codehaus.org/browse/JRUBY-5627

JRuby's "stable Rails" CI started failing after this bug was fixed: http://ci.jruby.org/job/rails-3-0-stable/component=activesupport,la...

One possible fix would be to attempt to open the file read/write before acquiring the lock. All cases in our "stable" version of Rails appear to be from active_support/cache/strategy/local_cache.rb, but that appears to no longer call file_lock directly. I have not done any further investigation on Rails master.

Reported by Charles Oliver Nutter · April 1st, 2011 @ 10:12 AM

State: committed
Milestone: none
Assigned to: Aaron Patterson Aaron Patterson
Importance: Low

Activity

  1. Arun Agrawal
    Arun Agrawal
    • Tag set to jruby
    • Assigned user set to rails

    Pull request for this solution is here

    https://github.com/rails/rails/pull/288

    April 18th, 2011 @ 12:41 PM

  2. Arun Agrawal
    Arun Agrawal

    https://github.com/rails/rails/pull/288

    Passes test with

    Jruby-1.6.0
    Jruby-1.6.1
    ruby-1.9.2
    ruby-1.8.7
    ree

    April 18th, 2011 @ 12:43 PM

  3. Aaron Patterson
    Aaron Patterson
    • State changed from new to committed
    • Assigned user changed from rails to Aaron Patterson
    • Importance changed from to Low

    Fixed in master and 3-0-stable. This patch will be released with 3.0.8.

    Thanks.

    April 18th, 2011 @ 05:09 PM