Skip to content

Commit

Permalink
Recover from client connection errors (#2879)
Browse files Browse the repository at this point in the history
Attempt to fix #2805 We now
catch the exception, delete the content of the mill-worker-xyx directory
and try again.
Seems to be working correcly from manual tests.

Pull request: #2879
  • Loading branch information
lolgab committed Nov 16, 2023
1 parent d81aeeb commit bc84d5b
Showing 1 changed file with 56 additions and 43 deletions.
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

0 comments on commit bc84d5b

Please sign in to comment.