Skip to content

Commit e1778ba

Browse files
authored
Merge pull request #1733 from marklogic/feature/oom-memory-leak
Sanity check to ensure connection is closed
2 parents 9a20a17 + dcabf41 commit e1778ba

File tree

2 files changed

+27
-9
lines changed

2 files changed

+27
-9
lines changed

marklogic-client-api/src/main/java/com/marklogic/client/eval/EvalResultIterator.java

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,23 @@
66
import java.io.Closeable;
77
import java.util.Iterator;
88

9-
/** An Iterator to walk through all results returned from calls to
9+
/**
10+
* An Iterator to walk through all results returned from calls to
1011
* {@link ServerEvaluationCall#eval()}.
1112
*/
1213
public interface EvalResultIterator extends Iterable<EvalResult>, Iterator<EvalResult>, Closeable {
13-
@Override
14-
Iterator<EvalResult> iterator();
15-
@Override
16-
boolean hasNext();
17-
@Override
18-
EvalResult next();
19-
void close();
14+
@Override
15+
Iterator<EvalResult> iterator();
16+
17+
@Override
18+
boolean hasNext();
19+
20+
@Override
21+
EvalResult next();
22+
23+
/**
24+
* As of 7.1.0, this must be called to ensure that the response is closed, as results are now
25+
* streamed from MarkLogic instead of being read entirely into memory first.
26+
*/
27+
void close();
2028
}

marklogic-client-api/src/main/java/com/marklogic/client/impl/OkHttpServices.java

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3856,7 +3856,17 @@ private <U extends OkHttpResultIterator> U evalAndStreamResults(RequestLogger re
38563856
try {
38573857
MultipartReader reader = new MultipartReader(response.body());
38583858
PartIterator partIterator = new PartIterator(reader);
3859-
return (U) new DefaultOkHttpResultIterator(reqlog, partIterator, response);
3859+
return (U) new DefaultOkHttpResultIterator(reqlog, partIterator, () -> {
3860+
// Looking at OkHttp source code, it does not appear necessary to call close on the reader; it appears
3861+
// sufficient to only call it on the response. But doing both in case this behavior changes in a future
3862+
// OkHttp release.
3863+
try {
3864+
reader.close();
3865+
} catch (IOException e) {
3866+
// Ignore, the next call should close everything properly.
3867+
}
3868+
response.close();
3869+
});
38603870
} catch (IOException e) {
38613871
throw new MarkLogicIOException(e);
38623872
}

0 commit comments

Comments
 (0)