Skip to content

Fix file descriptor leak in FileIO instrumentation - #2248

Merged
adinauer merged 5 commits into
getsentry:mainfrom
ntoskrnl:bugfix/fix-file-descriptor-leak
Sep 19, 2022
Merged

Fix file descriptor leak in FileIO instrumentation#2248
adinauer merged 5 commits into
getsentry:mainfrom
ntoskrnl:bugfix/fix-file-descriptor-leak

Conversation

@ntoskrnl

@ntoskrnl ntoskrnl commented Sep 17, 2022

Copy link
Copy Markdown
Contributor

📜 Description

There is a bug in FileIO tracing that causes leak in file descriptors.
The bug affects Android and Java.

💡 Motivation and Context

The context and all the information is here.

When we create new SentryFileInputStream(file) or new SentryFileOutputStream(file), 2 file descriptors are opened. But when we close the stream, only one is closed. The cause is in this comment.

💚 How did you test it?

Run the code below in the sample app. Before fix I see lsof still shows 10 open files even though they are closed. After fix - nothing shows up.

// write into several files with SentryFileOutputStream, close and delete them.
ExecutorService executor = Executors.newSingleThreadExecutor();
for (int i = 0; i < 10; i++) {
  final String name = "my-test-file-" + i + ".bin";
  executor.submit(
    () -> {
      File file = new File(name);
      try (FileOutputStream out = new SentryFileOutputStream(file, false)) {
        out.write("Hello world!".getBytes(StandardCharsets.UTF_8));
        out.flush();
        Thread.sleep(50);
      } catch (Exception error) {
        // ignore
      }
      file.delete();
    }
  );
}

// run "lsof" and check if they are still open
executor.submit(
  () -> {
    try {
      StringBuilder output = new StringBuilder();
      Process process = Runtime.getRuntime().exec("lsof");
      BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream()));
      while (true) {
        String line = reader.readLine();
        if (line == null) break;
        if (line.contains("my-test-file-")) {
          output.append(line);
          output.append("\n");
        }
      }
      System.out.println("> slof \n" + output);
      process.destroy();
    } catch (IOException e) {
      // ignore
    }
  }
);

executor.shutdown();

📝 Checklist

  • I reviewed the submitted code
  • I added tests to verify the changes
  • I updated the docs if needed
  • No breaking changes

@adinauer adinauer left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM. Thank you very much for fixing this bug!

Comment thread sentry/src/main/java/io/sentry/instrumentation/file/SentryFileInputStream.java Outdated
Comment thread sentry/src/main/java/io/sentry/instrumentation/file/SentryFileOutputStream.java Outdated
adinauer and others added 2 commits September 19, 2022 09:29
…InputStream.java

Co-authored-by: Manoel Aranda Neto <5731772+marandaneto@users.noreply.github.com>
…OutputStream.java

Co-authored-by: Manoel Aranda Neto <5731772+marandaneto@users.noreply.github.com>
@codecov-commenter

codecov-commenter commented Sep 19, 2022

Copy link
Copy Markdown

Codecov Report

Base: 80.64% // Head: 80.62% // Decreases project coverage by -0.02% ⚠️

Coverage data is based on head (5986588) compared to base (429f878).
Patch coverage: 50.00% of modified lines in pull request are covered.

❗ Current head 5986588 differs from pull request most recent head d1a390b. Consider uploading reports for the commit d1a390b to get more accurate results

Additional details and impacted files
@@             Coverage Diff              @@
##               main    #2248      +/-   ##
============================================
- Coverage     80.64%   80.62%   -0.03%     
- Complexity     3366     3368       +2     
============================================
  Files           240      240              
  Lines         12382    12388       +6     
  Branches       1646     1646              
============================================
+ Hits           9986     9988       +2     
- Misses         1787     1791       +4     
  Partials        609      609              
Impacted Files Coverage Δ
...ry/instrumentation/file/SentryFileInputStream.java 65.95% <50.00%> (-2.23%) ⬇️
...y/instrumentation/file/SentryFileOutputStream.java 45.45% <50.00%> (-0.70%) ⬇️

Help us with your feedback. Take ten seconds to tell us how you rate us. Have a feature suggestion? Share it here.

☔ View full report at Codecov.
📢 Do you have feedback about the report comment? Let us know in this issue.

@adinauer
adinauer merged commit 9cd49cd into getsentry:main Sep 19, 2022
@ntoskrnl
ntoskrnl deleted the bugfix/fix-file-descriptor-leak branch September 19, 2022 15:24
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants