Skip to content

Commit d81f5e4

Browse files
authored
Fix prompt resizing when connecting to existing daemon (#5924)
Fixes #5919 This regressed in #5832, which made us only start the `terminfo` writing thread when spawning a new server, which neglects the case where we are connecting to an existing server. Thus when you ran `./mill clean && ./mill __.compile`, the `__.compile` runs with a launcher connected to the existing server, and thus doesn't update when the terminal size changes This PR makes us start the `terminfo` thread regardless of whether we are spawning a new server or not. Tested manually Also did some cleanup of `libs/daemon/client` to remove dead code.
1 parent 7b00eb7 commit d81f5e4

File tree

7 files changed

+31
-73
lines changed

7 files changed

+31
-73
lines changed

libs/daemon/client/src/mill/client/LaunchedServer.java

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,6 @@
33
public abstract class LaunchedServer {
44
public abstract boolean isAlive();
55

6-
public abstract void kill();
7-
86
/// An operating system process was launched.
97
public static class OsProcess extends LaunchedServer {
108
public final ProcessHandle process;
@@ -22,13 +20,6 @@ public String toString() {
2220
public boolean isAlive() {
2321
return process.isAlive();
2422
}
25-
26-
@Override
27-
public void kill() {
28-
if (!process.destroy())
29-
throw new IllegalStateException(
30-
"Asked " + process + " to terminate, but the termination request failed.");
31-
}
3223
}
3324

3425
/// Code running in the same process.
@@ -50,10 +41,5 @@ public String toString() {
5041
public boolean isAlive() {
5142
return thread.isAlive();
5243
}
53-
54-
@Override
55-
public void kill() {
56-
_kill.run();
57-
}
5844
}
5945
}

libs/daemon/client/src/mill/client/ServerLauncher.java

Lines changed: 5 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -37,16 +37,6 @@
3737
/// - Wait for `ProxyStream.END` packet or `clientSocket.close()`,
3838
/// indicating server has finished execution and all data has been received
3939
public abstract class ServerLauncher {
40-
public static class RunWithConnectionResult<A> {
41-
public final A result;
42-
43-
public final int exitCode;
44-
45-
public RunWithConnectionResult(A result, int exitCode) {
46-
this.result = result;
47-
this.exitCode = exitCode;
48-
}
49-
}
5040

5141
/// Run a client logic with a connection established to a Mill server (via [#connectToServer]).
5242
///
@@ -56,13 +46,13 @@ public RunWithConnectionResult(A result, int exitCode) {
5646
// client logic
5747
/// @param runClientLogic the client logic to run
5848
/// @return the exit code that the server sent back
59-
public static <A> RunWithConnectionResult<A> runWithConnection(
60-
String debugName,
49+
public static int runWithConnection(
6150
Socket connection,
6251
Streams streams,
6352
boolean closeConnectionAfterClientLogic,
6453
Consumer<OutputStream> sendInitData,
65-
RunClientLogic<A> runClientLogic)
54+
Runnable runClientLogic,
55+
String debugName)
6656
throws Exception {
6757
// According to
6858
// https://pzemtsov.github.io/2015/01/19/on-the-benefits-of-stream-buffering-in-Java.html it
@@ -76,10 +66,10 @@ public static <A> RunWithConnectionResult<A> runWithConnection(
7666
socketOutputStream.flush();
7767
var pumperThread =
7868
startStreamPumpers(socketInputStream, socketOutputStream, streams, debugName);
79-
var result = runClientLogic.run();
69+
runClientLogic.run();
8070
if (closeConnectionAfterClientLogic) socketInputStream.close();
8171
pumperThread.join();
82-
return new RunWithConnectionResult<>(result, pumperThread.exitCode());
72+
return pumperThread.exitCode();
8373
}
8474

8575
/// Run a client logic with a connection established to a Mill server (via [#connectToServer]).
@@ -90,7 +80,6 @@ public static <A> RunWithConnectionResult<A> runWithConnection(
9080
/// @param runClientLogic the client logic to run
9181
/// @return the exit code that the server sent back
9282
public static <A> A runWithConnection(
93-
String debugName,
9483
Socket connection,
9584
boolean closeConnectionAfterClientLogic,
9685
Consumer<OutputStream> sendInitData,
@@ -266,11 +255,6 @@ public static ServerLaunchResult ensureServerIsRunning(
266255
public boolean isAlive() {
267256
throw new RuntimeException("not implemented, this should never happen");
268257
}
269-
270-
@Override
271-
public void kill() {
272-
throw new RuntimeException("not implemented, this should never happen");
273-
}
274258
};
275259
return Optional.of(new ServerLaunchResult.AlreadyRunning(launchedServer));
276260
} catch (IOException | NumberFormatException e) {
@@ -353,26 +337,11 @@ public interface InitServer {
353337
LaunchedServer init() throws Exception;
354338
}
355339

356-
public interface RunClientLogic<A> {
357-
/// Runs the client logic.
358-
A run() throws Exception;
359-
}
360-
361340
public interface RunClientLogicWithStreams<A> {
362341
/// Runs the client logic.
363342
A run(BufferedInputStream inStream, BufferedOutputStream outStream) throws Exception;
364343
}
365344

366-
public static class Result {
367-
public final int exitCode;
368-
public final Path daemonDir;
369-
370-
public Result(int exitCode, Path daemonDir) {
371-
this.exitCode = exitCode;
372-
this.daemonDir = daemonDir;
373-
}
374-
}
375-
376345
public static class Streams {
377346
/// The input stream to send to the server as the stdin.
378347
public final InputStream stdin;

libs/javalib/worker/src/mill/javalib/worker/JvmWorkerImpl.scala

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -340,7 +340,6 @@ class JvmWorkerImpl(args: JvmWorkerArgs) extends JvmWorkerApi with AutoCloseable
340340
val debugName =
341341
s"ZincWorker,TCP ${socket.getRemoteSocketAddress} -> ${socket.getLocalSocketAddress}"
342342
ServerLauncher.runWithConnection(
343-
debugName,
344343
socket,
345344
/* closeConnectionAfterCommand */ true,
346345
/* sendInitData */ _ => {},

runner/client/src/mill/client/MillServerLauncher.java

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ public MillServerLauncher(
4343
*/
4444
public abstract LaunchedServer initServer(Path daemonDir, Locks locks) throws Exception;
4545

46-
public Result run(Path daemonDir, String javaHome, Consumer<String> log) throws Exception {
46+
public int run(Path daemonDir, String javaHome, Consumer<String> log) throws Exception {
4747
Files.createDirectories(daemonDir);
4848

4949
var initData = new ClientInitData(
@@ -76,8 +76,6 @@ public Result run(Path daemonDir, String javaHome, Consumer<String> log) throws
7676
true /*openSocket*/)) {
7777
log.accept("runWithConnection: " + launched);
7878
var result = runWithConnection(
79-
"MillServerLauncher[" + launched.socket.getLocalSocketAddress() + " -> "
80-
+ launched.socket.getRemoteSocketAddress() + "]",
8179
launched.socket,
8280
streams,
8381
false,
@@ -93,19 +91,24 @@ public Result run(Path daemonDir, String javaHome, Consumer<String> log) throws
9391
() -> {
9492
log.accept("running client logic");
9593
forceTestFailure(daemonDir, log);
96-
return 0;
97-
});
98-
log.accept("runWithConnection exit code: " + result.exitCode);
99-
return new Result(result.exitCode, daemonDir);
94+
},
95+
"MillServerLauncher[" + launched.socket.getLocalSocketAddress() + " -> "
96+
+ launched.socket.getRemoteSocketAddress() + "]");
97+
log.accept("runWithConnection exit code: " + result);
98+
return result;
10099
}
101100
}
102101

103-
private void forceTestFailure(Path daemonDir, Consumer<String> log) throws Exception {
102+
private void forceTestFailure(Path daemonDir, Consumer<String> log) {
104103
if (forceFailureForTestingMillisDelay > 0) {
105104
log.accept(
106105
"Force failure for testing in " + forceFailureForTestingMillisDelay + "ms: " + daemonDir);
107-
Thread.sleep(forceFailureForTestingMillisDelay);
108-
throw new Exception("Force failure for testing: " + daemonDir);
106+
try {
107+
Thread.sleep(forceFailureForTestingMillisDelay);
108+
} catch (InterruptedException e) {
109+
throw new RuntimeException(e);
110+
}
111+
throw new RuntimeException("Force failure for testing: " + daemonDir);
109112
} else {
110113
log.accept("No force failure for testing: " + daemonDir);
111114
}

runner/launcher/src/mill/launcher/MillLauncherMain.java

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -48,12 +48,9 @@ public static void main(String[] args) throws Exception {
4848
// Ensure that if we're running in BSP mode we don't start a daemon.
4949
//
5050
// This is needed because when Metals/Idea closes, they only kill the BSP client and the BSP
51-
// server lurks around
52-
// waiting for the next client to connect.
53-
//
51+
// server lurks around waiting for the next client to connect.
5452
// This is unintuitive from the user's perspective and wastes resources, as most people expect
55-
// everything related
56-
// to the BSP server to be killed when closing the editor.
53+
// everything related to the BSP server to be killed when closing the editor.
5754
if (bspMode) runNoDaemon = true;
5855

5956
var outMode = bspMode ? OutFolderMode.BSP : OutFolderMode.REGULAR;
@@ -116,12 +113,13 @@ public LaunchedServer initServer(Path daemonDir, Locks locks) throws Exception {
116113
}
117114
};
118115

119-
var daemonDir0 = Paths.get(outDir, OutFiles.millDaemon);
116+
var daemonDir = Paths.get(outDir, OutFiles.millDaemon);
120117
String javaHome = MillProcessLauncher.javaHome(outMode);
121118

122-
var exitCode = launcher.run(daemonDir0, javaHome, log).exitCode;
119+
MillProcessLauncher.prepareMillRunFolder(daemonDir);
120+
var exitCode = launcher.run(daemonDir, javaHome, log);
123121
if (exitCode == ClientUtil.ExitServerCodeWhenVersionMismatch()) {
124-
exitCode = launcher.run(daemonDir0, javaHome, log).exitCode;
122+
exitCode = launcher.run(daemonDir, javaHome, log);
125123
}
126124
System.exit(exitCode);
127125
} catch (Exception e) {

runner/launcher/src/mill/launcher/MillProcessLauncher.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@ static int launchMillNoDaemon(
2626
final Path processDir =
2727
Paths.get(".").resolve(outFor(outMode)).resolve(millNoDaemon).resolve(sig);
2828

29+
MillProcessLauncher.prepareMillRunFolder(processDir);
30+
2931
final List<String> l = new ArrayList<>();
3032
l.addAll(millLaunchJvmCommand(outMode, runnerClasspath));
3133
Map<String, String> propsMap = ClientUtil.getUserSetProperties();
@@ -77,7 +79,7 @@ static Process configureRunMillProcess(ProcessBuilder builder, Path daemonDir) t
7779

7880
Path sandbox = daemonDir.resolve(DaemonFiles.sandbox);
7981
Files.createDirectories(sandbox);
80-
MillProcessLauncher.prepareMillRunFolder(daemonDir);
82+
8183
builder.environment().put(EnvVars.MILL_WORKSPACE_ROOT, new File("").getCanonicalPath());
8284
if (System.getenv(EnvVars.MILL_EXECUTABLE_PATH) == null)
8385
builder.environment().put(EnvVars.MILL_EXECUTABLE_PATH, getExecutablePath());

runner/server/test/src/mill/server/ClientServerTests.scala

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,7 @@ object ClientServerTests extends TestSuite {
9999
val in = new ByteArrayInputStream(s"hello$ENDL".getBytes())
100100
val out = new ByteArrayOutputStream()
101101
val err = new ByteArrayOutputStream()
102+
val daemonDir = outDir / "server-0"
102103
val result = new MillServerLauncher(
103104
ServerLauncher.Streams(in, out, err),
104105
env.asJava,
@@ -121,14 +122,14 @@ object ClientServerTests extends TestSuite {
121122
LaunchedServer.NewThread(t, () => { /* do nothing */ })
122123
}
123124
}.run(
124-
(outDir / "server-0").relativeTo(os.pwd).toNIO,
125+
daemonDir.relativeTo(os.pwd).toNIO,
125126
"",
126127
msg => println(s"MillServerLauncher: $msg")
127128
)
128129

129130
ClientResult(
130-
result.exitCode,
131-
os.Path(result.daemonDir, os.pwd),
131+
result,
132+
daemonDir,
132133
outDir,
133134
out.toString,
134135
err.toString

0 commit comments

Comments
 (0)