Skip to content

Commit

Permalink
#2530 Fix StringLogAppender leaks for MockHandler
Browse files Browse the repository at this point in the history
The StringBuilder object inside StringLogAppender has never be called collect() to de-refer all the string objects. it leads memory leak on long running mock server
  • Loading branch information
brown-kaew authored and marcinmilewski93 committed Jul 15, 2024
1 parent e4c8cac commit 52c3124
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 6 deletions.
30 changes: 25 additions & 5 deletions karate-core/src/main/java/com/intuit/karate/Logger.java
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,8 @@ public class Logger {

private boolean appendOnly;

private boolean logOnly;

public void setAppender(LogAppender appender) {
this.appender = appender;
}
Expand All @@ -68,6 +70,14 @@ public boolean isAppendOnly() {
return appendOnly;
}

public void setLogOnly(boolean logOnly) {
this.logOnly = logOnly;
}

public boolean isLogOnly() {
return logOnly;
}

public Logger(Class clazz) {
LOGGER = LoggerFactory.getLogger(clazz);
}
Expand All @@ -85,7 +95,9 @@ public void trace(String format, Object... arguments) {
if (!appendOnly) {
LOGGER.trace(format, arguments);
}
formatAndAppend(format, arguments);
if (!logOnly) {
formatAndAppend(format, arguments);
}
}
}

Expand All @@ -94,7 +106,9 @@ public void debug(String format, Object... arguments) {
if (!appendOnly) {
LOGGER.debug(format, arguments);
}
formatAndAppend(format, arguments);
if (!logOnly) {
formatAndAppend(format, arguments);
}
}
}

Expand All @@ -103,7 +117,9 @@ public void info(String format, Object... arguments) {
if (!appendOnly) {
LOGGER.info(format, arguments);
}
formatAndAppend(format, arguments);
if (!logOnly) {
formatAndAppend(format, arguments);
}
}
}

Expand All @@ -112,7 +128,9 @@ public void warn(String format, Object... arguments) {
if (!appendOnly) {
LOGGER.warn(format, arguments);
}
formatAndAppend(format, arguments);
if (!logOnly) {
formatAndAppend(format, arguments);
}
}
}

Expand All @@ -121,7 +139,9 @@ public void error(String format, Object... arguments) {
if (!appendOnly) {
LOGGER.error(format, arguments);
}
formatAndAppend(format, arguments);
if (!logOnly) {
formatAndAppend(format, arguments);
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,8 @@ private ScenarioRuntime initRuntime(Feature feature, Map<String, Object> args) {
section.setIndex(-1); // TODO util for creating dummy scenario
Scenario dummy = new Scenario(feature, section, -1);
section.setScenario(dummy);
ScenarioRuntime runtime = new ScenarioRuntime(featureRuntime, dummy);
ScenarioRuntime runtime = new ScenarioRuntime(featureRuntime, dummy);
runtime.logger.setLogOnly(true);
runtime.engine.setVariable(PATH_MATCHES, (Function<String, Boolean>) this::pathMatches);
runtime.engine.setVariable(PARAM_EXISTS, (Function<String, Boolean>) this::paramExists);
runtime.engine.setVariable(PARAM_VALUE, (Function<String, String>) this::paramValue);
Expand Down

0 comments on commit 52c3124

Please sign in to comment.