Skip to content

Commit

Permalink
Lucene.Net.Store.RateLimitedIndexOutput: Allow double-dispose calls a…
Browse files Browse the repository at this point in the history
…nd guard against usage after Dispose(). See apache#265.
  • Loading branch information
NightOwl888 committed May 14, 2023
1 parent 0321896 commit a846ed9
Showing 1 changed file with 24 additions and 2 deletions.
26 changes: 24 additions & 2 deletions src/Lucene.Net/Store/RateLimitedIndexOutput.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System;
using System;
using System.Runtime.CompilerServices;
using System.Threading;

namespace Lucene.Net.Store
{
Expand Down Expand Up @@ -30,6 +31,7 @@ internal sealed class RateLimitedIndexOutput : BufferedIndexOutput
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)
{
Expand Down Expand Up @@ -62,15 +64,21 @@ protected internal override void FlushBuffer(byte[] b, int offset, int len)

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);
}
Expand All @@ -90,6 +98,8 @@ public override void Flush()

protected override void Dispose(bool disposing)
{
if (0 != Interlocked.CompareExchange(ref this.disposed, 1, 0)) return; // LUCENENET specific - allow double-dispose

if (disposing)
{
try
Expand All @@ -102,5 +112,17 @@ protected override void Dispose(bool disposing)
}
}
}

// 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.");
}
}
}
}

0 comments on commit a846ed9

Please sign in to comment.