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

HTTP client stream should handle a write to a stream which has been reset without having being allocated #5411

Merged
merged 1 commit into from
Dec 2, 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
30 changes: 26 additions & 4 deletions src/main/java/io/vertx/core/http/impl/Http1xClientConnection.java
Original file line number Diff line number Diff line change
Expand Up @@ -395,7 +395,7 @@ private static class StreamImpl extends Stream implements HttpClientStream {

private final Http1xClientConnection conn;
private final InboundBuffer<Object> queue;
private boolean reset;
private Throwable reset;
private boolean closed;
private Handler<HttpResponseHead> headHandler;
private Handler<Buffer> chunkHandler;
Expand All @@ -414,7 +414,7 @@ private static class StreamImpl extends Stream implements HttpClientStream {
this.conn = conn;
this.queue = new InboundBuffer<>(context, 5)
.handler(item -> {
if (!reset) {
if (reset == null) {
if (item instanceof MultiMap) {
Handler<MultiMap> handler = endHandler;
if (handler != null) {
Expand Down Expand Up @@ -526,7 +526,22 @@ public void writeHead(HttpRequestHead request, boolean chunked, ByteBuf buf, boo
writeHead(request, chunked, buf, end, connect, handler == null ? null : context.promise(handler));
}

private boolean checkReset(Handler<AsyncResult<Void>> handler) {
Throwable reset;
synchronized (this) {
reset = this.reset;
if (reset == null) {
return false;
}
}
handler.handle(context.failedFuture(reset));
return true;
}

private void writeHead(HttpRequestHead request, boolean chunked, ByteBuf buf, boolean end, boolean connect, Handler<AsyncResult<Void>> handler) {
if (checkReset(handler)) {
return;
}
EventLoop eventLoop = conn.context.nettyEventLoop();
synchronized (this) {
if (shouldQueue(eventLoop)) {
Expand All @@ -537,12 +552,19 @@ private void writeHead(HttpRequestHead request, boolean chunked, ByteBuf buf, bo
return;
}
}
if (reset != null) {
handler.handle(context.failedFuture(reset));
return;
}
((Stream)this).request = request;
conn.beginRequest(this, request, chunked, buf, end, connect, handler);
}

@Override
public void writeBuffer(ByteBuf buff, boolean end, Handler<AsyncResult<Void>> handler) {
if (checkReset(handler)) {
return;
}
if (buff != null || end) {
FutureListener<Void> listener = handler == null ? null : context.promise(handler);
writeBuffer(buff, end, listener);
Expand Down Expand Up @@ -636,10 +658,10 @@ public void doFetch(long amount) {
@Override
public void reset(Throwable cause) {
synchronized (conn) {
if (reset) {
if (reset != null) {
return;
}
reset = true;
reset = Objects.requireNonNull(cause);
}
EventLoop eventLoop = conn.context.nettyEventLoop();
if (eventLoop.inEventLoop()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -546,6 +546,9 @@ void handleException(Throwable exception) {

@Override
public void writeHead(HttpRequestHead request, boolean chunked, ByteBuf buf, boolean end, StreamPriority priority, boolean connect, Handler<AsyncResult<Void>> handler) {
if (checkReset(handler)) {
return;
}
priority(priority);
ContextInternal ctx = conn.getContext();
EventLoop eventLoop = ctx.nettyEventLoop();
Expand Down Expand Up @@ -629,6 +632,9 @@ private void createStream(HttpRequestHead head, Http2Headers headers) throws Htt

@Override
public void writeBuffer(ByteBuf buf, boolean end, Handler<AsyncResult<Void>> listener) {
if (checkReset(listener)) {
return;
}
if (buf != null) {
int size = buf.readableBytes();
synchronized (this) {
Expand Down
21 changes: 21 additions & 0 deletions src/main/java/io/vertx/core/http/impl/VertxHttp2Stream.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import io.vertx.core.buffer.Buffer;
import io.vertx.core.http.HttpFrame;
import io.vertx.core.http.StreamPriority;
import io.vertx.core.http.StreamResetException;
import io.vertx.core.http.impl.headers.Http2HeadersAdaptor;
import io.vertx.core.impl.ContextInternal;
import io.vertx.core.impl.VertxInternal;
Expand All @@ -49,6 +50,7 @@ abstract class VertxHttp2Stream<C extends Http2ConnectionBase> {
private long bytesWritten;
private int writeInProgress = 0;
protected boolean isConnect;
private long reset = -1L;

VertxHttp2Stream(C conn, ContextInternal context) {
this.conn = conn;
Expand Down Expand Up @@ -178,7 +180,22 @@ private void doWriteFrame(int type, int flags, ByteBuf payload) {
conn.handler.writeFrame(stream, (byte) type, (short) flags, payload);
}

protected final boolean checkReset(Handler<AsyncResult<Void>> handler) {
long reset;
synchronized (this) {
reset = this.reset;
if (reset == -1L) {
return false;
}
}
handler.handle(context.failedFuture(new StreamResetException(reset)));
return true;
}

final void writeHeaders(Http2Headers headers, boolean end, boolean checkFlush, Handler<AsyncResult<Void>> handler) {
if (checkReset(handler)) {
return;
}
EventLoop eventLoop = conn.getContext().nettyEventLoop();
synchronized (this) {
if (shouldQueue(eventLoop)) {
Expand All @@ -205,6 +222,9 @@ private void writePriorityFrame(StreamPriority priority) {
}

final void writeData(ByteBuf chunk, boolean end, Handler<AsyncResult<Void>> handler) {
if (checkReset(handler)) {
return;
}
ContextInternal ctx = conn.getContext();
EventLoop eventLoop = ctx.nettyEventLoop();
synchronized (this) {
Expand Down Expand Up @@ -260,6 +280,7 @@ protected void doWriteReset(long code) {
int streamId;
synchronized (this) {
streamId = stream != null ? stream.id() : -1;
reset = code;
}
if (streamId != -1) {
conn.handler.writeReset(streamId, code);
Expand Down
14 changes: 14 additions & 0 deletions src/test/java/io/vertx/core/http/HttpTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -5854,6 +5854,20 @@ public void testResetClientRequestResponseInProgress() throws Exception {
await();
}

@Test
public void testResetPartialClientRequest() throws Exception {
server.requestHandler(req -> {
});
startServer(testAddress);
client.request(requestOptions).onComplete(onSuccess(req -> {
assertTrue(req.reset());
req.end("body").onComplete(onFailure(err -> {
testComplete();
}));
}));
await();
}

@Test
public void testSimpleCookie() throws Exception {
testCookies("foo=bar", req -> {
Expand Down
Loading