From 46a199ebf7cf4b047de4e59a4de44dc5f7ec2bfb Mon Sep 17 00:00:00 2001 From: Li Haoyi Date: Fri, 6 Dec 2024 08:57:28 +0800 Subject: [PATCH] More fixes for prompt logger (#4075) Fixes https://github.com/com-lihaoyi/mill/issues/4074 We shouldn't sleep for `nonInteractivePromptUpdateIntervalMillis` every time `readTerminalDims` fails, because that means if there's a transient failure it doesn't check again for 60s during which there is no prompt shown Instead, we always check after `promptUpdateIntervalMillis` seconds, and but only refresh the prompt if `now - lastUpdate > nonInteractivePromptUpdateIntervalMillis` Also we make sure we call `writeTerminalDims` at least once before we start the Mill background process, rather than relying on the background thread to reach that code path in time Either of these fixes alone should solve the issue, but might as well do both --- main/util/src/mill/util/PromptLogger.scala | 23 ++++++++++------- .../mill/runner/client/MillClientMain.java | 5 +--- .../runner/client/MillProcessLauncher.java | 25 ++++++++++++------- 3 files changed, 31 insertions(+), 22 deletions(-) diff --git a/main/util/src/mill/util/PromptLogger.scala b/main/util/src/mill/util/PromptLogger.scala index 4459d33dd39..4c06142ffc7 100644 --- a/main/util/src/mill/util/PromptLogger.scala +++ b/main/util/src/mill/util/PromptLogger.scala @@ -63,23 +63,28 @@ private[mill] class PromptLogger( if (enableTicker) refreshPrompt() val promptUpdaterThread = new Thread( - () => + () => { + var lastUpdate = System.currentTimeMillis() while (!runningState.stopped) { - val promptUpdateInterval = - if (termDimensions._1.isDefined) promptUpdateIntervalMillis - else nonInteractivePromptUpdateIntervalMillis - - try Thread.sleep(promptUpdateInterval) + try Thread.sleep(promptUpdateIntervalMillis) catch { case e: InterruptedException => /*do nothing*/ } readTerminalDims(terminfoPath).foreach(termDimensions = _) - synchronized { - if (!runningState.paused && !runningState.stopped) refreshPrompt() + val now = System.currentTimeMillis() + if ( + termDimensions._1.nonEmpty || + (now - lastUpdate > nonInteractivePromptUpdateIntervalMillis) + ) { + lastUpdate = now + synchronized { + if (!runningState.paused && !runningState.stopped) refreshPrompt() + } } - }, + } + }, "prompt-logger-updater-thread" ) diff --git a/runner/client/src/mill/runner/client/MillClientMain.java b/runner/client/src/mill/runner/client/MillClientMain.java index aecee51404e..24452c2ac16 100644 --- a/runner/client/src/mill/runner/client/MillClientMain.java +++ b/runner/client/src/mill/runner/client/MillClientMain.java @@ -5,10 +5,7 @@ import java.nio.file.Path; import java.util.Arrays; import java.util.Collections; -import mill.main.client.OutFiles; -import mill.main.client.ServerCouldNotBeStarted; -import mill.main.client.ServerLauncher; -import mill.main.client.Util; +import mill.main.client.*; import mill.main.client.lock.Locks; /** diff --git a/runner/client/src/mill/runner/client/MillProcessLauncher.java b/runner/client/src/mill/runner/client/MillProcessLauncher.java index 641b078ebeb..95ccfa02e77 100644 --- a/runner/client/src/mill/runner/client/MillProcessLauncher.java +++ b/runner/client/src/mill/runner/client/MillProcessLauncher.java @@ -32,8 +32,8 @@ static int launchMillNoServer(String[] args) throws Exception { boolean interrupted = false; try { - Process p = configureRunMillProcess(builder, processDir); MillProcessLauncher.runTermInfoThread(processDir); + Process p = configureRunMillProcess(builder, processDir); return p.waitFor(); } catch (InterruptedException e) { @@ -230,18 +230,25 @@ static void writeTerminalDims(boolean tputExists, Path serverDir) throws Excepti Files.write(serverDir.resolve(ServerFiles.terminfo), str.getBytes()); } + public static boolean checkTputExists() { + try { + getTerminalDim("cols", false); + getTerminalDim("lines", false); + return true; + } catch (Exception e) { + return false; + } + } + public static void runTermInfoThread(Path serverDir) throws Exception { + Path sandbox = serverDir.resolve(ServerFiles.sandbox); + Files.createDirectories(sandbox); + boolean tputExists = checkTputExists(); + + writeTerminalDims(tputExists, serverDir); Thread termInfoPropagatorThread = new Thread( () -> { try { - boolean tputExists; - try { - getTerminalDim("cols", false); - getTerminalDim("lines", false); - tputExists = true; - } catch (Exception e) { - tputExists = false; - } while (true) { writeTerminalDims(tputExists, serverDir); Thread.sleep(100);