GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — Formatter leaks threads and memory · Issue #846 · google/google-java-format

Formatter leaks threads and memory #846

New issue
Copy link
New issue
Copy link
Closed
#890
Closed
Formatter leaks threads and memory#846
#890
Copy link

Description

@stiemannkj1
stiemannkj1
opened on Oct 15, 2022
Issue body actions

The formatter fails to clean up the threads it creates which leaks the threads themselves (if the formatter is run multiple times) and the memory for the threads.

This also can prevent the JVM to from shutting down because the threads are non-daemon threads.

The reason this leak doesn't usually affect users is probably because users run the formatter via the CLI which calls System#exit to kill the JVM process.

Steps to Reproduce:

  1. Add this test to MainTest.java:

    private static Path createTestSourceFile(TemporaryFolder testFolder, String className)
        throws IOException {
      Path path = testFolder.newFile(className + ".java").toPath();
      Files.write(path, ("class " + className + " {}\n").getBytes(UTF_8));
      try {
        Files.setPosixFilePermissions(path, EnumSet.of(PosixFilePermission.OWNER_READ));
      } catch (UnsupportedOperationException e) {
        throw new IOException(e);
      }
      return path;
    }
    
    @Test
    public void doesNotLeakThreads() throws Exception {
      Path testA = createTestSourceFile(testFolder, "TestA");
      Path testB = createTestSourceFile(testFolder, "TestB");
    
      // Get the number of threads before formatting.
      int numberOfThreadsBeforeFormatting = Thread.currentThread().getThreadGroup().activeCount();
    
      // Run the formatter several times.
      for (int i = 0; i < 10000; i++) {
        final var errorCode =
            new GoogleJavaFormatToolProvider()
                .run(
                    new PrintWriter(
                        new BufferedWriter(new OutputStreamWriter(System.out, UTF_8)), true),
                    new PrintWriter(
                        new BufferedWriter(new OutputStreamWriter(System.err, UTF_8)), true),
                    "--replace",
                    testA.toString(),
                    testB.toString());
        assertWithMessage("Error Code").that(errorCode).isEqualTo(0);
      }
    
      // Verify that the number of threads is nearly the same before and after. NB: we allow for a few
      // additional threads since we don't completely control the number of running threads in this
      // test.
      assertThat(Thread.currentThread().getThreadGroup().activeCount())
          .isLessThan(numberOfThreadsBeforeFormatting + 5);
    }
    
  2. Run the test:

     mvn test -Dtest="MainTest#doesNotLeakThreads"
    

If the test fails you'll get an OOM error:

[WARNING] Corrupted STDOUT by directly writing to native stream in forked JVM 1. See FAQ web page and the dump file /Volumes/Projects/google/google-java-format/core/target/surefire-reports/2022-10-15T16-48-29_971-jvmRun1.dumpstream
[ERROR] Tests run: 1, Failures: 0, Errors: 1, Skipped: 0, Time elapsed: 1.654 s <<< FAILURE! - in com.google.googlejavaformat.java.MainTest
[ERROR] doesNotLeakThreads(com.google.googlejavaformat.java.MainTest)  Time elapsed: 1.629 s  <<< ERROR!
java.lang.OutOfMemoryError: unable to create native thread: possibly out of memory or process/resource limits reached
        at com.google.googlejavaformat.java.MainTest.doesNotLeakThreads(MainTest.java:645)

java.lang.OutOfMemoryError: unable to create native thread: possibly out of memory or process/resource limits reached
        at java.base/java.lang.Thread.start0(Native Method)
        at java.base/java.lang.Thread.start(Thread.java:798)
        at java.base/java.util.concurrent.ThreadPoolExecutor.addWorker(ThreadPoolExecutor.java:937)
        at java.base/java.util.concurrent.ThreadPoolExecutor.ensurePrestart(ThreadPoolExecutor.java:1583)
        at java.base/java.util.concurrent.ScheduledThreadPoolExecutor.delayedExecute(ScheduledThreadPoolExecutor.java:346)
        at java.base/java.util.concurrent.ScheduledThreadPoolExecutor.schedule(ScheduledThreadPoolExecutor.java:562)
        at org.apache.maven.surefire.booter.ForkedBooter.launchLastDitchDaemonShutdownThread(ForkedBooter.java:369)
        at org.apache.maven.surefire.booter.ForkedBooter.acknowledgedExit(ForkedBooter.java:333)
        at org.apache.maven.surefire.booter.ForkedBooter.execute(ForkedBooter.java:145)
        at org.apache.maven.surefire.booter.ForkedBooter.main(ForkedBooter.java:418)
Exception in thread "main" java.lang.OutOfMemoryError: unable to create native thread: possibly out of memory or process/resource limits reached
        at java.base/java.lang.Thread.start0(Native Method)
        at java.base/java.lang.Thread.start(Thread.java:798)
        at java.base/java.util.concurrent.ThreadPoolExecutor.addWorker(ThreadPoolExecutor.java:937)
        at java.base/java.util.concurrent.ThreadPoolExecutor.ensurePrestart(ThreadPoolExecutor.java:1583)
        at java.base/java.util.concurrent.ScheduledThreadPoolExecutor.delayedExecute(ScheduledThreadPoolExecutor.java:346)
        at java.base/java.util.concurrent.ScheduledThreadPoolExecutor.schedule(ScheduledThreadPoolExecutor.java:562)
        at org.apache.maven.surefire.booter.ForkedBooter.launchLastDitchDaemonShutdownThread(ForkedBooter.java:369)
        at org.apache.maven.surefire.booter.ForkedBooter.exit(ForkedBooter.java:316)
        at org.apache.maven.surefire.booter.ForkedBooter.main(ForkedBooter.java:425)

If the test passes the bug is fixed.

Activity

  1. stiemannkj1
    added 3 commits that reference this issue on Oct 15, 2022

    Fix google#846 Formatter leaks threads and memory

    This issue will close once commit 6de1c63 is merged into the 'master' branch.
    6de1c63

    Fix google#846 Formatter leaks threads and memory

    This issue will close once commit 5fcb5e0 is merged into the 'master' branch.
    5fcb5e0

    Fix google#846 Formatter leaks threads and memory

    This issue will close once commit 7e12876 is merged into the 'master' branch.
    7e12876
  2. stiemannkj1
    added a commit that references this issue on Oct 31, 2022

    Fix google#846 Formatter leaks threads and memory

    This issue will close once commit db89e6a is merged into the 'master' branch.
    db89e6a
  3. stiemannkj1
    added 3 commits that reference this issue on Feb 3, 2023

    Fix google#846 Formatter leaks threads and memory

    This issue will close once commit 3671975 is merged into the 'master' branch.
    3671975

    Fix google#846 Formatter leaks threads and memory

    This issue will close once commit e61f7d9 is merged into the 'master' branch.
    e61f7d9

    Fix google#846 Formatter leaks threads and memory

    This issue will close once commit 0ca1e9b is merged into the 'master' branch.
    0ca1e9b
  4. copybara-service
    added 2 commits that reference this issue on Feb 4, 2023

    Fix #846 Formatter leaks threads and memory

    This issue will close once commit 3f6f7e1 is merged into the 'master' branch.
    3f6f7e1

    Fix #846 Formatter leaks threads and memory

    This issue will close once commit 91223aa is merged into the 'master' branch.
    91223aa
  5. copybara-service
    closed this as completedin 91223aaon Feb 4, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    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

      You are visitor 01402775 — GeezerVison does not store the pages you look up.
      Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.