Search code examples
mysqljvmmariadblogbackshutdown-hook

Low logging level preventing shutdown hook to run properly


I am using the MariaDb4j library for my integration tests and it registers a shutdown hook, this way:

    protected void cleanupOnExit() {
    String threadName = "Shutdown Hook Deletion Thread for Temporary DB " + dataDir.toString();
    final DB db = this;
    Runtime.getRuntime().addShutdownHook(new Thread(threadName) {

        @Override
        public void run() {
            // ManagedProcess DestroyOnShutdown ProcessDestroyer does
            // something similar, but it shouldn't hurt to better be save
            // than sorry and do it again ourselves here as well.
            try {
                // Shut up and don't log if it was already stop() before
                if (mysqldProcess != null && mysqldProcess.isAlive()) {
                    logger.info("cleanupOnExit() ShutdownHook now stopping database");
                    db.stop();
                }
            } catch (ManagedProcessException e) {
                logger.warn("cleanupOnExit() ShutdownHook: An error occurred while stopping the database", e);
            }

            if (dataDir.exists() && Util.isTemporaryDirectory(dataDir.getAbsolutePath())) {
                logger.info("cleanupOnExit() ShutdownHook quietly deleting temporary DB data directory: " + dataDir);
                FileUtils.deleteQuietly(dataDir);
            }
            if (baseDir.exists() && Util.isTemporaryDirectory(baseDir.getAbsolutePath())) {
                logger.info("cleanupOnExit() ShutdownHook quietly deleting temporary DB base directory: " + baseDir);
                FileUtils.deleteQuietly(baseDir);
            }
        }
    });
}

This was working fine. But then I added Logback and created a Console appender.

    <appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
      <encoder>
          <Pattern>${defaultPattern}</Pattern>
      </encoder>
  </appender>

  <root level="DEBUG">
      <appender-ref ref="STDOUT" />
  </root>

If I set the Logging level to WARN or ERROR it is still working fine, but when I set it to INFO or lower then I get this exception:

Exception: java.lang.IllegalStateException thrown from the UncaughtExceptionHandler in thread "Shutdown Hook Deletion Thread for Temporary DB /var/folders/t5/lr8ytf257hb9_649cjp9hkn40000gn/T/MariaDB4j/data/3306"

"Shutdown Hook Deletion Thread for Temporary DB... " is the name of the thread registered in the first piece of code above.

The result is that I am left with a mysqld process running. And that prevents the tests to run again as MariaDB4j complains about it and won't start a new database.

Once I kill the mysqld process than I can run my tests again, but then same thing happens.

I assume this is a JVM problem. I don't see how the logging level can prevent a shutdown hook to work properly.

I use MariaDB4j in my integration tests. When I run them with IntelliJ or Eclipse it do not get this error. I only get it when I run them with gradle (as part of the build task).

What could be causing that and how to get around it?


Solution

  • I had similar problem. It was caused by Gradle issue which is described there.

    It can be solved by downgrading Gradle to version 3.2 or upgrading to version 3.5.