19

After process is completed, I see that the lock file isn't deleted? Is there any reason that why flock keeps the file ? Also how does flock knows if there is a lock acquired ?

Here is the example from a crontab file

* * * * * flock python <script_name>.py
roaima
  • 107,089
  • 14
  • 139
  • 261
wayfare
  • 293
  • 1
  • 2
  • 5
  • Can you give more details, code, etc – Romeo Ninov May 30 '17 at 18:09
  • I saw flock being used in a shell script. The task is executed by cron and if it is already running then it should not run it. – wayfare May 30 '17 at 20:18
  • There are different methods for shell-script based locking. Can you extract the relevant part of the code and include it in your question, please. (Ideally just a few lines, with something like `...` thrown in where appropriate.) – roaima May 30 '17 at 21:07
  • Its like flock python .py cron entry is such that this job runs every minute. @roaima – wayfare Jun 01 '17 at 02:39
  • 1
    Next time please put the information *in your question*. I've done it for you this time. – roaima Jun 01 '17 at 07:48

1 Answers1

27

For most use cases of flock, it's very important that the lock file not be "cleaned up". Otherwise, imagine this scenario:

  • process A opens the lock file, finds it does not exists, so it creates it.
  • process A acquires the lock
  • process B opens the lock (finds it already exists)
  • process B tries to acquire the lock but has to wait
  • process A releases the lock
  • process B acquires the lock instantly
  • process A deletes the lock file
  • process C opens the lock file, finds it does not exists, so it creates a new one. Note that it is now holding open a different lock file that the one that process B has locked.
  • process C tries to acquire the lock and succeeds... but it should have had to wait, because process B still has [a prior incarnation of] the lock file open and locked.
Celada
  • 43,173
  • 5
  • 96
  • 105
  • Thanks. I was not aware that flock waits to get a lock. Do you know if its like a queue ? What happens when I have 10 tasks who needs lock to same resource ? – wayfare May 30 '17 at 20:16
  • 3
    @wayfare What happens when you have 10 processes waiting for the same resource is implementation-dependent. The important point is that only one of them can get said resource at any given time. – Satō Katsura May 31 '17 at 07:13
  • @wayfare flock waiting to get the lock is optional. You can also ask it to fail immediately instead of waiting in case the lock is not available. But the same problem remains. – Celada Jun 01 '17 at 20:35
  • 1
    Then how can you safely unlink the `flock` file if you have many accumulated? – user3019105 Apr 22 '19 at 20:29
  • 1
    You will have to unlink the files at a moment when you are sure no locking process has a chance of running (e.g. a maintenance phase on the server, with all relevant services shut down, cron disabled, etc). – Thibaut Barrère Apr 19 '20 at 16:45