Skip to content

Apache Lucene open-source search software

LUCENE-2053: throw ThreadInterruptedException when the thread is interrupt()d

Michael McCandless4422d07e8af2
diff --git a/CHANGES.txt b/CHANGES.txt
index 36740698afd..8a828b4ba27 100644
--- a/CHANGES.txt
+++ b/CHANGES.txt
@@ -16,6 +16,14 @@ Changes in backwards compatibility policy
the internal cache implementation for thread safety, before it was
declared protected. (Peter Lenahan, Uwe Schindler, Simon Willnauer)
+* LUCENE-2053: If you call Thread.interrupt() on a thread inside
+ Lucene, Lucene will do its best to interrupt the thread. However,
+ instead of throwing InterruptedException (which is a checked
+ exception), you'll get an oal.util.ThreadInterruptedException (an
+ unchecked exception, subclassing RuntimeException). The interrupt
+ status on the thread is cleared when this exception is thrown.
+ (Mike McCandless)
+
Changes in runtime behavior
* LUCENE-1677: Remove the system property to set SegmentReader class
diff --git a/common-build.xml b/common-build.xml
index 37fc21abaa7..64a7e934005 100644
--- a/common-build.xml
+++ b/common-build.xml
@@ -42,7 +42,7 @@
<property name="Name" value="Lucene"/>
<property name="dev.version" value="3.0-dev"/>
<property name="version" value="${dev.version}"/>
- <property name="compatibility.tag" value="lucene_2_9_back_compat_tests_20091111"/>
+ <property name="compatibility.tag" value="lucene_2_9_back_compat_tests_20091114"/>
<property name="spec.version" value="${version}"/>
<property name="year" value="2000-${current.year}"/>
<property name="final.name" value="lucene-${name}-${version}"/>
diff --git a/contrib/benchmark/src/java/org/apache/lucene/benchmark/byTask/feeds/EnwikiContentSource.java b/contrib/benchmark/src/java/org/apache/lucene/benchmark/byTask/feeds/EnwikiContentSource.java
index 84922a3d9f5..71d066c7e60 100644
--- a/contrib/benchmark/src/java/org/apache/lucene/benchmark/byTask/feeds/EnwikiContentSource.java
+++ b/contrib/benchmark/src/java/org/apache/lucene/benchmark/byTask/feeds/EnwikiContentSource.java
@@ -24,6 +24,7 @@ import java.util.HashMap;
import java.util.Map;
import org.apache.lucene.benchmark.byTask.utils.Config;
+import org.apache.lucene.util.ThreadInterruptedException;
import org.xml.sax.Attributes;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
@@ -66,6 +67,7 @@ public class EnwikiContentSource extends ContentSource {
try {
wait();
} catch (InterruptedException ie) {
+ throw new ThreadInterruptedException(ie);
}
}
if (nmde != null) {
@@ -127,6 +129,7 @@ public class EnwikiContentSource extends ContentSource {
try {
wait();
} catch (InterruptedException ie) {
+ throw new ThreadInterruptedException(ie);
}
}
tuple = tmpTuple;
diff --git a/contrib/benchmark/src/java/org/apache/lucene/benchmark/byTask/feeds/TrecContentSource.java b/contrib/benchmark/src/java/org/apache/lucene/benchmark/byTask/feeds/TrecContentSource.java
index bf0ecb79d0b..dba774b3e8d 100644
--- a/contrib/benchmark/src/java/org/apache/lucene/benchmark/byTask/feeds/TrecContentSource.java
+++ b/contrib/benchmark/src/java/org/apache/lucene/benchmark/byTask/feeds/TrecContentSource.java
@@ -33,6 +33,7 @@ import java.util.zip.GZIPInputStream;
import org.apache.lucene.benchmark.byTask.utils.Config;
import org.apache.lucene.benchmark.byTask.utils.StringBufferReader;
+import org.apache.lucene.util.ThreadInterruptedException;
/**
* Implements a {@link ContentSource} over the TREC collection.
@@ -302,10 +303,8 @@ public class TrecContentSource extends ContentSource {
try {
docData = htmlParser.parse(docData, name, date, r, null);
addDoc();
- } catch (InterruptedException e) {
- IOException ex = new IOException(e.getMessage());
- ex.initCause(e);
- throw ex;
+ } catch (InterruptedException ie) {
+ throw new ThreadInterruptedException(ie);
}
return docData;
diff --git a/src/java/org/apache/lucene/index/ConcurrentMergeScheduler.java b/src/java/org/apache/lucene/index/ConcurrentMergeScheduler.java
index e362fd6f4af..bdb3bc918e2 100644
--- a/src/java/org/apache/lucene/index/ConcurrentMergeScheduler.java
+++ b/src/java/org/apache/lucene/index/ConcurrentMergeScheduler.java
@@ -18,6 +18,7 @@ package org.apache.lucene.index;
*/
import org.apache.lucene.store.Directory;
+import org.apache.lucene.util.ThreadInterruptedException;
import java.io.IOException;
import java.util.List;
@@ -130,10 +131,7 @@ public class ConcurrentMergeScheduler extends MergeScheduler {
try {
wait();
} catch (InterruptedException ie) {
- // In 3.0 we will change this to throw
- // InterruptedException instead
- Thread.currentThread().interrupt();
- throw new RuntimeException(ie);
+ throw new ThreadInterruptedException(ie);
}
}
}
@@ -200,10 +198,7 @@ public class ConcurrentMergeScheduler extends MergeScheduler {
try {
wait();
} catch (InterruptedException ie) {
- // In 3.0 we will change this to throw
- // InterruptedException instead
- Thread.currentThread().interrupt();
- throw new RuntimeException(ie);
+ throw new ThreadInterruptedException(ie);
}
}
@@ -347,9 +342,7 @@ public class ConcurrentMergeScheduler extends MergeScheduler {
// cases:
Thread.sleep(250);
} catch (InterruptedException ie) {
- Thread.currentThread().interrupt();
- // In 3.0 this will throw InterruptedException
- throw new RuntimeException(ie);
+ throw new ThreadInterruptedException(ie);
}
throw new MergePolicy.MergeException(exc, dir);
}
diff --git a/src/java/org/apache/lucene/index/DocumentsWriter.java b/src/java/org/apache/lucene/index/DocumentsWriter.java
index c3f6437aaed..90d539c398b 100644
--- a/src/java/org/apache/lucene/index/DocumentsWriter.java
+++ b/src/java/org/apache/lucene/index/DocumentsWriter.java
@@ -38,6 +38,7 @@ import org.apache.lucene.store.AlreadyClosedException;
import org.apache.lucene.store.Directory;
import org.apache.lucene.util.ArrayUtil;
import org.apache.lucene.util.Constants;
+import org.apache.lucene.util.ThreadInterruptedException;
/**
* This class accepts multiple added documents and directly
@@ -513,10 +514,7 @@ final class DocumentsWriter {
try {
wait();
} catch (InterruptedException ie) {
- // In 3.0 we will change this to throw
- // InterruptedException instead
- Thread.currentThread().interrupt();
- throw new RuntimeException(ie);
+ throw new ThreadInterruptedException(ie);
}
}
@@ -851,10 +849,7 @@ final class DocumentsWriter {
try {
wait();
} catch (InterruptedException ie) {
- // In 3.0 we will change this to throw
- // InterruptedException instead
- Thread.currentThread().interrupt();
- throw new RuntimeException(ie);
+ throw new ThreadInterruptedException(ie);
}
}
@@ -1108,10 +1103,7 @@ final class DocumentsWriter {
try {
wait();
} catch (InterruptedException ie) {
- // In 3.0 we will change this to throw
- // InterruptedException instead
- Thread.currentThread().interrupt();
- throw new RuntimeException(ie);
+ throw new ThreadInterruptedException(ie);
}
} while (!waitQueue.doResume());
}
diff --git a/src/java/org/apache/lucene/index/IndexWriter.java b/src/java/org/apache/lucene/index/IndexWriter.java
index 4d0d7186cb8..eb370449601 100644
--- a/src/java/org/apache/lucene/index/IndexWriter.java
+++ b/src/java/org/apache/lucene/index/IndexWriter.java
@@ -28,6 +28,7 @@ import org.apache.lucene.store.LockObtainFailedException;
import org.apache.lucene.store.AlreadyClosedException;
import org.apache.lucene.store.BufferedIndexInput;
import org.apache.lucene.util.Constants;
+import org.apache.lucene.util.ThreadInterruptedException;
import java.io.IOException;
import java.io.Closeable;
@@ -144,6 +145,13 @@ import java.util.Map;
synchronize on the <code>IndexWriter</code> instance as
this may cause deadlock; use your own (non-Lucene) objects
instead. </p>
+
+ <p><b>NOTE</b>: If you call
+ <code>Thread.interrupt()</code> on a thread that's within
+ IndexWriter, IndexWriter will try to catch this (eg, if
+ it's in a wait() or Thread.sleep()), and will then throw
+ the unchecked exception {@link ThreadInterruptedException}
+ and <b>clear</b> the interrupt status on the thread.</p>
*/
/*
@@ -4506,10 +4514,7 @@ public class IndexWriter implements Closeable {
try {
synced.wait();
} catch (InterruptedException ie) {
- // In 3.0 we will change this to throw
- // InterruptedException instead
- Thread.currentThread().interrupt();
- throw new RuntimeException(ie);
+ throw new ThreadInterruptedException(ie);
}
}
}
@@ -4527,10 +4532,7 @@ public class IndexWriter implements Closeable {
try {
wait(1000);
} catch (InterruptedException ie) {
- // In 3.0 we will change this to throw
- // InterruptedException instead
- Thread.currentThread().interrupt();
- throw new RuntimeException(ie);
+ throw new ThreadInterruptedException(ie);
}
}
diff --git a/src/java/org/apache/lucene/index/SegmentInfos.java b/src/java/org/apache/lucene/index/SegmentInfos.java
index 9972bd49184..cb1a3ccc88d 100644
--- a/src/java/org/apache/lucene/index/SegmentInfos.java
+++ b/src/java/org/apache/lucene/index/SegmentInfos.java
@@ -23,6 +23,7 @@ import org.apache.lucene.store.IndexOutput;
import org.apache.lucene.store.ChecksumIndexOutput;
import org.apache.lucene.store.ChecksumIndexInput;
import org.apache.lucene.store.NoSuchDirectoryException;
+import org.apache.lucene.util.ThreadInterruptedException;
import java.io.FileNotFoundException;
import java.io.IOException;
@@ -610,10 +611,7 @@ public final class SegmentInfos extends Vector<SegmentInfo> {
try {
Thread.sleep(defaultGenFileRetryPauseMsec);
} catch (InterruptedException ie) {
- // In 3.0 we will change this to throw
- // InterruptedException instead
- Thread.currentThread().interrupt();
- throw new RuntimeException(ie);
+ throw new ThreadInterruptedException(ie);
}
}
diff --git a/src/java/org/apache/lucene/search/FilterManager.java b/src/java/org/apache/lucene/search/FilterManager.java
index 870e8d9198e..608f243890b 100644
--- a/src/java/org/apache/lucene/search/FilterManager.java
+++ b/src/java/org/apache/lucene/search/FilterManager.java
@@ -24,6 +24,8 @@ import java.util.Iterator;
import java.util.Map;
import java.util.TreeSet;
+import org.apache.lucene.util.ThreadInterruptedException;
+
/**
* Filter caching singleton. It can be used
* to save filters locally for reuse.
@@ -193,8 +195,7 @@ public class FilterManager {
try {
Thread.sleep(cleanSleepTime);
} catch (InterruptedException ie) {
- Thread.currentThread().interrupt();
- throw new RuntimeException(ie);
+ throw new ThreadInterruptedException(ie);
}
}
}
diff --git a/src/java/org/apache/lucene/search/ParallelMultiSearcher.java b/src/java/org/apache/lucene/search/ParallelMultiSearcher.java
index ed8615a1422..ae6ad46abb7 100644
--- a/src/java/org/apache/lucene/search/ParallelMultiSearcher.java
+++ b/src/java/org/apache/lucene/search/ParallelMultiSearcher.java
@@ -33,6 +33,7 @@ import org.apache.lucene.index.IndexReader;
import org.apache.lucene.index.Term;
import org.apache.lucene.util.NamedThreadFactory;
import org.apache.lucene.util.PriorityQueue;
+import org.apache.lucene.util.ThreadInterruptedException;
/** Implements parallel search over a set of <code>Searchables</code>.
*
@@ -186,11 +187,8 @@ public class ParallelMultiSearcher extends MultiSearcher {
if (e.getCause() instanceof IOException)
throw (IOException) e.getCause();
throw new RuntimeException(e.getCause());
- } catch (InterruptedException e) {
- Thread.currentThread().interrupt();
- // In 3.0 we will change this to throw
- // InterruptedException instead
- throw new RuntimeException(e);
+ } catch (InterruptedException ie) {
+ throw new ThreadInterruptedException(ie);
}
}
}
diff --git a/src/java/org/apache/lucene/search/TimeLimitingCollector.java b/src/java/org/apache/lucene/search/TimeLimitingCollector.java
index 9e623a047b4..8a94ac38151 100644
--- a/src/java/org/apache/lucene/search/TimeLimitingCollector.java
+++ b/src/java/org/apache/lucene/search/TimeLimitingCollector.java
@@ -20,6 +20,7 @@ package org.apache.lucene.search;
import java.io.IOException;
import org.apache.lucene.index.IndexReader;
+import org.apache.lucene.util.ThreadInterruptedException;
/**
* The {@link TimeLimitingCollector} is used to timeout search requests that
@@ -78,8 +79,7 @@ public class TimeLimitingCollector extends Collector {
try {
Thread.sleep( resolution );
} catch (InterruptedException ie) {
- Thread.currentThread().interrupt();
- throw new RuntimeException(ie);
+ throw new ThreadInterruptedException(ie);
}
}
}
diff --git a/src/java/org/apache/lucene/store/FSDirectory.java b/src/java/org/apache/lucene/store/FSDirectory.java
index fd7891b6c39..7bf3100446a 100644
--- a/src/java/org/apache/lucene/store/FSDirectory.java
+++ b/src/java/org/apache/lucene/store/FSDirectory.java
@@ -24,6 +24,7 @@ import java.io.RandomAccessFile;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
+import org.apache.lucene.util.ThreadInterruptedException;
import org.apache.lucene.util.Constants;
/**
@@ -317,10 +318,7 @@ public abstract class FSDirectory extends Directory {
// Pause 5 msec
Thread.sleep(5);
} catch (InterruptedException ie) {
- // In 3.0 we will change this to throw
- // InterruptedException instead
- Thread.currentThread().interrupt();
- throw new RuntimeException(ie);
+ throw new ThreadInterruptedException(ie);
}
}
}
diff --git a/src/java/org/apache/lucene/store/Lock.java b/src/java/org/apache/lucene/store/Lock.java
index d017dc54ad3..f56dd1d9511 100644
--- a/src/java/org/apache/lucene/store/Lock.java
+++ b/src/java/org/apache/lucene/store/Lock.java
@@ -17,6 +17,7 @@ package org.apache.lucene.store;
* limitations under the License.
*/
+import org.apache.lucene.util.ThreadInterruptedException;
import java.io.IOException;
/** An interprocess mutex lock.
@@ -88,11 +89,8 @@ public abstract class Lock {
}
try {
Thread.sleep(LOCK_POLL_INTERVAL);
- } catch (InterruptedException e) {
- // In 3.0 we will change this to throw
- // InterruptedException instead
- Thread.currentThread().interrupt();
- throw new IOException(e.toString());
+ } catch (InterruptedException ie) {
+ throw new ThreadInterruptedException(ie);
}
locked = obtain();
}
diff --git a/src/java/org/apache/lucene/store/RAMDirectory.java b/src/java/org/apache/lucene/store/RAMDirectory.java
index 4f9742c1276..8edc43b24b2 100644
--- a/src/java/org/apache/lucene/store/RAMDirectory.java
+++ b/src/java/org/apache/lucene/store/RAMDirectory.java
@@ -22,6 +22,7 @@ import java.io.FileNotFoundException;
import java.io.Serializable;
import java.util.HashMap;
import java.util.Set;
+import org.apache.lucene.util.ThreadInterruptedException;
/**
* A memory-resident {@link Directory} implementation. Locking
@@ -124,10 +125,7 @@ public class RAMDirectory extends Directory implements Serializable {
try {
Thread.sleep(0, 1);
} catch (InterruptedException ie) {
- // In 3.0 we will change this to throw
- // InterruptedException instead
- Thread.currentThread().interrupt();
- throw new RuntimeException(ie);
+ throw new ThreadInterruptedException(ie);
}
ts2 = System.currentTimeMillis();
} while(ts1 == ts2);
diff --git a/src/java/org/apache/lucene/util/ThreadInterruptedException.java b/src/java/org/apache/lucene/util/ThreadInterruptedException.java
new file mode 100644
index 00000000000..51bd3331adb
--- /dev/null
+++ b/src/java/org/apache/lucene/util/ThreadInterruptedException.java
@@ -0,0 +1,30 @@
+package org.apache.lucene.util;
+
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ * <p/>
+ * http://www.apache.org/licenses/LICENSE-2.0
+ * <p/>
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+/**
+ * Thrown by lucene on detecting that Thread.interrupt() had
+ * been called. Unlike Java's InterruptedException, this
+ * exception is not checked..
+ */
+
+public final class ThreadInterruptedException extends RuntimeException {
+ public ThreadInterruptedException(InterruptedException ie) {
+ super(ie);
+ }
+}
diff --git a/src/test/org/apache/lucene/TestSnapshotDeletionPolicy.java b/src/test/org/apache/lucene/TestSnapshotDeletionPolicy.java
index b652ac5ce02..3e5639968c4 100644
--- a/src/test/org/apache/lucene/TestSnapshotDeletionPolicy.java
+++ b/src/test/org/apache/lucene/TestSnapshotDeletionPolicy.java
@@ -36,7 +36,7 @@ import org.apache.lucene.index.KeepOnlyLastCommitDeletionPolicy;
import org.apache.lucene.index.IndexWriter;
import org.apache.lucene.index.TestIndexWriter;
import org.apache.lucene.index.SnapshotDeletionPolicy;
-
+import org.apache.lucene.util.ThreadInterruptedException;
import org.apache.lucene.util.LuceneTestCase;
import org.apache.lucene.util._TestUtil;
@@ -141,8 +141,7 @@ public class TestSnapshotDeletionPolicy extends LuceneTestCase
try {
Thread.sleep(1);
} catch (InterruptedException ie) {
- Thread.currentThread().interrupt();
- throw new RuntimeException(ie);
+ throw new ThreadInterruptedException(ie);
}
}
}
diff --git a/src/test/org/apache/lucene/index/TestIndexWriter.java b/src/test/org/apache/lucene/index/TestIndexWriter.java
index d8d1144db22..ec4532763e1 100644
--- a/src/test/org/apache/lucene/index/TestIndexWriter.java
+++ b/src/test/org/apache/lucene/index/TestIndexWriter.java
@@ -66,6 +66,7 @@ import org.apache.lucene.store.SingleInstanceLockFactory;
import org.apache.lucene.util.UnicodeUtil;
import org.apache.lucene.util._TestUtil;
import org.apache.lucene.util.Version;
+import org.apache.lucene.util.ThreadInterruptedException;
public class TestIndexWriter extends LuceneTestCase {
public TestIndexWriter(String name) {
@@ -2216,8 +2217,7 @@ public class TestIndexWriter extends LuceneTestCase {
try {
Thread.sleep(1);
} catch (InterruptedException ie) {
- Thread.currentThread().interrupt();
- throw new RuntimeException(ie);
+ throw new ThreadInterruptedException(ie);
}
if (fullCount++ >= 5)
break;
@@ -4385,18 +4385,13 @@ public class TestIndexWriter extends LuceneTestCase {
w.addDocument(doc);
w.commit();
}
- } catch (RuntimeException re) {
+ } catch (ThreadInterruptedException re) {
Throwable e = re.getCause();
- if (e instanceof InterruptedException) {
- // Make sure IW restored interrupted bit
- if (!interrupted()) {
- System.out.println("FAILED; InterruptedException hit but thread.interrupted() was false");
- e.printStackTrace(System.out);
- failed = true;
- break;
- }
- } else {
- System.out.println("FAILED; unexpected exception");
+ assertTrue(e instanceof InterruptedException);
+
+ // Make sure IW cleared the interrupted bit
+ if (interrupted()) {
+ System.out.println("FAILED; InterruptedException hit but thread.interrupted() was true");
e.printStackTrace(System.out);
failed = true;
break;
diff --git a/src/test/org/apache/lucene/index/TestIndexWriterReader.java b/src/test/org/apache/lucene/index/TestIndexWriterReader.java
index 109e56c84ba..abd4533f4be 100644
--- a/src/test/org/apache/lucene/index/TestIndexWriterReader.java
+++ b/src/test/org/apache/lucene/index/TestIndexWriterReader.java
@@ -38,6 +38,7 @@ import org.apache.lucene.store.MockRAMDirectory;
import org.apache.lucene.store.AlreadyClosedException;
import org.apache.lucene.util.LuceneTestCase;
import org.apache.lucene.util._TestUtil;
+import org.apache.lucene.util.ThreadInterruptedException;
public class TestIndexWriterReader extends LuceneTestCase {
static PrintStream infoStream;
@@ -360,7 +361,7 @@ public class TestIndexWriterReader extends LuceneTestCase {
try {
threads[i].join();
} catch (InterruptedException ie) {
- Thread.currentThread().interrupt();
+ throw new ThreadInterruptedException(ie);
}
}
}
@@ -402,7 +403,7 @@ public class TestIndexWriterReader extends LuceneTestCase {
try {
threads[i].join();
} catch (InterruptedException ie) {
- Thread.currentThread().interrupt();
+ throw new ThreadInterruptedException(ie);
}
}
diff --git a/src/test/org/apache/lucene/search/TestTimeLimitingCollector.java b/src/test/org/apache/lucene/search/TestTimeLimitingCollector.java
index 4f4ccd1f22a..edc9387edf4 100644
--- a/src/test/org/apache/lucene/search/TestTimeLimitingCollector.java
+++ b/src/test/org/apache/lucene/search/TestTimeLimitingCollector.java
@@ -32,6 +32,7 @@ import org.apache.lucene.store.Directory;
import org.apache.lucene.store.RAMDirectory;
import org.apache.lucene.util.LuceneTestCase;
import org.apache.lucene.util.Version;
+import org.apache.lucene.util.ThreadInterruptedException;
/**
* Tests the {@link TimeLimitingCollector}. This test checks (1) search
@@ -328,8 +329,7 @@ public class TestTimeLimitingCollector extends LuceneTestCase {
try {
Thread.sleep(slowdown);
} catch (InterruptedException ie) {
- Thread.currentThread().interrupt();
- throw new RuntimeException(ie);
+ throw new ThreadInterruptedException(ie);
}
}
assert docId >= 0: " base=" + docBase + " doc=" + doc;