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

Attempt to fix logText progressive memory issue in LargeText.java #10236

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
6 changes: 6 additions & 0 deletions core/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -250,6 +250,12 @@ THE SOFTWARE.
<groupId>org.connectbot</groupId>
<artifactId>jbcrypt</artifactId>
</dependency>
<dependency>
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-server</artifactId>
<version>9.4.51.v20230217</version>
<scope>compile</scope>
</dependency>
<dependency>
<!-- Groovy shell uses this, but it doesn't declare the dependency -->
<groupId>org.fusesource.jansi</groupId>
Expand Down
32 changes: 24 additions & 8 deletions core/src/main/java/hudson/console/AnnotatedLargeText.java
Original file line number Diff line number Diff line change
Expand Up @@ -197,16 +197,20 @@ private ConsoleAnnotator<T> createAnnotator(StaplerRequest2 req) throws IOExcept
private ConsoleAnnotator getConsoleAnnotator(ObjectInputStream ois) throws IOException, ClassNotFoundException {
return (ConsoleAnnotator) ois.readObject();
}

@CheckReturnValue
@Override
public long writeLogTo(long start, Writer w) throws IOException {
if (isHtml())
if (isHtml()) {
return writeHtmlTo(start, w);
else
return super.writeLogTo(start, w);
}

long bytesRead = super.writeLogTo(start, w);
w.flush();

return bytesRead;
}


/**
* Strips annotations using a {@link PlainTextConsoleOutputStream}.
* {@inheritDoc}
Expand All @@ -231,17 +235,29 @@ public long writeHtmlTo(long start, Writer w) throws IOException {
ConsoleAnnotationOutputStream<T> caw = new ConsoleAnnotationOutputStream<>(
w, createAnnotator(Stapler.getCurrentRequest2()), context, charset);
long r = super.writeLogTo(start, caw);
long bytesRead = 0;

try (Writer writer = w) {
bytesRead = super.writeLogTo(start, writer);
writer.flush();

}
ByteArrayOutputStream baos = new ByteArrayOutputStream();
Cipher sym = PASSING_ANNOTATOR.encrypt();
ObjectOutputStream oos = AnonymousClassWarnings.checkingObjectOutputStream(new GZIPOutputStream(new CipherOutputStream(baos, sym)));
oos.writeLong(System.currentTimeMillis()); // send timestamp to prevent a replay attack
ObjectOutputStream oos = AnonymousClassWarnings.checkingObjectOutputStream(
new GZIPOutputStream(new CipherOutputStream(baos, sym))
);

oos.writeLong(System.currentTimeMillis());
oos.writeObject(caw.getConsoleAnnotator());
oos.close();

StaplerResponse2 rsp = Stapler.getCurrentResponse2();
if (rsp != null)
if (rsp != null) {
rsp.setHeader("X-ConsoleAnnotator", Base64.getEncoder().encodeToString(baos.toByteArray()));
return r;
}

return bytesRead;
}

/**
Expand Down
6 changes: 6 additions & 0 deletions core/src/main/java/hudson/util/ChunkedInputStream.java
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,9 @@ public int read(byte[] b, int off, int len) throws IOException {
len = Math.min(len, chunkSize - pos);
int count = in.read(b, off, len);
pos += count;
if (pos >= chunkSize) {
in.skip(2);
}
return count;
}

Expand Down Expand Up @@ -273,6 +276,9 @@ private static int getChunkSizeFromInputStream(final InputStream in)
} catch (NumberFormatException e) {
throw new IOException("Bad chunk size: " + dataString, e);
}
if (result > 10 * 1024 * 1024) {
throw new IOException("Chunk size too large: " + result);
}
return result;
}

Expand Down