Skip to content

Commit

Permalink
Avoid racing calls to close MarkFile.
Browse files Browse the repository at this point in the history
  • Loading branch information
mjpt777 committed May 31, 2022
1 parent c5f9ead commit 8ecd5dd
Showing 1 changed file with 5 additions and 9 deletions.
14 changes: 5 additions & 9 deletions agrona/src/main/java/org/agrona/MarkFile.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import java.io.IOException;
import java.nio.MappedByteBuffer;
import java.nio.channels.FileChannel;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.function.Consumer;
import java.util.function.IntConsumer;

Expand All @@ -46,7 +47,7 @@ public class MarkFile implements AutoCloseable
private final MappedByteBuffer mappedBuffer;
private final UnsafeBuffer buffer;

private volatile boolean isClosed = false;
private final AtomicBoolean isClosed = new AtomicBoolean();

/**
* Create a directory and mark file if none present. Checking if an active Mark file exists and is active.
Expand Down Expand Up @@ -230,22 +231,17 @@ public MarkFile(final UnsafeBuffer buffer, final int versionFieldOffset, final i
*/
public boolean isClosed()
{
return isClosed;
return isClosed.get();
}

/**
* {@inheritDoc}
*/
public void close()
{
if (!isClosed)
if (isClosed.compareAndSet(false, true))
{
if (null != mappedBuffer)
{
BufferUtil.free(mappedBuffer);
}

isClosed = true;
BufferUtil.free(mappedBuffer);
}
}

Expand Down

0 comments on commit 8ecd5dd

Please sign in to comment.