Skip to content
Original file line number Diff line number Diff line change
Expand Up @@ -663,9 +663,33 @@ private void doOnData(ByteBuf data, boolean endStream) {
handleH2TransportError(TriRpcStatus.INTERNAL.withDescription("headers not received before payload"));
return;
}
// Use ByteBufInputStream to adapt ByteBuf to InputStream
// The second parameter 'true' means release ByteBuf after reading
deframer.decode(new ByteBufInputStream(data, true));
// Use ByteBufInputStream to adapt ByteBuf to InputStream.
// The second parameter 'true' means release ByteBuf when the stream is closed.
// NOTE: We must NOT close this stream here because deframer holds a reference
// to it via accumulate.addInputStream(). The stream will be closed later (releasing
// the underlying ByteBuf) when accumulate.close() is called via deframer.close().
// Closing it here would prematurely invalidate data that deframer still needs
// for subsequent deserialization, causing "Deserialize response failed".
try {
deframer.decode(new ByteBufInputStream(data, true));
} catch (Throwable t) {
// decode() threw — e.g. DecodeException from invalid gRPC length field.
// We must immediately close the accumulate to release the ByteBuf that was
// added via accumulate.addInputStream() before decode() failed.
// deframer.close() takes a deferred (stalled-aware) path which never executes
// in the decode-failure case because state stays HEADER; onStreamClosed()
// calls accumulate.close() directly and now, releasing the ByteBuf.
try {
deframer.onStreamClosed();
} catch (Exception ignore) {
// accumulate.close() may throw; safe to ignore — ByteBuf is released
}
// Route through handleH2TransportError so the caller is notified instead
// of silently waiting until request timeout.
handleH2TransportError(TriRpcStatus.INTERNAL
.withDescription("Failed to decode triple response data: " + t.getMessage())
.withCause(t));
}
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -314,4 +314,65 @@ private static TriRpcStatus receiveErrorTrailers(String grpcMessage, String deta
stream.createTransportListener().onHeader(trailers, true);
return listener.status;
}
}

@Test
public void testDecodeFailureReleasesByteBufAndNotifiesCaller() {
// Regression test for #16587:
// When deframer.decode() throws (e.g. negative gRPC length field),
// the ByteBuf must be released and the error must propagate to the caller.
// Previously deframer.close() took a deferred path and never ran in this case,
// leaking the ByteBuf. Now deframer.onStreamClosed() closes accumulate directly.

TripleWriteQueue writeQueue = mock(TripleWriteQueue.class);
when(writeQueue.enqueueFuture(any(QueuedCommand.class), any(Executor.class)))
.thenReturn(new CompletableFuture<>());
when(writeQueue.enqueue(any(QueuedCommand.class)))
.thenReturn(new CompletableFuture<>());

TripleClientStreamListener listener = new TripleClientStreamListener();
StreamObserver<Object> streamObserver = new StreamObserver<>() {
@Override public void onNext(Object data) {}
@Override public void onError(Throwable t) {}
@Override public void onCompleted() {}
@Override public void setCompletableFuture(CompletableFuture<Object> completableFuture) {}
};
ClientStream stream = new Http2TripleClientStream(
ApplicationModel.defaultModel(),
Runnable::run,
writeQueue,
listener,
streamObserver,
new SocketStub(),
new Metadata(),
new Metadata(),
Compressor.NONE);
stream.initStream();

H2TransportListener transportListener = stream.createTransportListener();

// Send valid headers first
DefaultHttp2Headers headers = new DefaultHttp2Headers();
headers.scheme(HttpScheme.HTTP.name()).status(HttpResponseStatus.OK.codeAsText());
headers.set(TripleHeaderEnum.STATUS_KEY.getKey(), TriRpcStatus.OK.code.code + "");
headers.set(HttpHeaderNames.CONTENT_TYPE.getKey(), MediaType.APPLICATION_GRPC_PROTO.getName());
transportListener.onHeader(headers, false);
assertTrue(listener.started);

stream.request(2);

// DATA frame with NEGATIVE gRPC length field (0x80000000 = -2147483648)
// This makes LengthFieldStreamingDecoder.processHeader() throw RpcException
byte[] corruptData = new byte[] {0, (byte)0x80, 0, 0, 1};
final ByteBuf buf = Unpooled.wrappedBuffer(corruptData);
assertEquals(1, buf.refCnt());

transportListener.onData(buf, false);

// ByteBuf MUST be released (refCnt == 0) after decode failure
assertEquals(0, buf.refCnt(), "ByteBuf should be released after decode failure");

// Caller MUST be notified via listener.onComplete (not silently timeout)
assertTrue(listener.completed, "listener.onComplete should be called on decode failure");
assertNotNull(listener.status, "listener.status should be set on decode failure");
}
}
Loading