This project is archived and is in readonly mode.

#6662 ✓committed
Charles Oliver Nutter

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

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

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:

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:

JRuby's "stable Rails" CI started failing after this bug was fixed:,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.

Comments and changes to this ticket

Create your profile

Help contribute to this project by taking a few moments to create your personal profile. Create your profile »

<h2 style="font-size: 14px">Tickets have moved to Github</h2>

The new ticket tracker is available at <a href=""></a>