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: Close Jetty h2 streams with RST_STREAM and no error code #6424

Merged
merged 4 commits into from
Nov 26, 2024
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 @@ -17,20 +17,23 @@
import jakarta.servlet.WriteListener;

import java.io.IOException;
import java.util.concurrent.atomic.AtomicReference;

/**
* Wraps the usual ServletOutputStream so as to allow downstream writers to use it according to the servlet spec, but
* still make it easy to write trailers as a payload instead of using HTTP trailers at the end of a stream.
*/
public class GrpcWebOutputStream extends ServletOutputStream implements WriteListener {
private final ServletOutputStream wrapped;
private final GrpcWebServletResponse grpcWebServletResponse;

// Access to these are guarded by synchronized
private Runnable waiting;
private WriteListener writeListener;

public GrpcWebOutputStream(ServletOutputStream wrapped) {
public GrpcWebOutputStream(ServletOutputStream wrapped, GrpcWebServletResponse grpcWebServletResponse) {
this.wrapped = wrapped;
this.grpcWebServletResponse = grpcWebServletResponse;
}

@Override
Expand Down Expand Up @@ -97,7 +100,21 @@ public void flush() throws IOException {

@Override
public void close() throws IOException {
wrapped.close();
// Since we're a grpc-web response, we must write trailers on our way out as part of close - but trailers
// for grpc-web are a data frame, not HTTP trailers. Call up to the response to write the trailer frame,
// then close the underlying stream.
AtomicReference<IOException> exception = new AtomicReference<>();
grpcWebServletResponse.writeTrailers(() -> {
try {
wrapped.close();
} catch (IOException e) {
exception.set(e);
}
});
IOException ex = exception.get();
if (ex != null) {
throw ex;
}
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,8 @@ public AsyncContext startAsync() throws IllegalStateException {
public AsyncContext startAsync(ServletRequest servletRequest, ServletResponse servletResponse)
throws IllegalStateException {
AsyncContext delegate = super.startAsync(servletRequest, servletResponse);
// Note that this anonymous class has no purpose while our workaround for
// https://github.com/deephaven/deephaven-core/issues/6400 is in place.
return new DelegatingAsyncContext(delegate) {
private void safelyComplete() {
try {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ public Supplier<Map<String, String>> getTrailerFields() {
public synchronized GrpcWebOutputStream getOutputStream() throws IOException {
if (outputStream == null) {
// Provide our own output stream instance, so we can control/monitor the write listener
outputStream = new GrpcWebOutputStream(super.getOutputStream());
outputStream = new GrpcWebOutputStream(super.getOutputStream(), this);
}
return outputStream;
}
Expand Down