Skip to content

Commit

Permalink
fix "Exception saving profile preferences" #499
Browse files Browse the repository at this point in the history
synchronize static + retry Files.move due to JDK bug

#499
  • Loading branch information
EcljpseB0T authored and iloveeclipse committed Feb 10, 2024
1 parent a61555a commit 4b4c36b
Showing 1 changed file with 10 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,8 @@

import java.io.*;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.*;
import java.nio.file.Path;
import java.nio.file.StandardCopyOption;
import java.util.*;
import java.util.concurrent.ConcurrentHashMap;
import org.eclipse.core.internal.runtime.RuntimeLog;
Expand Down Expand Up @@ -271,7 +270,7 @@ protected static void convertFromProperties(EclipsePreferences node, Properties
}
}

private final Object writeLock = new Object();
private static final Object WRITE_LOCK = new Object();

/*
* Helper method to persist a Properties object to the filesystem. We use this
Expand All @@ -287,13 +286,19 @@ private void write(Properties props, IPath location) throws BackingStoreExceptio
try {
Files.createDirectories(parentFile);
String fileContent = removeTimestampFromTable(props);
synchronized (writeLock) {
synchronized (WRITE_LOCK) {
if (Files.exists(preferenceFile)) {
// Write new file content to a temporary file first to not loose the old content
// in case of a failure. If everything goes OK, it is moved to the right place.
Path tmp = preferenceFile.resolveSibling(preferenceFile.getFileName() + BACKUP_FILE_EXTENSION);
Files.writeString(tmp, fileContent, StandardCharsets.UTF_8);
Files.move(tmp, preferenceFile, StandardCopyOption.REPLACE_EXISTING);
try {
Files.move(tmp, preferenceFile, StandardCopyOption.REPLACE_EXISTING);
} catch (NoSuchFileException e) {
// workaround for JDK-8325302 throws Exception if file is deleted in parallel.
// retry:
Files.move(tmp, preferenceFile, StandardCopyOption.REPLACE_EXISTING);
}
} else {
Files.writeString(preferenceFile, fileContent, StandardCharsets.UTF_8);
}
Expand Down

0 comments on commit 4b4c36b

Please sign in to comment.