You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@lucenenet.apache.org by pa...@apache.org on 2026/10/23 21:17:41 UTC
(lucenenet) branch master updated: Fix IndexWriter.Dispose breaking the standard Dispose contract, #1399 (#1426)
This is an automated email from the ASF dual-hosted git repository.
paulirwin pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/lucenenet.git
The following commit(s) were added to refs/heads/master by this push:
new 9dfd6758e Fix IndexWriter.Dispose breaking the standard Dispose contract, #1399 (#1426)
9dfd6758e is described below
commit 9dfd6758e9aa40a0d392932f584913f8b3dc229d
Author: Paul Irwin <pa...@gmail.com>
AuthorDate: Fri Oct 23 15:17:36 2026 -0600
Fix IndexWriter.Dispose breaking the standard Dispose contract, #1399 (#1426)
Co-authored-by: Claude Opus 5.5 <no...@anthropic.com>
---
.../ByTask/Tasks/CloseIndexTask.cs | 2 +-
.../Index/ThreadedIndexingAndSearchingTestCase.cs | 2 +-
src/Lucene.Net.Tests/Index/TestAddIndexes.cs | 2 +-
.../Index/TestBackwardsCompatibility.cs | 6 +-
.../Index/TestBackwardsCompatibility3x.cs | 6 +-
.../Index/TestConcurrentMergeScheduler.cs | 6 +-
src/Lucene.Net.Tests/Index/TestCrash.cs | 2 +-
.../Index/TestFlushByRamOrCountsPolicy.cs | 4 +-
.../Index/TestIndexWriterDispose.cs | 218 +++++++++++++++++++++
.../Index/TestIndexWriterMerging.cs | 2 +-
.../Index/TestIndexWriterOnDiskFull.cs | 4 +-
.../Index/TestIndexWriterReader.cs | 2 +-
.../Index/TestIndexWriterWithThreads.cs | 10 +-
src/Lucene.Net/Index/IndexWriter.cs | 112 ++++-------
src/Lucene.Net/Index/MergePolicy.cs | 2 +-
src/Lucene.Net/Index/MergeState.cs | 2 +-
16 files changed, 282 insertions(+), 100 deletions(-)
diff --git a/src/Lucene.Net.Benchmark/ByTask/Tasks/CloseIndexTask.cs b/src/Lucene.Net.Benchmark/ByTask/Tasks/CloseIndexTask.cs
index a3677dab5..9bbb31d19 100644
--- a/src/Lucene.Net.Benchmark/ByTask/Tasks/CloseIndexTask.cs
+++ b/src/Lucene.Net.Benchmark/ByTask/Tasks/CloseIndexTask.cs
@@ -48,7 +48,7 @@ public override int DoLogic()
infoStream.Dispose();
}
#pragma warning disable 612, 618
- iw.Dispose(doWait);
+ iw.Close(doWait);
#pragma warning restore 612, 618
RunData.IndexWriter = null;
}
diff --git a/src/Lucene.Net.TestFramework/Index/ThreadedIndexingAndSearchingTestCase.cs b/src/Lucene.Net.TestFramework/Index/ThreadedIndexingAndSearchingTestCase.cs
index cf99b3b78..be8756565 100644
--- a/src/Lucene.Net.TestFramework/Index/ThreadedIndexingAndSearchingTestCase.cs
+++ b/src/Lucene.Net.TestFramework/Index/ThreadedIndexingAndSearchingTestCase.cs
@@ -773,7 +773,7 @@ public virtual void RunTest(string testName)
DoClose();
#pragma warning disable 612, 618
- m_writer.Dispose(false);
+ m_writer.Close(false);
#pragma warning restore 612, 618
// Cannot shutdown until after writer is closed because
diff --git a/src/Lucene.Net.Tests/Index/TestAddIndexes.cs b/src/Lucene.Net.Tests/Index/TestAddIndexes.cs
index d79d81bed..0efdde901 100644
--- a/src/Lucene.Net.Tests/Index/TestAddIndexes.cs
+++ b/src/Lucene.Net.Tests/Index/TestAddIndexes.cs
@@ -805,7 +805,7 @@ internal virtual void Close(bool doWait)
{
didClose = true;
#pragma warning disable 612, 618
- writer2.Dispose(doWait);
+ writer2.Close(doWait);
#pragma warning restore 612, 618
}
diff --git a/src/Lucene.Net.Tests/Index/TestBackwardsCompatibility.cs b/src/Lucene.Net.Tests/Index/TestBackwardsCompatibility.cs
index e8fbaf1c5..f790407ed 100644
--- a/src/Lucene.Net.Tests/Index/TestBackwardsCompatibility.cs
+++ b/src/Lucene.Net.Tests/Index/TestBackwardsCompatibility.cs
@@ -314,7 +314,7 @@ public virtual void TestUnsupportedOldIndexes()
if (writer != null)
{
#pragma warning disable 612, 618
- writer.Dispose(false);
+ writer.Close(false);
#pragma warning restore 612, 618
}
writer = null;
@@ -1053,7 +1053,7 @@ public virtual void TestUpgradeOldSingleSegmentIndexWithAdditions()
AddDoc(w, id++);
}
#pragma warning disable 612, 618
- w.Dispose(false);
+ w.Close(false);
#pragma warning restore 612, 618
}
@@ -1066,7 +1066,7 @@ public virtual void TestUpgradeOldSingleSegmentIndexWithAdditions()
IndexWriter iw = new IndexWriter(dir, iwc_);
iw.AddIndexes(ramDir);
#pragma warning disable 612, 618
- iw.Dispose(false);
+ iw.Close(false);
#pragma warning restore 612, 618
// determine count of segments in modified index
diff --git a/src/Lucene.Net.Tests/Index/TestBackwardsCompatibility3x.cs b/src/Lucene.Net.Tests/Index/TestBackwardsCompatibility3x.cs
index 214c37e3d..3fd0c9c08 100644
--- a/src/Lucene.Net.Tests/Index/TestBackwardsCompatibility3x.cs
+++ b/src/Lucene.Net.Tests/Index/TestBackwardsCompatibility3x.cs
@@ -245,7 +245,7 @@ public virtual void TestUnsupportedOldIndexes()
if (writer != null)
{
#pragma warning disable 612, 618
- writer.Dispose(false);
+ writer.Close(false);
#pragma warning restore 612, 618
}
writer = null;
@@ -958,7 +958,7 @@ public virtual void TestUpgradeOldSingleSegmentIndexWithAdditions()
AddDoc(w, id++);
}
#pragma warning disable 612, 618
- w.Dispose(false);
+ w.Close(false);
#pragma warning restore 612, 618
}
@@ -970,7 +970,7 @@ public virtual void TestUpgradeOldSingleSegmentIndexWithAdditions()
IndexWriter w_ = new IndexWriter(dir, iwc_);
w_.AddIndexes(ramDir);
#pragma warning disable 612, 618
- w_.Dispose(false);
+ w_.Close(false);
#pragma warning restore 612, 618
// determine count of segments in modified index
diff --git a/src/Lucene.Net.Tests/Index/TestConcurrentMergeScheduler.cs b/src/Lucene.Net.Tests/Index/TestConcurrentMergeScheduler.cs
index aaa566d8d..4a7bb67fe 100644
--- a/src/Lucene.Net.Tests/Index/TestConcurrentMergeScheduler.cs
+++ b/src/Lucene.Net.Tests/Index/TestConcurrentMergeScheduler.cs
@@ -68,7 +68,7 @@ public override void Eval(MockDirectoryWrapper dir)
// LUCENENET specific: for these to work in release mode, we have added [MethodImpl(MethodImplOptions.NoInlining)]
// to each possible target of the StackTraceHelper. If these change, so must the attribute on the target methods.
bool isDoFlush = StackTraceHelper.DoesStackTraceContainMethod(nameof(DocumentsWriterPerThread.Flush));
- bool isClose = StackTraceHelper.DoesStackTraceContainMethod(nameof(IndexWriter.Close)) || // LUCENENET NOTE: Close is aggressively inlined, so likely won't hit this case, but would hit Dispose
+ bool isClose = StackTraceHelper.DoesStackTraceContainMethod(nameof(IndexWriter.Close)) || // LUCENENET NOTE: Close is marked NoInlining so it stays on the stack; Dispose is checked as well as a belt-and-suspenders
StackTraceHelper.DoesStackTraceContainMethod(nameof(IndexWriter.Dispose));
if (isDoFlush && !isClose && Random.NextBoolean())
@@ -262,7 +262,7 @@ public virtual void TestNoWaitClose()
writer.Commit();
#pragma warning disable 612, 618
- writer.Dispose(false);
+ writer.Close(false);
#pragma warning restore 612, 618
IndexReader reader = DirectoryReader.Open(directory);
@@ -319,7 +319,7 @@ public virtual void TestMaxMergeCount()
}
}
#pragma warning disable 612, 618
- w.Dispose(false);
+ w.Close(false);
#pragma warning restore 612, 618
dir.Dispose();
}
diff --git a/src/Lucene.Net.Tests/Index/TestCrash.cs b/src/Lucene.Net.Tests/Index/TestCrash.cs
index 4efcd9e74..d31683ccc 100644
--- a/src/Lucene.Net.Tests/Index/TestCrash.cs
+++ b/src/Lucene.Net.Tests/Index/TestCrash.cs
@@ -211,7 +211,7 @@ public virtual void TestCrashAfterCloseNoWait()
MockDirectoryWrapper dir = (MockDirectoryWrapper)writer.Directory;
#pragma warning disable 612, 618
- writer.Dispose(false);
+ writer.Close(false);
#pragma warning restore 612, 618
dir.Crash();
diff --git a/src/Lucene.Net.Tests/Index/TestFlushByRamOrCountsPolicy.cs b/src/Lucene.Net.Tests/Index/TestFlushByRamOrCountsPolicy.cs
index a462f57ea..6f8182fae 100644
--- a/src/Lucene.Net.Tests/Index/TestFlushByRamOrCountsPolicy.cs
+++ b/src/Lucene.Net.Tests/Index/TestFlushByRamOrCountsPolicy.cs
@@ -314,9 +314,7 @@ public virtual void TestStallControl()
Assert.IsTrue(docsWriter.flushControl.stallControl.WasStalled);
}
AssertActiveBytesAfter(flushControl);
-#pragma warning disable 612, 618
- writer.Dispose(true);
-#pragma warning restore 612, 618
+ writer.Dispose();
dir.Dispose();
}
}
diff --git a/src/Lucene.Net.Tests/Index/TestIndexWriterDispose.cs b/src/Lucene.Net.Tests/Index/TestIndexWriterDispose.cs
new file mode 100644
index 000000000..765be22b5
--- /dev/null
+++ b/src/Lucene.Net.Tests/Index/TestIndexWriterDispose.cs
@@ -0,0 +1,218 @@
+using Lucene.Net.Analysis;
+using Lucene.Net.Attributes;
+using Lucene.Net.Documents;
+using Lucene.Net.Store;
+using Lucene.Net.Util;
+using NUnit.Framework;
+using System;
+using Assert = Lucene.Net.TestFramework.Assert;
+
+namespace Lucene.Net.Index
+{
+ /*
+ * 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
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * 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.
+ */
+
+ /// <summary>
+ /// Tests the <see cref="IndexWriter"/> dispose contract for subclasses
+ /// that override <c>Dispose(bool disposing)</c>. See #1399.
+ /// </summary>
+ [LuceneNetSpecific]
+ public class TestIndexWriterDispose : LuceneTestCase
+ {
+ private sealed class DisposeTrackingIndexWriter : IndexWriter
+ {
+ public int DisposingTrueCount { get; private set; }
+ public int DisposingFalseCount { get; private set; }
+ public bool WasOpenBeforeBaseDispose { get; private set; }
+ public bool WasClosedAfterBaseDispose { get; private set; }
+ public bool FinallyBlockRan { get; private set; }
+ public Action BeforeBaseDispose { get; set; }
+
+ public DisposeTrackingIndexWriter(Directory directory, IndexWriterConfig config)
+ : base(directory, config)
+ {
+ }
+
+ public void DisposeFromFinalizer() => Dispose(disposing: false);
+
+ protected override void Dispose(bool disposing)
+ {
+ if (disposing)
+ {
+ DisposingTrueCount++;
+ }
+ else
+ {
+ DisposingFalseCount++;
+ }
+
+ try
+ {
+ if (disposing)
+ {
+ WasOpenBeforeBaseDispose = IsOpen();
+ BeforeBaseDispose?.Invoke();
+ }
+
+ base.Dispose(disposing);
+
+ if (disposing)
+ {
+ WasClosedAfterBaseDispose = !IsOpen();
+ }
+ }
+ finally
+ {
+ FinallyBlockRan = true;
+ }
+ }
+
+ private bool IsOpen()
+ {
+ try
+ {
+ _ = Config;
+ return true;
+ }
+ catch (Exception e) when (e.IsAlreadyClosedException())
+ {
+ return false;
+ }
+ }
+ }
+
+ private DisposeTrackingIndexWriter NewTrackingWriter(Directory dir)
+ {
+ return new DisposeTrackingIndexWriter(dir, NewIndexWriterConfig(TEST_VERSION_CURRENT, new MockAnalyzer(Random)));
+ }
+
+ private static Document NewDoc()
+ {
+ Document doc = new Document();
+ doc.Add(new StringField("id", "1", Field.Store.NO));
+ return doc;
+ }
+
+ private static int CommittedDocCount(Directory dir)
+ {
+ if (!DirectoryReader.IndexExists(dir))
+ {
+ return 0;
+ }
+ using DirectoryReader reader = DirectoryReader.Open(dir);
+ return reader.NumDocs;
+ }
+
+ [Test]
+ public void TestDisposeClosesIndexInsideOverriddenDispose()
+ {
+ using Directory dir = NewDirectory();
+ DisposeTrackingIndexWriter writer = NewTrackingWriter(dir);
+ writer.BeforeBaseDispose = () => writer.AddDocument(NewDoc());
+
+ writer.Dispose();
+
+ Assert.AreEqual(1, writer.DisposingTrueCount);
+ Assert.IsTrue(writer.WasOpenBeforeBaseDispose, "writer should still be open before base.Dispose(true)");
+ Assert.IsTrue(writer.WasClosedAfterBaseDispose, "writer should be closed after base.Dispose(true)");
+ Assert.IsFalse(IndexWriter.IsLocked(dir));
+ Assert.AreEqual(1, CommittedDocCount(dir), "changes made before base.Dispose(true) should be committed");
+ }
+
+ [Test]
+ public void TestDisposeRunsSubclassFinallyWhenCloseThrows()
+ {
+ using Directory dir = NewDirectory();
+ DisposeTrackingIndexWriter writer = NewTrackingWriter(dir);
+ writer.AddDocument(NewDoc());
+ writer.PrepareCommit();
+
+ // Closing with a pending commit throws IllegalStateException and leaves the writer open
+ Assert.Throws<InvalidOperationException>(() => writer.Dispose());
+ Assert.IsTrue(writer.FinallyBlockRan);
+
+ writer.Commit();
+ writer.Dispose();
+ Assert.AreEqual(1, CommittedDocCount(dir));
+ }
+
+ [Test]
+ public void TestCloseDoesNotDisposeAndLaterDisposeIsSafe()
+ {
+ using Directory dir = NewDirectory();
+ DisposeTrackingIndexWriter writer = NewTrackingWriter(dir);
+ writer.AddDocument(NewDoc());
+
+#pragma warning disable 612, 618
+ writer.Close(false);
+#pragma warning restore 612, 618
+
+ Assert.AreEqual(0, writer.DisposingTrueCount, "Close(bool) should not invoke Dispose(bool)");
+ Assert.IsFalse(IndexWriter.IsLocked(dir));
+ Assert.AreEqual(1, CommittedDocCount(dir));
+
+ writer.Dispose();
+
+ Assert.AreEqual(1, writer.DisposingTrueCount);
+ Assert.IsFalse(writer.WasOpenBeforeBaseDispose);
+ Assert.AreEqual(1, CommittedDocCount(dir));
+ }
+
+ [Test]
+ public void TestDisposeAfterRollbackDoesNotCommit()
+ {
+ using Directory dir = NewDirectory();
+ DisposeTrackingIndexWriter writer = NewTrackingWriter(dir);
+ writer.AddDocument(NewDoc());
+
+ writer.Rollback();
+ writer.Dispose();
+
+ Assert.AreEqual(1, writer.DisposingTrueCount);
+ Assert.AreEqual(0, CommittedDocCount(dir));
+ }
+
+ [Test]
+ public void TestDisposeTwiceIsSafe()
+ {
+ using Directory dir = NewDirectory();
+ DisposeTrackingIndexWriter writer = NewTrackingWriter(dir);
+ writer.AddDocument(NewDoc());
+
+ writer.Dispose();
+ writer.Dispose();
+
+ Assert.AreEqual(2, writer.DisposingTrueCount);
+ Assert.AreEqual(1, CommittedDocCount(dir));
+ }
+
+ [Test]
+ public void TestDisposeFromFinalizerDoesNotCloseIndex()
+ {
+ using Directory dir = NewDirectory();
+ DisposeTrackingIndexWriter writer = NewTrackingWriter(dir);
+
+ writer.DisposeFromFinalizer();
+
+ Assert.AreEqual(1, writer.DisposingFalseCount);
+ Assert.IsTrue(IndexWriter.IsLocked(dir), "Dispose(false) should not close the index");
+
+ writer.Dispose();
+ Assert.IsFalse(IndexWriter.IsLocked(dir));
+ }
+ }
+}
diff --git a/src/Lucene.Net.Tests/Index/TestIndexWriterMerging.cs b/src/Lucene.Net.Tests/Index/TestIndexWriterMerging.cs
index 7f81d0435..1055de5d7 100644
--- a/src/Lucene.Net.Tests/Index/TestIndexWriterMerging.cs
+++ b/src/Lucene.Net.Tests/Index/TestIndexWriterMerging.cs
@@ -434,7 +434,7 @@ public virtual void TestNoWaitClose()
t1.Start();
#pragma warning disable 612, 618
- writer.Dispose(false);
+ writer.Close(false);
#pragma warning restore 612, 618
t1.Join();
diff --git a/src/Lucene.Net.Tests/Index/TestIndexWriterOnDiskFull.cs b/src/Lucene.Net.Tests/Index/TestIndexWriterOnDiskFull.cs
index ba7e31635..0ee46a519 100644
--- a/src/Lucene.Net.Tests/Index/TestIndexWriterOnDiskFull.cs
+++ b/src/Lucene.Net.Tests/Index/TestIndexWriterOnDiskFull.cs
@@ -665,7 +665,7 @@ public virtual void TestImmediateDiskFull()
try
{
#pragma warning disable 612, 618
- writer.Dispose(false);
+ writer.Close(false);
#pragma warning restore 612, 618
Assert.Fail("did not hit disk full");
}
@@ -677,7 +677,7 @@ public virtual void TestImmediateDiskFull()
// cleanly close:
dir.MaxSizeInBytes = 0;
#pragma warning disable 612, 618
- writer.Dispose(false);
+ writer.Close(false);
#pragma warning restore 612, 618
dir.Dispose();
}
diff --git a/src/Lucene.Net.Tests/Index/TestIndexWriterReader.cs b/src/Lucene.Net.Tests/Index/TestIndexWriterReader.cs
index e5b689dd4..b61559a20 100644
--- a/src/Lucene.Net.Tests/Index/TestIndexWriterReader.cs
+++ b/src/Lucene.Net.Tests/Index/TestIndexWriterReader.cs
@@ -516,7 +516,7 @@ internal virtual void Close(bool doWait)
mainWriter.WaitForMerges();
}
#pragma warning disable 612, 618
- mainWriter.Dispose(doWait);
+ mainWriter.Close(doWait);
#pragma warning restore 612, 618
}
diff --git a/src/Lucene.Net.Tests/Index/TestIndexWriterWithThreads.cs b/src/Lucene.Net.Tests/Index/TestIndexWriterWithThreads.cs
index a86e3f52d..4824b8928 100644
--- a/src/Lucene.Net.Tests/Index/TestIndexWriterWithThreads.cs
+++ b/src/Lucene.Net.Tests/Index/TestIndexWriterWithThreads.cs
@@ -198,7 +198,7 @@ public virtual void TestImmediateDiskFullWithThreads()
// cleanly close:
dir.MaxSizeInBytes = 0;
#pragma warning disable 612, 618
- writer.Dispose(false);
+ writer.Close(false);
#pragma warning restore 612, 618
dir.Dispose();
}
@@ -270,7 +270,7 @@ public virtual void TestCloseWithThreads()
Console.WriteLine("\nTEST: now close");
}
#pragma warning disable 612, 618
- writer.Dispose(false);
+ writer.Close(false);
#pragma warning restore 612, 618
// Make sure threads that are adding docs are not hung:
@@ -348,7 +348,7 @@ public virtual void TestMultipleThreadsFailure(Failure failure)
try
{
#pragma warning disable 612, 618
- writer.Dispose(false);
+ writer.Close(false);
#pragma warning restore 612, 618
success = true;
}
@@ -356,7 +356,7 @@ public virtual void TestMultipleThreadsFailure(Failure failure)
{
failure.ClearDoFail();
#pragma warning disable 612, 618
- writer.Dispose(false);
+ writer.Close(false);
#pragma warning restore 612, 618
}
if (Verbose)
@@ -417,7 +417,7 @@ public virtual void TestSingleThreadFailure(Failure failure)
failure.ClearDoFail();
writer.AddDocument(doc);
#pragma warning disable 612, 618
- writer.Dispose(false);
+ writer.Close(false);
#pragma warning restore 612, 618
dir.Dispose();
}
diff --git a/src/Lucene.Net/Index/IndexWriter.cs b/src/Lucene.Net/Index/IndexWriter.cs
index 4b7649787..bcdaaa6ec 100644
--- a/src/Lucene.Net/Index/IndexWriter.cs
+++ b/src/Lucene.Net/Index/IndexWriter.cs
@@ -1117,114 +1117,80 @@ private void Shutdown(bool waitForMerges)
[MethodImpl(MethodImplOptions.NoInlining)] // Stack trace needed intact in TestConcurrentMergeScheduler and TestIndexWriterWithThreads
public void Dispose()
{
- Dispose(disposing: true, waitForMerges: true);
+ Dispose(disposing: true);
GC.SuppressFinalize(this);
}
/// <summary>
- /// Disposes the index with or without waiting for currently
+ /// Closes the index with or without waiting for currently
/// running merges to finish. This is only meaningful when
/// using a <see cref="MergeScheduler"/> that runs merges in background
/// threads. See <see cref="Dispose()"/> for details on behavior
/// when exceptions are thrown.
///
+ /// <para><b>NOTE</b>: This closes the index (commits, then releases the write lock),
+ /// but does not dispose this <see cref="IndexWriter"/> instance. You should still call
+ /// <see cref="Dispose()"/> (or use a <c>using</c> block) afterward so that subclasses
+ /// can release their own resources. Calling <see cref="Dispose()"/> after this method
+ /// does not close the index again.</para>
+ ///
/// <para><b>NOTE</b>: It is dangerous to always call
- /// <c>Dispose(false)</c>, especially when <see cref="IndexWriter"/> is not open
+ /// <c>Close(false)</c>, especially when <see cref="IndexWriter"/> is not open
/// for very long, because this can result in "merge
/// starvation" whereby long merges will never have a
/// chance to finish. This will cause too many segments in
/// your index over time, which leads to all sorts of
/// problems like slow searches, too much RAM and too
/// many file descriptors used by readers, etc.</para>
- ///
- /// <para><b>NOTE</b>: This overload should not be called when implementing a finalizer.
- /// Instead, call <see cref="Dispose(bool, bool)"/> with <c>disposing</c> set to
- /// <c>false</c> and <c>waitForMerges</c> set to <c>true</c>.</para>
/// </summary>
/// <param name="waitForMerges"> If <c>true</c>, this call will block
/// until all merges complete; else, it will ask all
/// running merges to abort, wait until those merges have
/// finished (which should be at most a few seconds), and
/// then return. </param>
- // LUCENENET: doc-comment updated and [Obsolete] applied to match upstream LUCENE-5871
- // (commit 2cfcdcc, first released in 4.10.0). Upstream marked this overload @Deprecated
- // because the new Shutdown contract makes "close without waiting for merges" easy to
- // misuse; prefer Commit() followed by Rollback() to abort merges and then close.
- //
- // The deprecation generates CS0618 warnings on existing in-tree callers. This mirrors
- // upstream's posture exactly: branch_4x kept javac [deprecation] warnings at the
- // method's call sites and did not migrate them when 2cfcdcc landed. The intent was
- // always to remove the overload entirely on the next major release: trunk did exactly
- // that in LUCENE-4246 (commit 8559eaf, Lucene 5.0), deleting close(boolean) and
- // migrating all the trunk call sites in the same commit.
+ // LUCENENET: this maps to upstream close(boolean). Upstream (branch_4x) exposed it as a
+ // public method; LUCENE-5871 (commit 2cfcdcc, first released in 4.10.0) marked it
+ // @Deprecated because the shutdown contract makes "close without waiting for merges" easy
+ // to misuse, and trunk LUCENE-4246 (commit 8559eaf, Lucene 5.0) deleted it entirely. We
+ // keep it public but [Obsolete] to mirror the 4.10 state and preserve the 4.8.1 capability.
+ // It also serves the test/benchmark call sites that need the "abort merges on close" path.
[Obsolete("To abort merges and then close, call Commit() and then Rollback() instead.")]
[MethodImpl(MethodImplOptions.NoInlining)] // Stack trace needed intact in TestConcurrentMergeScheduler and TestIndexWriterWithThreads
- [SuppressMessage("CodeQuality", "IDE0079:Remove unnecessary suppression", Justification = "This is a SonarCloud issue")]
- [SuppressMessage("Usage", "CA1816:Dispose methods should call SuppressFinalize", Justification = "This is Lucene's alternate path to Dispose() and we must suppress the finalizer here.")]
- [SuppressMessage("Usage", "S2953:Methods named \"Dispose\" should implement \"IDisposable.Dispose\"", Justification = "This is Lucene's alternate path to Dispose() and we must suppress the finalizer here.")]
- [SuppressMessage("Usage", "S3971:\"GC.SuppressFinalize\" should not be called", Justification = "This is Lucene's alternate path to Dispose() and we must suppress the finalizer here.")]
- public void Dispose(bool waitForMerges)
+ public void Close(bool waitForMerges)
{
- Dispose(disposing: true, waitForMerges);
- GC.SuppressFinalize(this);
+ Shutdown(waitForMerges);
}
/// <summary>
- /// Disposes the index with or without waiting for currently
- /// running merges to finish. This is only meaningful when
- /// using a <see cref="MergeScheduler"/> that runs merges in background
- /// threads.
- ///
- /// <para>This call will block
- /// until all merges complete; else, it will ask all
- /// running merges to abort, wait until those merges have
- /// finished (which should be at most a few seconds), and
- /// then return.
- /// </para>
- ///
- /// <para><b>NOTE</b>: Always be sure to call <c>base.Dispose(disposing, waitForMerges)</c>
- /// when overriding this method.</para>
- ///
- /// <para><b>NOTE</b>: When implementing a finalizer in a subclass, this overload should be called
- /// with <paramref name="disposing"/> set to <c>false</c> and <paramref name="waitForMerges"/>
- /// set to <c>true</c>.</para>
- ///
- /// <para><b>NOTE</b>: If this method hits an <see cref="OutOfMemoryException"/>
- /// you should immediately dispose the writer, again. See
- /// <see cref="IndexWriter"/> for details.</para>
- ///
- /// <para><b>NOTE</b>: It is dangerous to always call
- /// with <paramref name="waitForMerges"/> set to <c>false</c>,
- /// especially when <see cref="IndexWriter"/> is not open
- /// for very long, because this can result in "merge
- /// starvation" whereby long merges will never have a
- /// chance to finish. This will cause too many segments in
- /// your index over time.</para>
+ /// Releases resources used by the <see cref="IndexWriter"/> and optionally
+ /// releases the managed resources.
+ /// <para/>
+ /// When <paramref name="disposing"/> is <c>true</c>, this closes the index (commits,
+ /// waits for merges, and releases the write lock) as described in <see cref="Dispose()"/>.
+ /// If the index was already closed (for example, by <see cref="Rollback()"/> or
+ /// <see cref="Close(bool)"/>), it is not closed again.
+ /// <para/>
+ /// <b>NOTE</b>: Always be sure to call <c>base.Dispose(disposing)</c> when overriding
+ /// this method. Work done before calling the base method happens while the
+ /// <see cref="IndexWriter"/> is still open.
+ /// <para/>
+ /// <b>NOTE</b>: When implementing a finalizer in a subclass, this method should be called
+ /// with <paramref name="disposing"/> set to <c>false</c>, which does not touch the index.
/// </summary>
- /// <param name="waitForMerges"> If <c>true</c>, this call will block
- /// until all merges complete; else, it will ask all
- /// running merges to abort, wait until those merges have
- /// finished (which should be at most a few seconds), and
- /// then return. </param>
/// <param name="disposing"><c>true</c> to release both managed and unmanaged resources;
/// <c>false</c> to release only unmanaged resources. </param>
- // LUCENENET specific - Added this overload to allow subclasses to dispose resources
- // in one place without also having to override Dispose(bool).
+ // LUCENENET specific - restored the conventional Dispose(bool disposing) signature. See #1399.
+ // The waitForMerges flag stays off this inheritance hook: the standard Dispose() path always
+ // waits for merges, and the [Obsolete] Close(bool) passes its flag to Shutdown(bool) directly.
[MethodImpl(MethodImplOptions.NoInlining)] // Stack trace needed intact in TestConcurrentMergeScheduler and TestIndexWriterWithThreads
- protected virtual void Dispose(bool disposing, bool waitForMerges)
+ protected virtual void Dispose(bool disposing)
{
if (disposing)
{
- Close(waitForMerges);
+ Shutdown(waitForMerges: true);
}
}
- [MethodImpl(MethodImplOptions.AggressiveInlining)] // LUCENENET NOTE: this will interfere with stack trace inspection in tests, but that case should be covered by Dispose above which is NoInlining
- internal void Close(bool waitForMerges) // LUCENENET: made internal for test purposes
- {
- Shutdown(waitForMerges);
- }
-
// LUCENENET: upstream 2cfcdcc had private assertEventQueueAfterClose() between
// close(boolean) and shouldClose() (and closeInternal(boolean, boolean) immediately
// after shouldClose()). The LUCENE-5871 rewrite of close(boolean) stranded both as
@@ -2094,7 +2060,7 @@ internal string NewSegmentName()
/// you should immediately dispose the writer. See
/// <see cref="IndexWriter"/> for details.</para>
///
- /// <para><b>NOTE</b>: if you call <see cref="Dispose(bool)"/>
+ /// <para><b>NOTE</b>: if you call <see cref="Close(bool)"/>
/// with <c>false</c>, which aborts all running merges,
/// then any thread still running this method might hit a
/// <see cref="MergePolicy.MergeAbortedException"/>.</para>
@@ -2276,7 +2242,7 @@ private bool MaxNumSegmentsMergesPending()
/// you should immediately dispose the writer. See
/// <see cref="IndexWriter"/> for details.</para>
///
- /// <para><b>NOTE</b>: if you call <see cref="Dispose(bool)"/>
+ /// <para><b>NOTE</b>: if you call <see cref="Close(bool)"/>
/// with <c>false</c>, which aborts all running merges,
/// then any thread still running this method might hit a
/// <see cref="MergePolicy.MergeAbortedException"/>.</para>
@@ -3335,7 +3301,7 @@ public virtual void AddIndexes(params Directory[] dirs)
/// <see cref="DirectoryReader.Open(Directory, int)"/>.
///
/// <para/>
- /// <b>NOTE</b>: if you call <see cref="Dispose(bool)"/> with <c>false</c>, which
+ /// <b>NOTE</b>: if you call <see cref="Close(bool)"/> with <c>false</c>, which
/// aborts all running merges, then any thread still running this method might
/// hit a <see cref="MergePolicy.MergeAbortedException"/>.
/// </summary>
diff --git a/src/Lucene.Net/Index/MergePolicy.cs b/src/Lucene.Net/Index/MergePolicy.cs
index a552f647a..86c75cee1 100644
--- a/src/Lucene.Net/Index/MergePolicy.cs
+++ b/src/Lucene.Net/Index/MergePolicy.cs
@@ -546,7 +546,7 @@ protected MergeException(SerializationInfo info, StreamingContext context)
/// <summary>
/// Thrown when a merge was explicitly aborted because
- /// <see cref="IndexWriter.Dispose(bool)"/> was called with
+ /// <see cref="IndexWriter.Close(bool)"/> was called with
/// <c>false</c>. Normally this exception is
/// privately caught and suppressed by <see cref="IndexWriter"/>.
/// </summary>
diff --git a/src/Lucene.Net/Index/MergeState.cs b/src/Lucene.Net/Index/MergeState.cs
index 0d738d081..14da40ea3 100644
--- a/src/Lucene.Net/Index/MergeState.cs
+++ b/src/Lucene.Net/Index/MergeState.cs
@@ -245,7 +245,7 @@ public virtual void Work(double units)
}
/// <summary>
- /// If you use this: IW.Dispose(false) cannot abort your merge!
+ /// If you use this: IW.Close(false) cannot abort your merge!
/// <para/>
/// @lucene.internal
/// </summary>