Skip to content

Commit

Permalink
Fix spawning of terminfo thread in server mode (#3833)
Browse files Browse the repository at this point in the history
Previously we only spawned the terminfo updater when spawning the Mill
server process, but that only handles the no-server case and
first-time-spawning-server case, without handling the
subsequently-re-use-server case.

This moves the logic into a helper method so we can call it from both
server and no-server launchers at the first point at which we have a
dedicated `serverDir` available. Since the `serverDIr` is only available
deep inside the `ServerLauncher` object, as it needs to do the work of
testing different locks to pick a server, we add a `preRun` hook that we
can override to start the terminfo updater thread
  • Loading branch information
lihaoyi authored Oct 24, 2024
1 parent 811e333 commit d6d672b
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 23 deletions.
2 changes: 2 additions & 0 deletions main/client/src/mill/main/client/ServerLauncher.java
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ public static class Result{
final int serverProcessesLimit = 5;
final int serverInitWaitMillis = 10000;
public abstract void initServer(Path serverDir, boolean b, Locks locks) throws Exception;
public abstract void preRun(Path serverDir) throws Exception;
InputStream stdin;
PrintStream stdout;
PrintStream stderr;
Expand Down Expand Up @@ -101,6 +102,7 @@ public Result acquireLocksAndRun(String outDir) throws Exception {
) {
if (clientLocked.isLocked()) {
Result result = new Result();
preRun(serverDir);
result.exitCode = run(serverDir, setJnaNoSys, locks);
result.serverDir = serverDir;
return result;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@ object ClientServerTests extends TestSuite {
memoryLocks,
forceFailureForTestingMillisDelay
) {
def preRun(serverDir: Path) = { /*do nothing*/ }
def initServer(serverDir: Path, b: Boolean, locks: Locks) = {
val serverId = "server-" + nextServerId
nextServerId += 1
Expand Down
3 changes: 3 additions & 0 deletions runner/client/src/mill/runner/client/MillClientMain.java
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,9 @@ public static void main(String[] args) throws Exception {
public void initServer(Path serverDir, boolean setJnaNoSys, Locks locks) throws Exception{
MillProcessLauncher.launchMillServer(serverDir, setJnaNoSys);
}
public void preRun(Path serverDir) throws Exception {
MillProcessLauncher.runTermInfoThread(serverDir);
}
};
int exitCode = launcher.acquireLocksAndRun(OutFiles.out).exitCode;
if (exitCode == Util.ExitServerCodeWhenVersionMismatch()) {
Expand Down
48 changes: 25 additions & 23 deletions runner/client/src/mill/runner/client/MillProcessLauncher.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import java.nio.file.Paths;
import java.util.*;

import mill.main.client.DebugLog;
import org.jline.terminal.TerminalBuilder;
import org.jline.terminal.Terminal;

Expand Down Expand Up @@ -37,7 +38,9 @@ static int launchMillNoServer(String[] args) throws Exception {
boolean interrupted = false;

try {
return configureRunMillProcess(builder, processDir).waitFor();
Process p = configureRunMillProcess(builder, processDir);
MillProcessLauncher.runTermInfoThread(processDir);
return p.waitFor();

} catch (InterruptedException e) {
interrupted = true;
Expand Down Expand Up @@ -71,30 +74,9 @@ static Process configureRunMillProcess(
ProcessBuilder builder,
Path serverDir
) throws Exception {
Terminal term = TerminalBuilder.builder().dumb(true).build();

Path sandbox = serverDir.resolve(ServerFiles.sandbox);
Files.createDirectories(sandbox);
Files.write(
serverDir.resolve(ServerFiles.terminfo),
(term.getWidth() + " " + term.getHeight()).getBytes()
);
Thread termInfoPropagatorThread = new Thread(
() -> {
try {

while(true){
Files.write(
serverDir.resolve(ServerFiles.terminfo),
(term.getWidth() + " " + term.getHeight()).getBytes()
);

Thread.sleep(100);
}
}catch (Exception e){}
},
"TermInfoPropagatorThread"
);
termInfoPropagatorThread.start();
builder.environment().put(EnvVars.MILL_WORKSPACE_ROOT, new File("").getCanonicalPath());

builder.directory(sandbox.toFile());
Expand Down Expand Up @@ -214,4 +196,24 @@ static List<String> millLaunchJvmCommand(boolean setJnaNoSys) throws Exception {
static List<String> readMillJvmOpts() {
return Util.readOptsFileLines(millJvmOptsFile());
}

public static void runTermInfoThread(Path serverDir) throws Exception{
Terminal term = TerminalBuilder.builder().dumb(true).build();
Thread termInfoPropagatorThread = new Thread(
() -> {
try {
while(true){
Files.write(
serverDir.resolve(ServerFiles.terminfo),
(term.getWidth() + " " + term.getHeight()).getBytes()
);

Thread.sleep(100);
}
}catch (Exception e){}
},
"TermInfoPropagatorThread"
);
termInfoPropagatorThread.start();
}
}

0 comments on commit d6d672b

Please sign in to comment.