Skip to content

Commit

Permalink
[Java] Prevent segfaults through mark file API after close.
Browse files Browse the repository at this point in the history
  • Loading branch information
vyazelenko committed Jan 17, 2025
1 parent 00d7969 commit bd230f7
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 16 deletions.
19 changes: 15 additions & 4 deletions aeron-archive/src/main/java/io/aeron/archive/ArchiveMarkFile.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
*/
package io.aeron.archive;

import io.aeron.Aeron;
import io.aeron.CommonContext;
import io.aeron.archive.client.ArchiveException;
import io.aeron.archive.codecs.mark.MarkFileHeaderDecoder;
Expand Down Expand Up @@ -278,7 +279,14 @@ public ArchiveMarkFile(
*/
public void close()
{
CloseHelper.close(markFile);
if (!markFile.isClosed())
{
CloseHelper.close(markFile);
final UnsafeBuffer emptyBuffer = new UnsafeBuffer();
headerEncoder.wrap(emptyBuffer, 0);
headerDecoder.wrap(emptyBuffer, 0, 0, 0);
errorBuffer.wrap(emptyBuffer, 0, 0);
}
}

/**
Expand All @@ -289,15 +297,18 @@ public void close()
*/
public long archiveId()
{
return headerDecoder.archiveId();
return markFile.isClosed() ? Aeron.NULL_VALUE : headerDecoder.archiveId();
}

/**
* Signal the archive has concluded successfully and ready to start.
*/
public void signalReady()
{
markFile.signalReady(SEMANTIC_VERSION);
if (!markFile.isClosed())
{
markFile.signalReady(SEMANTIC_VERSION);
}
}

/**
Expand All @@ -320,7 +331,7 @@ public void updateActivityTimestamp(final long nowMs)
*/
public long activityTimestampVolatile()
{
return markFile.timestampVolatile();
return markFile.isClosed() ? Aeron.NULL_VALUE : markFile.timestampVolatile();
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,6 @@
import java.nio.file.attribute.BasicFileAttributes;
import java.util.function.Consumer;

import static io.aeron.Aeron.NULL_VALUE;

/**
* Used to indicate if a cluster service is running and what configuration it is using. Errors encountered by
* the service are recorded within this file by a {@link org.agrona.concurrent.errors.DistinctErrorLog}.
Expand Down Expand Up @@ -233,7 +231,7 @@ else if (SemanticVersion.major(version) != MAJOR_VERSION)
null,
null);
buffer = markFile.buffer();
candidateTermId = NULL_VALUE;
candidateTermId = Aeron.NULL_VALUE;
}

headerOffset = HEADER_OFFSET;
Expand Down Expand Up @@ -332,7 +330,14 @@ public static boolean isConsensusModuleMarkFile(final Path path, final BasicFile
*/
public void close()
{
CloseHelper.close(markFile);
if (!markFile.isClosed())
{
CloseHelper.close(markFile);
final UnsafeBuffer emptyBuffer = new UnsafeBuffer();
headerEncoder.wrap(emptyBuffer, 0);
headerDecoder.wrap(emptyBuffer, 0, 0, 0);
errorBuffer.wrap(emptyBuffer, 0, 0);
}
}

/**
Expand All @@ -353,7 +358,8 @@ public boolean isClosed()
*/
public long candidateTermId()
{
return buffer.getLongVolatile(headerOffset + MarkFileHeaderDecoder.candidateTermIdEncodingOffset());
return markFile.isClosed() ? Aeron.NULL_VALUE :
buffer.getLongVolatile(headerOffset + MarkFileHeaderDecoder.candidateTermIdEncodingOffset());
}

/**
Expand All @@ -363,7 +369,7 @@ public long candidateTermId()
*/
public int memberId()
{
return headerDecoder.memberId();
return markFile.isClosed() ? Aeron.NULL_VALUE : headerDecoder.memberId();
}

/**
Expand All @@ -373,7 +379,10 @@ public int memberId()
*/
public void memberId(final int memberId)
{
headerEncoder.memberId(memberId);
if (!markFile.isClosed())
{
headerEncoder.memberId(memberId);
}
}

/**
Expand All @@ -383,7 +392,7 @@ public void memberId(final int memberId)
*/
public int clusterId()
{
return headerDecoder.clusterId();
return markFile.isClosed() ? Aeron.NULL_VALUE : headerDecoder.clusterId();
}

/**
Expand All @@ -393,23 +402,32 @@ public int clusterId()
*/
public void clusterId(final int clusterId)
{
headerEncoder.clusterId(clusterId);
if (!markFile.isClosed())
{
headerEncoder.clusterId(clusterId);
}
}

/**
* Signal the cluster component has concluded successfully and ready to start.
*/
public void signalReady()
{
markFile.signalReady(SEMANTIC_VERSION);
if (!markFile.isClosed())
{
markFile.signalReady(SEMANTIC_VERSION);
}
}

/**
* Signal the cluster component has failed to conclude and cannot start.
*/
public void signalFailedStart()
{
markFile.signalReady(VERSION_FAILED);
if (!markFile.isClosed())
{
markFile.signalReady(VERSION_FAILED);
}
}

/**
Expand All @@ -432,7 +450,7 @@ public void updateActivityTimestamp(final long nowMs)
*/
public long activityTimestampVolatile()
{
return markFile.timestampVolatile();
return markFile.isClosed() ? Aeron.NULL_VALUE : markFile.timestampVolatile();
}

/**
Expand Down

0 comments on commit bd230f7

Please sign in to comment.