Skip to content

Commit

Permalink
Improve crash consistency
Browse files Browse the repository at this point in the history
  • Loading branch information
basil committed Jan 1, 2024
1 parent 2e26745 commit b9e666b
Showing 1 changed file with 18 additions and 0 deletions.
18 changes: 18 additions & 0 deletions core/src/main/java/hudson/util/FileChannelWriter.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package hudson.util;

import hudson.Functions;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.Writer;
Expand All @@ -11,6 +12,7 @@
import java.nio.file.OpenOption;
import java.nio.file.Path;
import java.util.logging.Logger;
import jenkins.util.SystemProperties;
import org.kohsuke.accmod.Restricted;
import org.kohsuke.accmod.restrictions.NoExternalUse;

Expand All @@ -29,11 +31,16 @@
@Restricted(NoExternalUse.class)
public class FileChannelWriter extends Writer implements Channel {

private static final boolean REQUIRES_DIR_FSYNC =
SystemProperties.getBoolean(FileChannelWriter.class.getName() + ".requiresDirFsync", true);

private static final Logger LOGGER = Logger.getLogger(FileChannelWriter.class.getName());

private final Charset charset;
private final FileChannel channel;

private final Path parent;

/**
* {@link FileChannel#force(boolean)} is a <strong>very</strong> costly operation. This flag has been introduced mostly to
* accommodate Jenkins' previous behaviour, when using a simple {@link java.io.BufferedWriter}.
Expand Down Expand Up @@ -64,6 +71,7 @@ public class FileChannelWriter extends Writer implements Channel {
this.forceOnFlush = forceOnFlush;
this.forceOnClose = forceOnClose;
channel = FileChannel.open(filePath, options);
parent = filePath.getParent();
}

@Override
Expand All @@ -78,6 +86,11 @@ public void flush() throws IOException {
if (forceOnFlush) {
LOGGER.finest("Flush is forced");
channel.force(true);
if (REQUIRES_DIR_FSYNC && !Functions.isWindows()) {

Check warning on line 89 in core/src/main/java/hudson/util/FileChannelWriter.java

View check run for this annotation

ci.jenkins.io / Code Coverage

Partially covered line

Line 89 is only partially covered, 2 branches are missing
try (FileChannel parentChannel = FileChannel.open(parent)) {
parentChannel.force(true);
}
}
} else {
LOGGER.finest("Force disabled on flush(), no-op");
}
Expand All @@ -93,6 +106,11 @@ public void close() throws IOException {
if (channel.isOpen()) {
if (forceOnClose) {
channel.force(true);
if (REQUIRES_DIR_FSYNC && !Functions.isWindows()) {

Check warning on line 109 in core/src/main/java/hudson/util/FileChannelWriter.java

View check run for this annotation

ci.jenkins.io / Code Coverage

Partially covered line

Line 109 is only partially covered, 2 branches are missing
try (FileChannel parentChannel = FileChannel.open(parent)) {
parentChannel.force(true);
}
}
}
channel.close();
}
Expand Down

0 comments on commit b9e666b

Please sign in to comment.