This is an automated email from the ASF dual-hosted git repository.

jbonofre pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/arrow-java.git


The following commit(s) were added to refs/heads/main by this push:
     new c0fb78e8e GH-448: Preserve error metadata when server middleware 
rejects calls (#1388)
c0fb78e8e is described below

commit c0fb78e8e615f012cd82680c10cd6b933e9cec07
Author: Tiefang Hu <[email protected]>
AuthorDate: Tue Nov 17 01:34:35 2026 +0800

    GH-448: Preserve error metadata when server middleware rejects calls (#1388)
    
    ## What's Changed
    
    When a server middleware factory rejects a call with a
    `FlightRuntimeException`, the interceptor closes the RPC with empty
    trailers. The client receives the status but loses any custom error
    metadata. Preserve those trailers through the existing Flight-to-gRPC
    exception conversion, without changing the status conversion or adding
    public API.
    
    Add real client/server regressions for the status, description, ASCII
    and binary metadata, and rejection before the producer is invoked. A
    control covers rejection without custom metadata. The metadata
    regression fails on the unchanged implementation and passes with the
    fix.
    
    Validation on JDK 21, with the repository-pinned `testing` submodule
    initialized:
    - `mvn -B -ntp -Dmaven.gitcommitid.nativegit=true -pl flight/flight-core
    -am verify` passed, including the dependency modules.
    - Flight Core: 27 test classes, 148 cases, 17 skipped, no failures or
    errors. All 10 middleware cases and all 4 TLS cases passed without
    skips.
    - Spotless and Checkstyle passed. The native Git option handles the
    existing Git plugin's local worktree compatibility; no build
    configuration was changed.
    
    Closes #448.
    
    Maintainer note: please add the `bug-fix` label; my account cannot add
    labels to this repository. The Test and Integration workflows currently
    show `action_required`; could you approve those runs when appropriate?
    Remote CI and maintainer review remain pending.
    
    <sub>AI-generated PR description. Investigation, implementation and
    automated review assisted by Codex and DeepSeek.</sub>
---
 .../flight/grpc/ServerInterceptorAdapter.java      |  7 +-
 .../apache/arrow/flight/TestServerMiddleware.java  | 85 ++++++++++++++++++++++
 2 files changed, 90 insertions(+), 2 deletions(-)

diff --git 
a/flight/flight-core/src/main/java/org/apache/arrow/flight/grpc/ServerInterceptorAdapter.java
 
b/flight/flight-core/src/main/java/org/apache/arrow/flight/grpc/ServerInterceptorAdapter.java
index e7a79b9f7..caa5b9cd5 100644
--- 
a/flight/flight-core/src/main/java/org/apache/arrow/flight/grpc/ServerInterceptorAdapter.java
+++ 
b/flight/flight-core/src/main/java/org/apache/arrow/flight/grpc/ServerInterceptorAdapter.java
@@ -100,8 +100,11 @@ public class ServerInterceptorAdapter implements 
ServerInterceptor {
       try {
         m = factory.factory.onCallStarted(info, headerAdapter, 
requestContextAdapter);
       } catch (FlightRuntimeException e) {
-        // Cancel call
-        call.close(StatusUtils.toGrpcStatus(e.status()), new Metadata());
+        // Cancel call, but forward any custom error metadata attached to the 
Flight status
+        // (GH-448).
+        final Metadata trailers = 
Status.trailersFromThrowable(StatusUtils.toGrpcException(e));
+        call.close(
+            StatusUtils.toGrpcStatus(e.status()), trailers == null ? new 
Metadata() : trailers);
         return new Listener<ReqT>() {};
       }
       middleware.add(m);
diff --git 
a/flight/flight-core/src/test/java/org/apache/arrow/flight/TestServerMiddleware.java
 
b/flight/flight-core/src/test/java/org/apache/arrow/flight/TestServerMiddleware.java
index c60843d2a..40fbf07eb 100644
--- 
a/flight/flight-core/src/test/java/org/apache/arrow/flight/TestServerMiddleware.java
+++ 
b/flight/flight-core/src/test/java/org/apache/arrow/flight/TestServerMiddleware.java
@@ -18,15 +18,19 @@ package org.apache.arrow.flight;
 
 import static org.apache.arrow.flight.FlightTestUtil.LOCALHOST;
 import static org.apache.arrow.flight.Location.forGrpcInsecure;
+import static org.junit.jupiter.api.Assertions.assertArrayEquals;
 import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertFalse;
 import static org.junit.jupiter.api.Assertions.assertNotNull;
 import static org.junit.jupiter.api.Assertions.assertNull;
+import static org.junit.jupiter.api.Assertions.assertTrue;
 import static org.junit.jupiter.api.Assertions.fail;
 
 import java.io.IOException;
 import java.util.Collections;
 import java.util.List;
 import java.util.concurrent.CompletableFuture;
+import java.util.concurrent.atomic.AtomicBoolean;
 import java.util.function.BiConsumer;
 import org.apache.arrow.flight.FlightClient.ClientStreamListener;
 import org.apache.arrow.memory.BufferAllocator;
@@ -87,6 +91,71 @@ public class TestServerMiddleware {
         });
   }
 
+  /**
+   * Make sure custom error metadata attached to a middleware rejection 
reaches the client (GH-448).
+   */
+  @Test
+  public void middlewareRejectionPreservesMetadata() {
+    final ErrorFlightMetadata metadata = new ErrorFlightMetadata();
+    metadata.insert("x-foo", "foo");
+    // Values with an embedded 0 and non-ASCII bytes must survive the round 
trip unchanged.
+    final byte[] binaryValue = new byte[] {0, (byte) 0x80, 'a'};
+    metadata.insert("x-bar-bin", binaryValue);
+    final AtomicBoolean producerCalled = new AtomicBoolean(false);
+    test(
+        new NoOpFlightProducer() {
+          @Override
+          public FlightInfo getFlightInfo(CallContext context, 
FlightDescriptor descriptor) {
+            producerCalled.set(true);
+            throw new AssertionError(
+                "Middleware rejection should prevent the producer from being 
invoked");
+          }
+        },
+        Collections.singletonList(
+            new ServerMiddlewarePair<>(
+                FlightServerMiddleware.Key.of("m"),
+                new RejectingFactory(
+                    CallStatus.UNAUTHENTICATED
+                        .withDescription("metadata rejection")
+                        .withMetadata(metadata)))),
+        (allocator, client) -> {
+          final CallStatus status =
+              FlightTestUtil.assertCode(
+                  FlightStatusCode.UNAUTHENTICATED,
+                  () -> client.getInfo(FlightDescriptor.path("test")));
+          assertEquals("metadata rejection", status.description());
+          final ErrorFlightMetadata errorMetadata = status.metadata();
+          // Assert presence first so a missing key fails clearly instead of 
throwing.
+          assertTrue(errorMetadata.containsKey("x-foo"));
+          assertTrue(errorMetadata.containsKey("x-bar-bin"));
+          assertEquals("foo", errorMetadata.get("x-foo"));
+          assertArrayEquals(binaryValue, errorMetadata.getByte("x-bar-bin"));
+          assertFalse(producerCalled.get());
+        });
+  }
+
+  /** Control case: a rejection without metadata still propagates code and 
description. */
+  @Test
+  public void middlewareRejectionWithoutMetadata() {
+    test(
+        new NoOpFlightProducer(),
+        Collections.singletonList(
+            new ServerMiddlewarePair<>(
+                FlightServerMiddleware.Key.of("m"),
+                new 
RejectingFactory(CallStatus.UNAVAILABLE.withDescription("plain rejection")))),
+        (allocator, client) -> {
+          final CallStatus status =
+              FlightTestUtil.assertCode(
+                  FlightStatusCode.UNAVAILABLE,
+                  () -> client.getInfo(FlightDescriptor.path("test")));
+          assertEquals("plain rejection", status.description());
+          // gRPC adds its own transport trailers (e.g. content-type), so only 
assert that no
+          // custom error metadata came through.
+          assertFalse(status.metadata().containsKey("x-foo"));
+          assertFalse(status.metadata().containsKey("x-bar-bin"));
+        });
+  }
+
   /** Make sure uncaught exceptions in DoPut are intercepted. */
   @Test
   public void doPutUncaught() {
@@ -232,6 +301,22 @@ public class TestServerMiddleware {
     }
   }
 
+  /** A middleware factory that rejects every call before the producer is 
invoked. */
+  static class RejectingFactory implements 
FlightServerMiddleware.Factory<FlightServerMiddleware> {
+
+    private final CallStatus status;
+
+    RejectingFactory(CallStatus status) {
+      this.status = status;
+    }
+
+    @Override
+    public FlightServerMiddleware onCallStarted(
+        CallInfo info, CallHeaders incomingHeaders, RequestContext context) {
+      throw status.toRuntimeException();
+    }
+  }
+
   /** A producer that throws the given exception on a call. */
   static class ErrorProducer extends NoOpFlightProducer {
 

Reply via email to