Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Recover from client connection errors #2879

Merged
merged 1 commit into from
Nov 16, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
99 changes: 56 additions & 43 deletions main/client/src/mill/main/client/MillClientMain.java
Original file line number Diff line number Diff line change
Expand Up @@ -101,45 +101,58 @@ public static int main0(String[] args) throws Exception {

int index = 0;
while (index < serverProcessesLimit) {
index += 1;
index++;
final String lockBase = "out/mill-worker-" + versionAndJvmHomeEncoding + "-" + index;
new java.io.File(lockBase).mkdirs();

final File stdout = new java.io.File(lockBase + "/stdout");
final File stderr = new java.io.File(lockBase + "/stderr");
final int refeshIntervalMillis = 2;

try (
Locks locks = Locks.files(lockBase);
final FileToStreamTailer stdoutTailer = new FileToStreamTailer(stdout, System.out, refeshIntervalMillis);
final FileToStreamTailer stderrTailer = new FileToStreamTailer(stderr, System.err, refeshIntervalMillis);
) {
Locked clientLock = locks.clientLock.tryLock();
if (clientLock != null) {
stdoutTailer.start();
stderrTailer.start();
final int exitCode = run(
lockBase,
() -> {
try {
initServer(lockBase, setJnaNoSys);
} catch (Exception e) {
throw new RuntimeException(e);
}
},
locks,
System.in,
System.out,
System.err,
args,
System.getenv()
);

// Here, we ensure we process the tails of the output files before interrupting the threads
stdoutTailer.flush();
stderrTailer.flush();
clientLock.release();
return exitCode;
java.io.File lockBaseFile = new java.io.File(lockBase);
final File stdout = new java.io.File(lockBaseFile, "stdout");
final File stderr = new java.io.File(lockBaseFile, "stderr");

int attempts = 0;
while (attempts < 3) {
try {
lockBaseFile.mkdirs();

final int refeshIntervalMillis = 2;

try (
Locks locks = Locks.files(lockBase);
final FileToStreamTailer stdoutTailer =new FileToStreamTailer(stdout, System.out, refeshIntervalMillis);
final FileToStreamTailer stderrTailer = new FileToStreamTailer(stderr, System.err, refeshIntervalMillis);
) {
Locked clientLock = locks.clientLock.tryLock();
if (clientLock != null) {
stdoutTailer.start();
stderrTailer.start();
final int exitCode = run(
lockBase,
() -> {
try {
initServer(lockBase, setJnaNoSys);
} catch (Exception e) {
throw new RuntimeException(e);
}
},
locks,
System.in,
System.out,
System.err,
args,
System.getenv());

// Here, we ensure we process the tails of the output files before interrupting
// the threads
stdoutTailer.flush();
stderrTailer.flush();
clientLock.release();
return exitCode;
}
}
} catch (Exception e) {
for (File file : lockBaseFile.listFiles()) {
file.delete();
}
} finally {
attempts++;
}
}
}
Expand Down Expand Up @@ -176,14 +189,14 @@ public static int run(
}
while (locks.processLock.probe()) Thread.sleep(3);

String socketName = lockBase + "/mill-" + Util.md5hex(new File(lockBase).getCanonicalPath()) + "-io";
AFUNIXSocketAddress addr = AFUNIXSocketAddress.of(new File(socketName));

long retryStart = System.currentTimeMillis();
Socket ioSocket = null;
Throwable socketThrowable = null;
long retryStart = System.currentTimeMillis();

while (ioSocket == null && System.currentTimeMillis() - retryStart < 5000) {
while (ioSocket == null && System.currentTimeMillis() - retryStart < 1000) {
try {
String socketName = lockBase + "/mill-" + Util.md5hex(new File(lockBase).getCanonicalPath()) + "-io";
AFUNIXSocketAddress addr = AFUNIXSocketAddress.of(new File(socketName));
ioSocket = AFUNIXSocket.connectTo(addr);
} catch (Throwable e) {
socketThrowable = e;
Expand Down