Jason R. Coombs avatar Jason R. Coombs committed 2b154fc

Updated lockfile technique to match behavior of zc.lockfile 1.1.

Comments (0)

Files changed (1)

cherrypy/lib/lockfile.py

 
     def __init__(self, path):
         self.path = path
-        fp = open(path, 'w+')
 
-        self._lock_file(fp)
+        try:
+            # Open lockfile for writing without truncation:
+            fp = open(path, 'r+')
+        except IOError:
+            # If the file doesn't exist, IOError is raised; Use a+ instead.
+            # Note that there may be a race here. Multiple processes
+            # could fail on the r+ open and open the file a+, but only
+            # one will get the the lock and write a pid.
+            fp = open(path, 'a+')
+
+        try:
+            self._lock_file(fp)
+        except:
+            fp.seek(1)
+            fp.close()
+            raise
 
         self.fp = fp
         fp.write(" %s\n" % os.getpid())
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.