From 2b39422870394e1c6258e55dfe60f8332423aacc Mon Sep 17 00:00:00 2001 From: Michael McCandless Date: Thu, 30 Nov 2006 00:07:46 +0000 Subject: [PATCH] LUCENE-669: don't double-close RandomAccessFile in finalize git-svn-id: https://svn.apache.org/repos/asf/lucene/java/trunk@480785 13f79535-47bb-0310-9956-ffa450edef68 --- CHANGES.txt | 6 +++++ .../org/apache/lucene/store/FSDirectory.java | 27 ++++++++++++++++--- 2 files changed, 29 insertions(+), 4 deletions(-) diff --git a/CHANGES.txt b/CHANGES.txt index b2348681f98..c8974088c7f 100644 --- a/CHANGES.txt +++ b/CHANGES.txt @@ -230,6 +230,12 @@ Bug fixes classes from contrib/similarity, as their new home is under contrib/queries. +25. LUCENE-669: Do not double-close the RandomAccessFile in + FSIndexInput/Output during finalize(). Besides sending an + IOException up to the GC, this may also be the cause intermittent + "The handle is invalid" IOExceptions on Windows when trying to + close readers or writers. + Optimizations 1. LUCENE-586: TermDocs.skipTo() is now more efficient for diff --git a/src/java/org/apache/lucene/store/FSDirectory.java b/src/java/org/apache/lucene/store/FSDirectory.java index 62345015cae..4f70cb57324 100644 --- a/src/java/org/apache/lucene/store/FSDirectory.java +++ b/src/java/org/apache/lucene/store/FSDirectory.java @@ -504,11 +504,17 @@ class FSIndexInput extends BufferedIndexInput { } private Descriptor file = null; + + // remember if the file is open, so that we don't try to close it + // more than once + private boolean isOpen; + boolean isClone; private long length; public FSIndexInput(File path) throws IOException { file = new Descriptor(path, "r"); + isOpen = true; length = file.length(); } @@ -533,8 +539,12 @@ class FSIndexInput extends BufferedIndexInput { } public void close() throws IOException { - if (!isClone) + // only close the file if this is not a clone and the + // file has not been closed yet + if (!isClone && isOpen) { file.close(); + isOpen = false; + } } protected void seekInternal(long position) { @@ -566,8 +576,13 @@ class FSIndexInput extends BufferedIndexInput { class FSIndexOutput extends BufferedIndexOutput { RandomAccessFile file = null; + // remember if the file is open, so that we don't try to close it + // more than once + private boolean isOpen; + public FSIndexOutput(File path) throws IOException { file = new RandomAccessFile(path, "rw"); + isOpen = true; } /** output methods: */ @@ -575,8 +590,12 @@ class FSIndexOutput extends BufferedIndexOutput { file.write(b, 0, size); } public void close() throws IOException { - super.close(); - file.close(); + // only close the file if it has not been closed yet + if (isOpen) { + super.close(); + file.close(); + isOpen = false; + } } /** Random-access methods */ @@ -589,7 +608,7 @@ class FSIndexOutput extends BufferedIndexOutput { } protected void finalize() throws IOException { - file.close(); // close the file + close(); // close the file } }