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 2023/05/16 21:03:11 UTC

[lucenenet] 05/07: Lucene.Net.Store.RateLimitedIndexOutput: Allow double-dispose calls and guard against usage after Dispose(). See #265.

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

commit a9c8388aa9fc696ec14f1aa88daa0512f804be32
Author: Shad Storhaug <sh...@shadstorhaug.com>
AuthorDate: Sun May 14 23:02:52 2023 +0700

    Lucene.Net.Store.RateLimitedIndexOutput: Allow double-dispose calls and guard against usage after Dispose(). See #265.
---
 src/Lucene.Net/Store/RateLimitedIndexOutput.cs | 26 ++++++++++++++++++++++++--
 1 file changed, 24 insertions(+), 2 deletions(-)

diff --git a/src/Lucene.Net/Store/RateLimitedIndexOutput.cs b/src/Lucene.Net/Store/RateLimitedIndexOutput.cs
index 92ea575b6..fe0a147ae 100644
--- a/src/Lucene.Net/Store/RateLimitedIndexOutput.cs
+++ b/src/Lucene.Net/Store/RateLimitedIndexOutput.cs
@@ -1,5 +1,6 @@
-using System;
+using System;
 using System.Runtime.CompilerServices;
+using System.Threading;
 
 namespace Lucene.Net.Store
 {
@@ -30,6 +31,7 @@ namespace Lucene.Net.Store
         private readonly IndexOutput @delegate;
         private readonly BufferedIndexOutput bufferedDelegate;
         private readonly RateLimiter rateLimiter;
+        private int disposed = 0; // LUCENENET specific - allow double-dispose
 
         internal RateLimitedIndexOutput(RateLimiter rateLimiter, IndexOutput @delegate)
         {
@@ -62,15 +64,21 @@ namespace Lucene.Net.Store
 
         public override long Length
         {
-            get => @delegate.Length;
+            get
+            {
+                EnsureOpen(); // LUCENENET specific - ensure we can't be abused after dispose
+                return @delegate.Length;
+            }
             set
             {
+                // LUCENENET: Intentionally blank
             }
         }
 
         [Obsolete("(4.1) this method will be removed in Lucene 5.0")]
         public override void Seek(long pos)
         {
+            EnsureOpen(); // LUCENENET specific - ensure we can't be abused after dispose
             Flush();
             @delegate.Seek(pos);
         }
@@ -90,6 +98,8 @@ namespace Lucene.Net.Store
 
         protected override void Dispose(bool disposing)
         {
+            if (0 != Interlocked.CompareExchange(ref this.disposed, 1, 0)) return; // LUCENENET specific - allow double-dispose
+
             if (disposing)
             {
                 try
@@ -102,5 +112,17 @@ namespace Lucene.Net.Store
                 }
             }
         }
+
+        // LUCENENET specific - ensure we can't be abused after dispose
+        private bool IsOpen => Interlocked.CompareExchange(ref this.disposed, 0, 0) == 0 ? true : false;
+
+        // LUCENENET specific - ensure we can't be abused after dispose
+        private void EnsureOpen()
+        {
+            if (!IsOpen)
+            {
+                throw AlreadyClosedException.Create(this.GetType().FullName, "this IndexOutput is disposed.");
+            }
+        }
     }
 }
\ No newline at end of file