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

Fix file descriptor leak in logger reinitialisation logic and print stack trace in severe logs #347

Merged
merged 3 commits into from
Aug 29, 2023
Merged
Show file tree
Hide file tree
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
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ of this software and associated documentation files (the "Software"), to deal
import jenkins.model.Jenkins;
import org.apache.commons.lang.StringEscapeUtils;
import org.apache.commons.lang.StringUtils;
import org.apache.commons.lang.exception.ExceptionUtils;
import org.datadog.jenkins.plugins.datadog.clients.HttpClient;
import org.datadog.jenkins.plugins.datadog.model.CIGlobalTagsAction;
import org.datadog.jenkins.plugins.datadog.model.GitCommitAction;
Expand Down Expand Up @@ -862,18 +863,14 @@ public static String statusFromResult(String result) {
}

@SuppressFBWarnings("NP_NULL_ON_SOME_PATH")
public static void severe(Logger logger, Throwable e, String message) {
if (message == null) {
message = e != null ? "An unexpected error occurred" : "";
public static void severe(Logger logger, Throwable e, String message){
if (e != null) {
String stackTrace = ExceptionUtils.getStackTrace(e);
message = (message != null ? message : "An unexpected error occurred: ") + stackTrace;
}
if (!message.isEmpty()) {
if (StringUtils.isNotEmpty(message)) {
logger.severe(message);
}
if (e != null) {
StringWriter sw = new StringWriter();
e.printStackTrace(new PrintWriter(sw));
logger.info(message + ": " + sw.toString());
}
}

public static int toInt(boolean b) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -287,6 +287,8 @@ private boolean reinitializeLogger(boolean force) {
}
try {
logger.info("Re/Initialize Datadog-Plugin Logger: hostname = " + this.hostname + ", logCollectionPort = " + this.logCollectionPort);
// need to close existing logger since it has a socket opened - not closing it leads to a file descriptor leak
close(this.ddLogger);
this.ddLogger = Logger.getLogger("Datadog-Plugin Logger");
this.ddLogger.setUseParentHandlers(false);
//Remove all existing Handlers
Expand Down Expand Up @@ -315,6 +317,24 @@ private boolean reinitializeLogger(boolean force) {
return true;
}


private void close(Logger logger) {
if (logger == null) {
return;
}
Handler[] handlers = logger.getHandlers();
if (handlers == null) {
return;
}
for (Handler handler : handlers) {
try {
handler.close();
} catch (Exception e) {
// ignore
}
}
}

/**
* Fetches the supported endpoints from the Trace Agent /info API
*
Expand Down
Loading