forked from line/armeria
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix a bug where
INTERNAL_ERROR
is returned when gRPC message length…
… is exceeded (line#5824) Motivation: If a request or response message exceeds the maximum allowed message length, `RESOURCE_EXHAUSTED` status should be returned to the client. In `UnaryServerCall`, an exception raised `ArmeriaMessageFramer` is not handled by `GrpcExceptionHandlerFunction`. As a result, the exception is directly passed to the HTTP level `ServerErrorHandler` and a proper gRPC status is not delivered to the client. While I was fixing the bug, I found a lot of code that calls `generateMetadataFromThrowable()` and then `fromThrowable()`. As the same pattern was used repeatedly, I also refactored `GrpcExceptionHandlerFunctionUtil` to return `StatusAndMetata` instead. Modifications: - Apply `GrpcExceptionHandlerFunction` when an exception is raised in `UnaryServerCall.doClose()` - Refactor `GrpcExceptionHandlerFunctionUtil` and its calling site. - Add `InternalGrpcExceptionHandler` to wrap `GrpcExceptionHandlerFunction`. - Remove the incorrect `@Nullable` annotation for `GrpcExceptionHandlerFunction`. - Remove `ServerStatusAndMetadata.setResponseContent()` and use `RESPONSE_CONTENT` property for simpliciy. Result: - `GrpcService` now correctly returns `RESOURCE_EXHAUSTED` when a response message exceeds the maximum allowed length. - Closes line#5818
- Loading branch information
Showing
18 changed files
with
415 additions
and
231 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
68 changes: 0 additions & 68 deletions
68
...main/java/com/linecorp/armeria/internal/common/grpc/GrpcExceptionHandlerFunctionUtil.java
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
87 changes: 87 additions & 0 deletions
87
...src/main/java/com/linecorp/armeria/internal/common/grpc/InternalGrpcExceptionHandler.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
/* | ||
* Copyright 2024 LINE Corporation | ||
* | ||
* LINE Corporation licenses this file to you under the Apache License, | ||
* version 2.0 (the "License"); you may not use this file except in compliance | ||
* with the License. You may obtain a copy of the License at: | ||
* | ||
* https://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT | ||
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the | ||
* License for the specific language governing permissions and limitations | ||
* under the License. | ||
*/ | ||
|
||
package com.linecorp.armeria.internal.common.grpc; | ||
|
||
import static java.util.Objects.requireNonNull; | ||
|
||
import com.linecorp.armeria.common.RequestContext; | ||
import com.linecorp.armeria.common.grpc.GrpcExceptionHandlerFunction; | ||
import com.linecorp.armeria.common.grpc.protocol.ArmeriaStatusException; | ||
import com.linecorp.armeria.common.util.Exceptions; | ||
|
||
import io.grpc.Metadata; | ||
import io.grpc.Status; | ||
import io.grpc.Status.Code; | ||
import io.grpc.StatusException; | ||
import io.grpc.StatusRuntimeException; | ||
|
||
public final class InternalGrpcExceptionHandler { | ||
|
||
private final GrpcExceptionHandlerFunction delegate; | ||
|
||
public InternalGrpcExceptionHandler(GrpcExceptionHandlerFunction delegate) { | ||
this.delegate = delegate; | ||
} | ||
|
||
public StatusAndMetadata handle(RequestContext ctx, Throwable t) { | ||
final Throwable peeled = peelAndUnwrap(t); | ||
Metadata metadata = Status.trailersFromThrowable(peeled); | ||
if (metadata == null) { | ||
metadata = new Metadata(); | ||
} | ||
Status status = Status.fromThrowable(peeled); | ||
status = handle0(ctx, status, peeled, metadata); | ||
return new StatusAndMetadata(status, metadata); | ||
} | ||
|
||
public Status handle(RequestContext ctx, Status status, Throwable cause, Metadata metadata) { | ||
final Throwable peeled = peelAndUnwrap(cause); | ||
return handle0(ctx, status, peeled, metadata); | ||
} | ||
|
||
private Status handle0(RequestContext ctx, Status status, Throwable cause, Metadata metadata) { | ||
if (status.getCode() == Code.UNKNOWN) { | ||
// If ArmeriaStatusException is thrown, it is converted to UNKNOWN and passed through close(Status). | ||
// So try to restore the original status. | ||
Status newStatus = null; | ||
if (cause instanceof StatusRuntimeException) { | ||
newStatus = ((StatusRuntimeException) cause).getStatus(); | ||
} else if (cause instanceof StatusException) { | ||
newStatus = ((StatusException) cause).getStatus(); | ||
} | ||
if (newStatus != null && newStatus.getCode() != Code.UNKNOWN) { | ||
status = newStatus; | ||
} | ||
} | ||
status = delegate.apply(ctx, status, cause, metadata); | ||
assert status != null; | ||
return status; | ||
} | ||
|
||
private static Throwable peelAndUnwrap(Throwable t) { | ||
requireNonNull(t, "t"); | ||
t = Exceptions.peel(t); | ||
Throwable cause = t; | ||
while (cause != null) { | ||
if (cause instanceof ArmeriaStatusException) { | ||
return StatusExceptionConverter.toGrpc((ArmeriaStatusException) cause); | ||
} | ||
cause = cause.getCause(); | ||
} | ||
return t; | ||
} | ||
} |
Oops, something went wrong.