How to find out whether subversion working directory is locked by svn?

A python script is running two parallel python processes ( created via os.fork() ) each of which eventually tries to check out a subversion repository leaf into the same working copy dir.

Before running 'svn co ...' command in a sub-process ( via python subprocess module ) the parent python code checks if the working copy dir already exists.

  if os.path.isdir(checkout_dir):
     # working copy dir already exists
     return checkout_dir

So that if it does there shouldn't be any 'svn co' running, but rather immediate return from the parent function.

Nevertheless some collision happened and one of the python processes failed on 'svn co ..' with the following error.

checked-out failed: svn: Working copy '/tmp/qm_23683' locked
svn: run 'svn cleanup' to remove locks (type 'svn help cleanup' for details)**

So the first question is why the working copy dir existence check didn't work and the second - is there a way to find out that a working copy dir is locked by svn and loop until it is unlocked?


Asked by: Emma401 | Posted: 01-10-2021

Answer 1

Within the directory, there should be a directory called '.svn'. Within this, a file named 'locked' indicates that the directory is locked.

Answered by: Lily708 | Posted: 02-11-2021

Answer 2

This sounds like a potential race condition, in that something like the following can happen:

  1. Process A checks to see if the directory exists (it doesn't yet).
  2. Process B checks to see if the directory exists (it doesn't yet).
  3. Process A invokes svn, which creates the directory.
  4. Process B invokes svn, which subsequently fails.

An easy way to avoid this is to have each process attempt to create the directory rather than checking for its existence. If the other process has already created the directory, the other process is guaranteed to get a well-defined error code under a very wide variety of platforms and filesystems. For instance, this is one of the only reliable ways to do synchronization on many implementations of NFS. Luckily, svn won't care if the working directory already exists.

The Python code would look something like this:

import os, errno

# ...

except OSError, e:
  if e.errno != errno.EEXIST: raise # some other error
  print 'Directory already exists.'
  print 'Successfully created new directory.'

This technique is easy to implement, very reliable, and useful in a wide variety of situations.

Answered by: David125 | Posted: 02-11-2021

Similar questions

python - Turn subversion path into walkable directory

I have a subversion repo ie "http://crsvn/trunk/foo" ... I want to walk this directory or for starters simply to a directory list. The idea is to create a script that will do mergeinfo on all the branches in "http://crsvn/branches/bar" and compare them to trunk to see if the branch has been merged. So the first problem I have is that I cannot walk or do os.listdir('http://crsvn/branches/b...

python - ctypes bindings for Subversion in windows

Is there a binary installer or a faq for the new ctypes bindings for Subversion 1.6 in Windows (32 and 64bit)? What library would you use to make an easy to deploy (both win32 and x64) svn client in python for svn version >= 1.5?

python - Can pysvn 1.6.3 be made to work with Subversion 1.6 under linux?

I see no reference on their website for this. I get pysvn to configure and build, but then it fails all the test. Has anyone had any luck getting this to work under linux?

svn - How to use subversion Ctypes Python Bindings?

Subversion 1.6 introduce something that is called 'Ctypes Python Binding', but it is not documented. Is it any information available what this bindings are and how to use it? For example, i have a fresh windows XP and want to control SVN repository using subversiion 1.6 and this mysterious python bindings. What exactly i need to download/install/compile in order to do something like import svn from almighty...

Using python scripts in subversion hooks on windows

My main goal is to get this up and running. My hook gets called when I do the commit with Tortoise SVN, but it always exits when I get to this line: Python "" -p "%TRAC_ENV%" -r "%REV%" || EXIT 5 If I try and replace the call to the python script...

windows - Subversion python bindings could not be loaded

This is a but of a part 2 in trying to convert an SVN repository to a Mercurial one command is: hg convert file://c:/svnrepository but, the output I get is: assuming destination svnrepository-hg initializing destination svnrepository-hg repository file://c:/svnrepository does not look like a CVS checkout file://c:/svnrepository does not look like a Git repo Subversi...

python - Does PySVN need Subversion installed?

I have python script that uses pysvn and checks out or updates a local copy obtained also from a local repo. client.checkout(url, path, revision=pysvn.Revision(pysvn.opt_revision_kind.number, RevNumber), ignore_externals=False) I am running this on a windows machine in which I haven't installed subversion. The svnsync used to obtain a copy of an actual repo was done on another machine. Wi...

python - Yet another Subversion "Commit failed" MERGE of 'blabla': 200 OK

I get the infamous "MERGE of 'whatever': 200 OK" whenever I try to commit using a post-commit hook on Windows (running the repository and Trac locally), and I'm going crazy. I've been looking all over for a day now, without finding any solutions. So here's how it's set up and what I've tried so far: Settings: Windows 7 (64-bit) VisualSVN Server TortoiseSVN Trac 0.11.6 I'm using the three st...

php - subversion post commit hooks

I've got a situation where I'm contemplating using subversion/svn as the repository/version control system for a project. I'm trying to figure out if it's possible, (and if so, how) to be able to have the subversion system, on a post commit hook/process to to write the user/file/time (and maybe msg) to either an external file (csv) or to a mysql db. Once I can figure out how to invoke the post commit hook to write...

python - How do you step through a Subversion hook script with pdb?

I've Googled for this far & wide and turned up nothing. I've written a pre-commit hook for Subversion in Python. If I insert pdb.setTrace() into my hook script and call it manually from the command-line, I can step through it as normal. However, if I do a commit & allow Subversion to call the script, the call to setTrace() causes the script to print some traceback information to the console & die, instead of wh...

svn - Python Subversion bindings which package nicely with `pip`?

Are there any usable/documented Python bindings for Subversion that package nicely using pip? I'm primarily concerned with adding the bindings to a virtual environment. My goal is to be able to do something like pip install <pkg>. Packages I've tried: pysvn svn subvertpy Of these, s...

Still can't find your answer? Check out these communities...

PySlackers | Full Stack Python | NHS Python | Pythonist Cafe | Hacker Earth | Discord Python