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

NullPointerException: "Deflater has been closed" emanating from FilterServletOutputStream #553

Merged
merged 1 commit into from
May 28, 2024
Merged
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 @@ -15,6 +15,16 @@ public class FilterServletOutputStream extends ServletOutputStream {
private final OutputStream out;
private final ServletOutputStream realSream;

/**
* Whether the stream is closed; implicitly initialized to false.
*/
private volatile boolean closed;

/**
* Object used to prevent a race on the 'closed' instance variable.
*/
private final Object closeLock = new Object();

/**
* Constructs a new {@link FilterOutputStream}.
* @param out the stream that sits above the realStream, performing some filtering. This must be eventually delegating eventual writes to {@code realStream}.
Expand Down Expand Up @@ -42,7 +52,36 @@ public void write(byte[] b, int off, int len) throws IOException {

@Override
public void close() throws IOException {
out.close();
if (closed) {
return;
}
synchronized (closeLock) {
if (closed) {
return;
}
closed = true;
}

Throwable flushException = null;
try {
flush();
} catch (Throwable e) {
flushException = e;
throw e;
} finally {
if (flushException == null) {
out.close();
} else {
try {
out.close();
} catch (Throwable closeException) {
if (flushException != closeException) {
closeException.addSuppressed(flushException);
}
throw closeException;
}
}
}
}

@Override
Expand Down