threads / patch / 15042

patchOn error in DirCache.lock, do not leave the index locked.

Subject: [JGIT PATCH] On error in DirCache.lock, do not leave the index locked.

## tl;dr

2 messages between Aug 16, 2008 and Aug 18, 2008. Diffs are folded; open one to read it.

replies: 1people: 2as markdown or json

John J. Franey· Aug 16, 2008, 21:36 UTC · lore

After using C git on the same repository, the jgit cache read threw an EOFException, and left the index lock file. The repository is still valid according to C git, but unreachable by C git because it honors the lock file left behind by jgit.

Signed-off-by: John J. Franey <jjfraney@gmail.com>
---
 retry: My first email for this had mail header in mail body - sorry.
 Hi, hope this helps.
 The reason for the EOFException is beyond me right now.  It
 reported "Short read of block."  However, I'm pretty sure
 leaving the lock file in place is an error.
 Regards,
 John
 .../src/org/spearce/jgit/dircache/DirCache.java    |   18 ++++++++++++++++--
 1 files changed, 16 insertions(+), 2 deletions(-)
Show changes to org.spearce.jgit/src/org/spearce/jgit/dircache/DirCache.java +16 −2
diff --git a/org.spearce.jgit/src/org/spearce/jgit/dircache/DirCache.java b/org.spearce.jgit/src/org/spearce/jgit/dircache/DirCache.java
index 280149a..c52d98b 100644
--- a/org.spearce.jgit/src/org/spearce/jgit/dircache/DirCache.java
+++ b/org.spearce.jgit/src/org/spearce/jgit/dircache/DirCache.java
@@ -158,7 +158,8 @@ public static DirCache read(final Repository db)
 	 * <p>
 	 * The new index will be locked and then read before it is returned to the
 	 * caller. Read failures are reported as exceptions and therefore prevent
-	 * the method from returning a partially populated index.
+	 * the method from returning a partially populated index.  On read failure,
+	 * the lock is released.
 	 *
 	 * @param indexLocation
 	 *            location of the index file on disk.
@@ -176,7 +177,20 @@ public static DirCache lock(final File indexLocation)
 		final DirCache c = new DirCache(indexLocation);
 		if (!c.lock())
 			throw new IOException("Cannot lock " + indexLocation);
-		c.read();
+		
+		try {
+			c.read();
+		} catch(IOException e) {
+			c.unlock();
+			throw e;
+		} catch(RuntimeException e) {
+			c.unlock();
+			throw e;
+		} catch(Error e) {
+			c.unlock();
+			throw e;
+		}
+		
 		return c;
 	}
 
-- 
1.6.0.rc1.71.gfba5
Shawn O. Pearce· Aug 18, 2008, 05:46 UTC · re: John J. Franey · lore

Re: [JGIT PATCH] On error in DirCache.lock, do not leave the index locked.

"John J. Franey" <jjfraney@gmail.com> wrote:
Show 5 quoted lines
> After using C git on the same repository, the jgit cache read
> threw an EOFException, and left the index lock file.
> The repository is still valid according to C git, but
> unreachable by C git because it honors the lock file
> left behind by jgit.
Urgh.  Thanks for the patch.
 
>  The reason for the EOFException is beyond me right now.  It
>  reported "Short read of block."  However, I'm pretty sure
>  leaving the lock file in place is an error.
If you can reproduce it, I'd like to see a copy of the index file
if that is possible.  I was pretty certain DirCache honors the
index file format properly, but a short read of a block suggests
we misread a length field somewhere.
 
-- 
Shawn.

← back to recent threads