You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@lucenenet.apache.org by ni...@apache.org on 2021/10/11 10:43:29 UTC

[lucenenet] branch master updated: Lucene.Net.Index.IndexWriter: Removed call to ThreadJob.Interrupted() which internally calls Thread.Sleep(0) which was causing performance issues and deadlocks during IndexWriter.Dispose()

This is an automated email from the ASF dual-hosted git repository.

nightowl888 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 d7a68e4  Lucene.Net.Index.IndexWriter: Removed call to ThreadJob.Interrupted() which internally calls Thread.Sleep(0) which was causing performance issues and deadlocks during IndexWriter.Dispose()
d7a68e4 is described below

commit d7a68e4ef67a34f365c2ebc58563102ff8af1d25
Author: Shad Storhaug <sh...@shadstorhaug.com>
AuthorDate: Sat Oct 9 09:31:53 2021 +0700

    Lucene.Net.Index.IndexWriter: Removed call to ThreadJob.Interrupted() which internally calls Thread.Sleep(0) which was causing performance issues and deadlocks during IndexWriter.Dispose()
---
 src/Lucene.Net.Tests/Index/TestIndexWriter.cs | 25 +++++++++++++++++--------
 src/Lucene.Net/Index/IndexWriter.cs           |  8 +++++++-
 2 files changed, 24 insertions(+), 9 deletions(-)

diff --git a/src/Lucene.Net.Tests/Index/TestIndexWriter.cs b/src/Lucene.Net.Tests/Index/TestIndexWriter.cs
index 3057003..22f2baa 100644
--- a/src/Lucene.Net.Tests/Index/TestIndexWriter.cs
+++ b/src/Lucene.Net.Tests/Index/TestIndexWriter.cs
@@ -1365,21 +1365,30 @@ namespace Lucene.Net.Index
                     {
                         Console.WriteLine("TEST: now rollback");
                     }
+
+                    // LUCENENET specific - .NET has no way to "clear" the "interrupted status", so we
+                    // simply catch and ignore the ThreadInterruptedException on a call to Thread.Sleep(0).
+                    // This would cause undesired side effects if there were competing threads, but since
+                    // this is a standalone cleanup block in a single thread, we can get away with it here.
+                    // Thread.Sleep(0) should never be used in production code to read the "interrupted status",
+                    // always catch ThreadInterruptedException and ignore it instead.
+
                     // clear interrupt state:
-                    ThreadJob.Interrupted();
+                    try
+                    {
+                        Thread.Sleep(0);
+                    }
+                    catch (ThreadInterruptedException)
+                    {
+                        // ignore
+                    }
+
                     if (w != null)
                     {
                         try
                         {
                             w.Rollback();
                         }
-                        // LUCENENET specific - there is a chance that our thread will be
-                        // interrupted here, so we need to catch and ignore that exception
-                        // when our MockDirectoryWrapper throws it.
-                        catch (Exception e) when (e.IsInterruptedException())
-                        {
-                            // ignore
-                        }
                         catch (Exception ioe) when (ioe.IsIOException())
                         {
                             throw RuntimeException.Create(ioe);
diff --git a/src/Lucene.Net/Index/IndexWriter.cs b/src/Lucene.Net/Index/IndexWriter.cs
index a9a822e..5daf4ba 100644
--- a/src/Lucene.Net/Index/IndexWriter.cs
+++ b/src/Lucene.Net/Index/IndexWriter.cs
@@ -1161,8 +1161,14 @@ namespace Lucene.Net.Index
                 {
                     try
                     {
+                        // LUCENENET specific - Java calls Thread.interrupted(), which resets and returns the
+                        // initial "interrupted status". .NET has no such method. However, following the logic
+                        // carefully below, we call Thread.CurrentThread.Interrupted() if interrupted is true.
+                        // If the current thread is already in "interrupted status", there is no reason to call
+                        // Thread.CurrentThread.Interrupted() since it is already in that state.
+
                         // clean up merge scheduler in all cases, although flushing may have failed:
-                        interrupted = ThreadJob.Interrupted();
+                        //interrupted = ThreadJob.Interrupted();
 
                         if (waitForMerges)
                         {