jenkinsci / lib-file-leak-detector

Java agent that detects file handle leak
http://file-leak-detector.kohsuke.org/
MIT License
241 stars 112 forks source link

Missing open/close from Files.lines #20

Closed z669016 closed 2 years ago

z669016 commented 9 years ago

Hi, I tried to figure out if my Files.lines("some-path") stream is closed correctly. I used the agent but if I use the trace option, I do see messages from class files being opened and closed, but the text file ("some-path") is not in the list. Are streams handled differently, or does the agent just doesn't see the calls for the open/close?

public static void main(String[] args) {
    Stream<Entry> entries = getEntries();
    entries.forEach(System.out::println);
    entries.close();
}

public static Stream<Entry> getEntries() {
    Path path = Paths.get("./src/test/resources", "entries.txt");
    try {
        Stream<String> lines = Files.lines(path);
        return lines.map(line -> asEntry(line));
    } catch (IOException exc) {
        System.out.println(exc);
    }

    return Stream.empty();
}

Regards, René

kohsuke commented 9 years ago

Looks like the FileDescriptor sun.nio.fs.UnixChannelFactory.open(...) needs to be intercepted.

binma1 commented 8 years ago

@z669016 I've had a similar problem that calling Files.lines too many times will cause a "too many opened files" exception. Perhaps there is indeed a file leak in Java's Files.lines()?

centic9 commented 8 years ago

@binma1 , you have to close the stream to free the file handle.

This issue is about adding support for finding such invalid uses via file-leak-detector.

binma1 commented 8 years ago

@centic9 Thanks!