From 853c03ac43fec7c59b63bed47c1df8a7c213d504 Mon Sep 17 00:00:00 2001 From: Michael McCandless Date: Tue, 9 Jan 2007 17:13:57 +0000 Subject: [PATCH] LUCENE-768: make sure IndexReader.close releases write lock even after hitting an exception in one of the methods that acquire the write lock git-svn-id: https://svn.apache.org/repos/asf/lucene/java/trunk@494492 13f79535-47bb-0310-9956-ffa450edef68 --- CHANGES.txt | 5 +++ .../org/apache/lucene/index/IndexReader.java | 4 +-- .../apache/lucene/index/TestIndexReader.java | 34 ++++++++++++++++++- 3 files changed, 40 insertions(+), 3 deletions(-) diff --git a/CHANGES.txt b/CHANGES.txt index 27c2a70dc1b..ca3fc181092 100644 --- a/CHANGES.txt +++ b/CHANGES.txt @@ -295,6 +295,11 @@ Bug fixes in mergeSegments, when the corrupted segment is merged with segment(s) after it. (Mike McCandless) +30. LUCENE-768: Fix case where an Exception during deleteDocument, + undeleteAll or setNorm in IndexReader could leave the reader in a + state where close() fails to release the write lock. + (Mike McCandless) + Optimizations 1. LUCENE-586: TermDocs.skipTo() is now more efficient for diff --git a/src/java/org/apache/lucene/index/IndexReader.java b/src/java/org/apache/lucene/index/IndexReader.java index 9be6dc915a1..1a24696cd21 100644 --- a/src/java/org/apache/lucene/index/IndexReader.java +++ b/src/java/org/apache/lucene/index/IndexReader.java @@ -425,8 +425,8 @@ public abstract class IndexReader { throws IOException{ if(directoryOwner) aquireWriteLock(); - doSetNorm(doc, field, value); hasChanges = true; + doSetNorm(doc, field, value); } /** Implements setNorm in subclass.*/ @@ -580,8 +580,8 @@ public abstract class IndexReader { public final synchronized void undeleteAll() throws IOException{ if(directoryOwner) aquireWriteLock(); - doUndeleteAll(); hasChanges = true; + doUndeleteAll(); } /** Implements actual undeleteAll() in subclass. */ diff --git a/src/test/org/apache/lucene/index/TestIndexReader.java b/src/test/org/apache/lucene/index/TestIndexReader.java index f345cd98a1b..ba3b70685a3 100644 --- a/src/test/org/apache/lucene/index/TestIndexReader.java +++ b/src/test/org/apache/lucene/index/TestIndexReader.java @@ -788,7 +788,39 @@ public class TestIndexReader extends TestCase fail("delete of out-of-bounds doc number failed to hit exception"); } } - + + public void testExceptionReleaseWriteLockJIRA768() throws IOException { + + Directory dir = new RAMDirectory(); + IndexWriter writer = new IndexWriter(dir, new WhitespaceAnalyzer(), true); + addDoc(writer, "aaa"); + writer.close(); + + IndexReader reader = IndexReader.open(dir); + try { + reader.deleteDocument(1); + fail("did not hit exception when deleting an invalid doc number"); + } catch (ArrayIndexOutOfBoundsException e) { + // expected + } + reader.close(); + if (IndexReader.isLocked(dir)) { + fail("write lock is still held after close"); + } + + reader = IndexReader.open(dir); + try { + reader.setNorm(1, "content", (float) 2.0); + fail("did not hit exception when calling setNorm on an invalid doc number"); + } catch (ArrayIndexOutOfBoundsException e) { + // expected + } + reader.close(); + if (IndexReader.isLocked(dir)) { + fail("write lock is still held after close"); + } + } + private String arrayToString(String[] l) { String s = ""; for(int i=0;i