Skip to content

Commit

Permalink
fix: fixed file leak in listing directory ServicesMain (cherry-pick) (#…
Browse files Browse the repository at this point in the history
…16825) (#16997)

Signed-off-by: Alex Kuzmin <[email protected]>
  • Loading branch information
alex-kuzmin-hg authored Dec 10, 2024
1 parent e1cf4d9 commit 852cc14
Showing 1 changed file with 37 additions and 31 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
import java.util.ArrayList;
import java.util.List;
import java.util.TreeMap;
import java.util.stream.Stream;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

Expand Down Expand Up @@ -194,43 +195,48 @@ public List<SavedStateInfo> getSavedStateFiles(
return List.of();
}

final List<Path> dirs = Files.list(dir).filter(Files::isDirectory).toList();
try (final Stream<Path> list = Files.list(dir)) {

final TreeMap<Long, SavedStateInfo> savedStates = new TreeMap<>();
for (final Path subDir : dirs) {
try {
final long round = Long.parseLong(subDir.getFileName().toString());
final Path stateFile = subDir.resolve(SIGNED_STATE_FILE_NAME);
if (!exists(stateFile)) {
logger.warn(
EXCEPTION.getMarker(),
"Saved state file ({}) not found, but directory exists '{}'",
stateFile.getFileName(),
subDir.toAbsolutePath());
continue;
}
final List<Path> dirs = list.filter(Files::isDirectory).toList();

final Path metdataPath = subDir.resolve(SavedStateMetadata.FILE_NAME);
final SavedStateMetadata metadata;
final TreeMap<Long, SavedStateInfo> savedStates = new TreeMap<>();
for (final Path subDir : dirs) {
try {
metadata = SavedStateMetadata.parse(metdataPath);
} catch (final IOException e) {
logger.error(
EXCEPTION.getMarker(), "Unable to read saved state metadata file '{}'", metdataPath);
continue;
final long round = Long.parseLong(subDir.getFileName().toString());
final Path stateFile = subDir.resolve(SIGNED_STATE_FILE_NAME);
if (!exists(stateFile)) {
logger.warn(
EXCEPTION.getMarker(),
"Saved state file ({}) not found, but directory exists '{}'",
stateFile.getFileName(),
subDir.toAbsolutePath());
continue;
}

final Path metdataPath = subDir.resolve(SavedStateMetadata.FILE_NAME);
final SavedStateMetadata metadata;
try {
metadata = SavedStateMetadata.parse(metdataPath);
} catch (final IOException e) {
logger.error(
EXCEPTION.getMarker(),
"Unable to read saved state metadata file '{}'",
metdataPath);
continue;
}

savedStates.put(round, new SavedStateInfo(stateFile, metadata));

} catch (final NumberFormatException e) {
logger.warn(
EXCEPTION.getMarker(),
"Unexpected directory '{}' in '{}'",
subDir.getFileName(),
dir.toAbsolutePath());
}

savedStates.put(round, new SavedStateInfo(stateFile, metadata));

} catch (final NumberFormatException e) {
logger.warn(
EXCEPTION.getMarker(),
"Unexpected directory '{}' in '{}'",
subDir.getFileName(),
dir.toAbsolutePath());
}
return new ArrayList<>(savedStates.descendingMap().values());
}
return new ArrayList<>(savedStates.descendingMap().values());
} catch (final IOException e) {
throw new UncheckedIOException(e);
}
Expand Down

0 comments on commit 852cc14

Please sign in to comment.