From b2883061f8f3df12817ce8e99b9e8ccca2d317a9 Mon Sep 17 00:00:00 2001 From: ndob Date: Sat, 3 Oct 2020 13:54:37 +0300 Subject: [PATCH] Fix CPU hogging when running in background. Using "System.in.read()" to block the main thread only works if the application is running in the foreground (user provides stdin). If running in background or if stdin is otherwise not blocking this causes 100% cpu usage. Fix by blocking the main thread infinitely with a condition variable. This retains the old functionality (CTRL-C still works to quit the application). --- .../java/org/cryptomator/cli/CryptomatorCli.java | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/src/main/java/org/cryptomator/cli/CryptomatorCli.java b/src/main/java/org/cryptomator/cli/CryptomatorCli.java index d2cc903..62f9c8c 100644 --- a/src/main/java/org/cryptomator/cli/CryptomatorCli.java +++ b/src/main/java/org/cryptomator/cli/CryptomatorCli.java @@ -116,12 +116,18 @@ private static Optional initWebDavServer(Args args) { private static void waitForShutdown(Runnable runnable) { Runtime.getRuntime().addShutdownHook(new Thread(runnable)); LOG.info("Press Ctrl+C to terminate."); + + // Block the main thread infinitely as otherwise when using + // Fuse mounts the application quits immediately. try { - while (true) { - System.in.read(); + Object mainThreadBlockLock = new Object(); + synchronized (mainThreadBlockLock) { + while (true) { + mainThreadBlockLock.wait(); + } } - } catch (IOException e) { - e.printStackTrace(); + } catch (Exception e) { + LOG.error("Main thread blocking failed."); } } }