Avoid logging "length=0" exception

Avoid logging ssh exception for "stream is already closed" when length=0
if present in the stacktrace. This will reduce noise and we will be able
to see if we get this exception when length is not 0.

This exception is related to ssh streams being prematurely closed by the
connected slave frontends. This exception can be safely omitted from the
logs because it is thrown when sshd.flush is called and the channel is
closed. Length = 0 means that there was nothing to be flushed and no new
information is provided by this stack trace.

Change-Id: I85f77cab03453ec51d33b226804ed7f2a454ae6f
This commit is contained in:
Mark Bekhet
2019-06-13 14:08:36 -04:00
parent 9134e7917e
commit 5e4a4e7e48

View File

@@ -51,6 +51,7 @@ import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.io.StringWriter;
import java.nio.charset.Charset;
import java.util.Arrays;
import java.util.concurrent.Future;
import java.util.concurrent.ScheduledThreadPoolExecutor;
import java.util.concurrent.atomic.AtomicReference;
@@ -356,7 +357,7 @@ public abstract class BaseCommand implements Command {
}
m.append(" during ");
m.append(context.getCommandLine());
logger.atSevere().withCause(e).log(m.toString());
logCauseIfRelevant(e, m);
}
if (e instanceof Failure) {
@@ -383,6 +384,20 @@ public abstract class BaseCommand implements Command {
return 128;
}
private void logCauseIfRelevant(Throwable e, StringBuilder message) {
String zeroLength = "length=0";
String streamAlreadyClosed = "stream is already closed";
boolean isZeroLength = false;
if (streamAlreadyClosed.equals(e.getMessage())) {
StackTraceElement[] stackTrace = e.getStackTrace();
isZeroLength = Arrays.stream(stackTrace).anyMatch(s -> s.toString().contains(zeroLength));
}
if (!isZeroLength) {
logger.atSevere().withCause(e).log(message.toString());
}
}
protected UnloggedFailure die(String msg) {
return new UnloggedFailure(1, "fatal: " + msg);
}