@@ -725,6 +725,7 @@ public void onHalfClose() {
725
725
String vModelId = null ;
726
726
String requestId = null ;
727
727
ModelResponse response = null ;
728
+ ByteBuf responsePayload = null ;
728
729
try (InterruptingListener cancelListener = newInterruptingListener ()) {
729
730
if (logHeaders != null ) {
730
731
logHeaders .addToMDC (headers ); // MDC cleared in finally block
@@ -767,18 +768,20 @@ public void onHalfClose() {
767
768
} finally {
768
769
if (payloadProcessor != null ) {
769
770
processPayload (reqMessage .readerIndex (reqReaderIndex ),
770
- requestId , resolvedModelId , methodName , headers , null , true );
771
+ requestId , resolvedModelId , methodName , headers , null );
771
772
} else {
772
773
releaseReqMessage ();
773
774
}
774
775
reqMessage = null ; // ownership released or transferred
775
776
}
776
777
777
- respReaderIndex = response .data .readerIndex ();
778
778
respSize = response .data .readableBytes ();
779
779
call .sendHeaders (response .metadata );
780
+ if (payloadProcessor != null ) {
781
+ responsePayload = response .data .retainedSlice ();
782
+ }
780
783
call .sendMessage (response .data );
781
- // response is released via ReleaseAfterResponse.releaseAll()
784
+ // final response refcount is released via ReleaseAfterResponse.releaseAll()
782
785
status = OK ;
783
786
} catch (Exception e ) {
784
787
status = toStatus (e );
@@ -795,17 +798,13 @@ public void onHalfClose() {
795
798
evictMethodDescriptor (methodName );
796
799
}
797
800
} finally {
798
- final boolean releaseResponse = status != OK ;
799
801
if (payloadProcessor != null ) {
800
- ByteBuf data = null ;
801
- Metadata metadata = null ;
802
- if (response != null ) {
803
- data = response .data .readerIndex (respReaderIndex );
804
- metadata = response .metadata ;
805
- }
806
- processPayload (data , requestId , resolvedModelId , methodName , metadata , status , releaseResponse );
807
- } else if (releaseResponse && response != null ) {
808
- response .release ();
802
+ Metadata metadata = response != null ? response .metadata : null ;
803
+ processPayload (responsePayload , requestId , resolvedModelId , methodName , metadata , status );
804
+ }
805
+ if (status != OK && response != null ) {
806
+ // An additional release is required if we call.sendMessage() wasn't sucessful
807
+ response .data .release ();
809
808
}
810
809
ReleaseAfterResponse .releaseAll ();
811
810
clearThreadLocals ();
@@ -820,23 +819,21 @@ public void onHalfClose() {
820
819
}
821
820
822
821
/**
823
- * Invoke PayloadProcessor on the request/response data
822
+ * Invoke PayloadProcessor on the request/response data. This method takes ownership
823
+ * of the passed-in {@code ByteBuf}.
824
+ *
824
825
* @param data the binary data
825
826
* @param payloadId the id of the request
826
827
* @param modelId the id of the model
827
828
* @param methodName the name of the invoked method
828
829
* @param metadata the method name metadata
829
830
* @param status null for requests, non-null for responses
830
- * @param takeOwnership whether the processor should take ownership
831
831
*/
832
832
private void processPayload (ByteBuf data , String payloadId , String modelId , String methodName ,
833
- Metadata metadata , io .grpc .Status status , boolean takeOwnership ) {
833
+ Metadata metadata , io .grpc .Status status ) {
834
834
Payload payload = null ;
835
835
try {
836
836
assert payloadProcessor != null ;
837
- if (!takeOwnership ) {
838
- ReferenceCountUtil .retain (data );
839
- }
840
837
payload = new Payload (payloadId , modelId , methodName , metadata , data , status );
841
838
if (payloadProcessor .process (payload )) {
842
839
data = null ; // ownership transferred
0 commit comments