How can I make a non-blocking request for an exclusive lock using File#flock? How can I make a non-blocking request for an exclusive lock using File#flock? ruby ruby

How can I make a non-blocking request for an exclusive lock using File#flock?


Use Timeout Module with Exclusive Locks

You can use the Timeout module to set a duration for #flock to acquire an exclusive lock. The following example will raise Timeout::Error: execution expired, which can then be rescued in whatever way seems appropriate for the application. Returning nil when the timer expires allows the #flock expression to be tested for truth.

require 'timeout'f1 = File.open('foo', File::RDWR|File::CREAT, 0644)f1.flock(File::LOCK_EX)# => 0f2 = File.open('foo', File::RDWR|File::CREAT, 0644)Timeout::timeout(0.001) { f2.flock(File::LOCK_EX) } rescue nil# => nil

Use Bitwise OR for Non-Blocking Lock Attempts

The documentation for File#flock says:

Locks or unlocks a file according to locking_constant (a logical or of the values in the table below). Returns false if File::LOCK_NB is specified and the operation would otherwise have blocked.

However, the method actually expects a Bitwise OR operator, rather than a Logical OR keyword as defined in parse.y by the tOROP parser token. As a result, the correct argument that allows #flock to return false when an exclusive lock fails is actually File::LOCK_NB|File::LOCK_EX. For example:

f1 = File.open('foo', File::RDWR|File::CREAT, 0644)f1.flock(File::LOCK_EX|File::LOCK_NB)# => 0f2 = File.open('foo', File::RDWR|File::CREAT, 0644)f2.flock(File::LOCK_NB|File::LOCK_EX)# => falsef1.close; f2.close# => nil

This will consistently generate an exclusive lock when available; otherwise, it immediately returns a falsy value without the overhead of raising or rescuing exceptions. This is obviously the way the module is intended to be used, but the documentation could use some clarification and additional examples to make it easier to understand.