Skip to content

File Handle Leak in RecursiveDirectoryScanner #2478

Closed
@Koizumi85

Description

@Koizumi85

Hi everyone,

I think there is a file handle leak in the RecursiveDirectoryScanner class in the current version (5.0.5.RELEASE).
I experienced Exceptions with "too many open files" in the RecursiveDirectoryScanner and followed it down to the following code in the listFiles(File) method:

try {
			Stream<File> fileStream = Files.walk(directory.toPath(), this.maxDepth, this.fileVisitOptions)
					.skip(1)
					.map(Path::toFile)
					.filter(file -> !supportAcceptFilter
							|| ((AbstractFileListFilter<File>) filter).accept(file));

	if (supportAcceptFilter) {
		return fileStream.collect(Collectors.toList());
	}
	else {
		return filter.filterFiles(fileStream.toArray(File[]::new));
	}
} catch (IOException e) {
	throw new IllegalArgumentException(e);
}

According to this Stackoverflow thread and my own experiences you have to close the Stream returned by Files.walk.

So the code should be changed to something like this in my opinion:

try (Stream<File> fileStream = Files.walk(directory.toPath(), this.maxDepth, this.fileVisitOptions).skip(1)
				.map(Path::toFile)
				.filter(file -> !supportAcceptFilter || ((AbstractFileListFilter<File>) filter).accept(file));) {

	if (supportAcceptFilter) {
		return fileStream.collect(Collectors.toList());
	}
	return filter.filterFiles(fileStream.toArray(File[]::new));
} catch (IOException e) {
	throw new IllegalArgumentException(e);
}

I created my own RecursiveDirectoryScanner with this fix and unless now I couldn't reproduce the Exceptions again.

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions