Cysharp / GrpcWebSocketBridge

Yet Another gRPC over HTTP/1 using WebSocket implementation, primarily targets .NET platform.
MIT License
82 stars 12 forks source link

GrpcWebSocketBridge: Buffer is not large enough for header #26

Closed Sen-Gupta closed 7 months ago

Sen-Gupta commented 9 months ago

Hello Team

The issue still occurs.

Environment: .Net 8, MagicOnion 6.0, GrpcWebSocketBridge 6.0 Network: Client->Cloudflare-Firewall->Proxy-Gprc

Observations: We see MagicOnion returning a success message Request finished HTTP/2 POST https://www.fixme.com/IPortfoliosGrpcService/GetAllEntitiesAsync - 200 null application/grpc 20.9603ms

The grpc websocket bridge seems to be broken at the hightlighted code below.

ExceptionDetail.InnerException.Message Buffer is not large enough for header ExceptionDetail.InnerException.Source GrpcWebSocketBridge.Client ExceptionDetail.InnerException.TargetSite Void MoveNext() ExceptionDetail.InnerException.Type System.InvalidOperationException ExceptionDetail.Message Status(StatusCode="Internal", Detail="Error starting gRPC call. InvalidOperationException: Buffer is not large enough for header", DebugException="System.InvalidOperationException: Buffer is not large enough for header") ExceptionDetail.Source System.Private.CoreLib ExceptionDetail.Status._typeTag Status ExceptionDetail.Status.DebugException._typeTag InvalidOperationException ExceptionDetail.Status.DebugException.Data [] ExceptionDetail.Status.DebugException.HelpLink

ExceptionDetail.Status.DebugException.HResult -2146233079 ExceptionDetail.Status.DebugException.InnerException

ExceptionDetail.Status.DebugException.Message Buffer is not large enough for header ExceptionDetail.Status.DebugException.Source GrpcWebSocketBridge.Client ExceptionDetail.Status.DebugException.StackTrace at Grpc.Net.Client.Web.Internal.GrpcWebResponseStream.ReadAsync(Memory1 data, CancellationToken cancellationToken) at Grpc.Net.Client.Internal.StreamExtensions.ReadMessageAsync[TResponse](Stream responseStream, GrpcCall call, Func2 deserializer, String grpcEncoding, Boolean singleMessage, CancellationToken cancellationToken) at Grpc.Net.Client.Internal.GrpcCall2.ReadMessageAsync(Stream responseStream, String grpcEncoding, Boolean singleMessage, CancellationToken cancellationToken) at Grpc.Net.Client.Internal.GrpcCall2.RunCall(HttpRequestMessage request, Nullable`1 timeout) ExceptionDetail.Status.DebugException.TargetSite Void MoveNext() ExceptionDetail.Status.Detail Error starting gRPC call. InvalidOperationException: Buffer is not large enough for header ExceptionDetail.Status.StatusCode Internal ExceptionDetail.StatusCode Internal ExceptionDetail.TargetSite Void Throw() ExceptionDetail.Trailers []

licentia88 commented 9 months ago

I have come across this issue too, this exception is thrown in the latest version (version 6)

I've downgraded to the following version and it works.

MagicOnion.Client 5.1.8 MagicOnion.Shared 5.1.8 MagicOnion.Server 5.1.8 MagicOnion.Server.HttpGateway 5.1.8 MagicOnion.Serialization.MemoryPack 5.1.8-preview

KAW0 commented 8 months ago

I have also encountered that issue, but the solution is simple. Change:

 var channel = GrpcChannel.ForAddress(address, new GrpcChannelOptions()
                {
                    HttpHandler = new GrpcWebSocketBridgeHandler()
                });

To:

 var channel = GrpcChannel.ForAddress(address, new GrpcChannelOptions()
                {
                    HttpHandler = new GrpcWebSocketBridgeHandler(true)
                });
Sen-Gupta commented 7 months ago

@KAW0,

Thanks it works as expected with the changes.

KAW0 commented 7 months ago

While this solve this problem I don't think that issue is fully resolved since this parameter is undocumented. It easy to just follow basic example from Readme without realizing that this is and why it is needed. @mayuki

Sen-Gupta commented 7 months ago

Actually, it is partial solution. The setting won't work in MAUI project.

There were more changes required to make it work on Android.

I will document it here in a while.

Sent from Outlook for Androidhttps://aka.ms/AAb9ysg


From: KAW0 @.> Sent: Monday, April 8, 2024 3:21:00 PM To: Cysharp/GrpcWebSocketBridge @.> Cc: Sen Gupta @.>; State change @.> Subject: Re: [Cysharp/GrpcWebSocketBridge] GrpcWebSocketBridge: Buffer is not large enough for header (Issue #26)

While this solve this problem I don't think that issue is fully resolved since this parameter is undocumented. It easy to just follow basic example from Readme without realizing that this is and why it is needed. @neuecchttps://github.com/neuecc

— Reply to this email directly, view it on GitHubhttps://github.com/Cysharp/GrpcWebSocketBridge/issues/26#issuecomment-2042325116, or unsubscribehttps://github.com/notifications/unsubscribe-auth/AB5R3ZWK6EAVHY7MTUOLNKLY4JSAJAVCNFSM6AAAAABC7ZHVM2VHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMZDANBSGMZDKMJRGY. You are receiving this because you modified the open/close state.Message ID: @.***>

mayuki commented 7 months ago

We just released version 1.3.0. Could you please check if the problem occurs in the new version?

Sen-Gupta commented 7 months ago

Here is my test results.

Environment: Windows 11. (All nugets latest, magiconion and grpcwebsocketbridge. Application: Net MAUI Blazor Hybrid

Connection from Windows: Successful with new upgrade.

Case 1: Connecting from Windows MAUI App to MagicOnionGrpcServer

Channel Configuration code:

GrpcChannel channel?;
 if (OperatingSystem.IsWindows())
 {
     var httpHandler = new GrpcWebSocketBridgeHandler();  **//Note there was no need to forceWebSocketMode, though it works with that as sell**
     channel = GrpcChannel.ForAddress(baseUri, new GrpcChannelOptions
     {
         HttpHandler = httpHandler,
         MaxReceiveMessageSize = 20 * 1024 * 1024, // 10 MB
         MaxSendMessageSize = 10 * 1024 * 1024, // 10 MB
     });
 }

Results: Working as expected.

Case 2: Connecting from Android MAUI App to MagicOnionGrpcServer (Same App but running Android Build)

if (OperatingSystem.IsAndroid())
{
    var httpHandler = new GrpcWebSocketBridgeHandler(true); //Does not work with any either true or false for forceWebSocket
    channel = GrpcChannel.ForAddress(baseUri, new GrpcChannelOptions
    {
        HttpHandler = httpHandler,
        MaxReceiveMessageSize = 20 * 1024 * 1024, // 10 MB
        MaxSendMessageSize = 10 * 1024 * 1024, // 10 MB
    });
}
[chromium] [INFO:CONSOLE(1)] "The channel configuration isn't valid on Android devices. The channel is configured to use HttpClientHandler and Android's native HTTP/2 library. gRPC isn't fully supported by Android's native HTTP/2 library and it can cause runtime errors. To fix this problem, either configure the channel to use SocketsHttpHandler, or add <UseNativeHttpHandler>false</UseNativeHttpHandler> to the app's project file. For more information, see https://aka.ms/aspnet/grpc/android.
[chromium]    at Grpc.Net.Client.GrpcChannel.CreateInternalHttpInvoker(HttpMessageHandler handler) in /_/src/Grpc.Net.Client/GrpcChannel.cs:line 501
[chromium]    at Grpc.Net.Client.GrpcChannel..ctor(Uri address, GrpcChannelOptions channelOptions) in /_/src/Grpc.Net.Client/GrpcChannel.cs:line 161
[chromium]    at Grpc.Net.Client.GrpcChannel.ForAddress(Uri address, GrpcChannelOptions channelOptions) in /_/src/Grpc.Net.Client/GrpcChannel.cs:line 697
[chromium]    at Grpc.Net.Client.GrpcChannel.ForAddress(String address, GrpcChannelOptions channelOptions) in /_/src/Grpc.Net.Client/GrpcChannel.cs:line 667
[chromium]    at Insights.Services.Client.NativeGrpcClientChannel.GetGrpcChannel(String baseUri) in D:\Trade\InsightsOnDaprAspire\src\Services\Client\NativeGrpcClientChannel.cs:line 45
[chromium]    at Insights.Core.Client.Grpc.GrpcClients.EquitiesGrpcServiceClient.GetClient() in D:\Trade\InsightsOnDaprAspire\src\Core\Client\Grpc\GrpcClients\EquitiesGrpcServiceClient.cs:line 68
[chromium]    at Insights.Core.Client.Grpc.GrpcClients.EquitiesGrpcServiceClient..ctor(IGrpcClient nativeGrpcClient, IConfiguration configuration, ILogger`1 logger, IBlazorUserManager blazorUserManager) in D:\Trade\InsightsOnDaprAspire\src\Core\Client\Grpc\GrpcClients\EquitiesGrpcServiceClient.cs:line 31
[chromium]    at System.Reflection.MethodBaseInvoker.InterpretedInvoke_Constructor(Object obj, IntPtr* args)
[chromium]    at System.Reflection.MethodBaseInvoker.InvokeDirectByRefWithFewArgs(Object obj, Span`1 copyOfArgs, BindingFlags invokeAttr)
[chromium]    at System.Reflection.MethodBaseInvoker.InvokeWithFewArgs(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture)
[chromium]    at System.Reflection.RuntimeConstructorInfo.Invoke(BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture)
[chromium]    at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteRuntimeResolver.VisitConstructor(ConstructorCallSite constructorCallSite, RuntimeResolverContext context)
[chromium]    at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteVisitor`2[[Microsoft.Extensions.DependencyInjection.ServiceLookup.RuntimeResolverContext, Microsoft.Extensions.DependencyInjection, Version=8.0.0.0, Culture=neutral, PublicKeyToken=adb9793829ddae60],[System.Object, System.Private.CoreLib, Version=8.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e]].VisitCallSiteMain(ServiceCallSite callSite, RuntimeResolverContext argument)
[chromium]    at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteRuntimeResolver.VisitRootCache(ServiceCallSite callSite, RuntimeResolverContext context)
[chromium]    at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteRuntimeResolver.VisitScopeCache(ServiceCallSite callSite, RuntimeResolverContext context)
[chromium]    at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteVisitor`2[[Microsoft.Extensions.DependencyInjection.ServiceLookup.RuntimeResolverContext, Microsoft.Extensions.DependencyInjection, Version=8.0.0.0, Culture=neutral, PublicKeyToken=adb9793829ddae60],[System.Object, System.Private.CoreLib, Version=8.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e]].VisitCallSite(ServiceCallSite callSite, RuntimeResolverContext argument)
[chromium]    at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteRuntimeResolver.VisitConstructor(ConstructorCallSite constructorCallSite, RuntimeResolverContext context)
[chromium]    at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteVisitor`2[[Microsoft.Extensions.DependencyInjection.ServiceLookup.RuntimeResolverContext, Microsoft.Extensions.DependencyInjection, Version=8.0.0.0, Culture=neutral, PublicKeyToken=adb9793829ddae60],[System.Object, System.Private.CoreLib, Version=8.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e]].VisitCallSiteMain(ServiceCallSite callSite, RuntimeResolverContext argument)
[chromium]    at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteRuntimeResolver.VisitRootCache(ServiceCallSite callSite, RuntimeResolverContext context)
[chromium]    at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteVisitor`2[[Microsoft.Extensions.DependencyInjection.ServiceLookup.RuntimeResolverContext, Microsoft.Extensions.DependencyInjection, Version=8.0.0.0, Culture=neutral, PublicKeyToken=adb9793829ddae60],[System.Object, System.Private.CoreLib, Version=8.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e]].VisitCallSite(ServiceCallSite callSite, RuntimeResolverContext argument)
[chromium]    at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteRuntimeResolver.VisitConstructor(ConstructorCallSite constructorCallSite, RuntimeResolverContext context)
[chromium]    at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteVisitor`2[[Microsoft.Extensions.DependencyInjection.ServiceLookup.RuntimeResolverContext, Microsoft.Extensions.DependencyInjection, Version=8.0.0.0, Culture=neutral, PublicKeyToken=adb9793829ddae60],[System.Object, System.Private.CoreLib, Version=8.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e]].VisitCallSiteMain(ServiceCallSite callSite, RuntimeResolverContext argument)
[chromium]    at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteRuntimeResolver.VisitRootCache(ServiceCallSite callSite, RuntimeResolverContext context)
[chromium]    at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteVisitor`2[[Microsoft.Extensions.DependencyInjection.ServiceLookup.RuntimeResolverContext, Microsoft.Extensions.DependencyInjection, Version=8.0.0.0, Culture=neutral, PublicKeyToken=adb9793829ddae60],[System.Object, System.Private.CoreLib, Version=8.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e]].VisitCallSite(ServiceCallSite callSite, RuntimeResolverContext argument)
[chromium]    at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteRuntimeResolver.Resolve(ServiceCallSite callSite, ServiceProviderEngineScope scope)
[chromium]    at Microsoft.Extensions.DependencyInjection.ServiceProvider.CreateServiceAccessor(ServiceIdentifier serviceIdentifier)
[chromium]    at System.Collections.Concurrent.ConcurrentDictionary`2[[Microsoft.Extensions.DependencyInjection.ServiceLookup.ServiceIdentifier, Microsoft.Extensions.DependencyInjection, Version=8.0.0.0, Culture=neutral, PublicKeyToken=adb9793829ddae60],[Microsoft.Extensions.DependencyInjection.ServiceProvider.ServiceAccessor, Microsoft.Extensions.DependencyInjection, Version=8.0.0.0, Culture=neutral, PublicKeyToken=adb9793829ddae60]].GetOrAdd(ServiceIdentifier key, Func`2 valueFactory)
[chromium]    at Microsoft.Extensions.DependencyInjection.ServiceProvider.GetService(ServiceIdentifier serviceIdentifier, ServiceProviderEngineScope serviceProviderEngineScope)
[chromium]    at Microsoft.Extensions.DependencyInjection.ServiceLookup.ServiceProviderEngineScope.GetService(Type serviceType)
[chromium]    at Microsoft.AspNetCore.Components.ComponentFactory.<>c__DisplayClass9_0.<CreatePropertyInjector>g__Initialize|1(IServiceProvider serviceProvider, IComponent component)
[chromium]    at Microsoft.AspNetCore.Components.ComponentFactory.InstantiateComponent(IServiceProvider serviceProvider, Type componentType, IComponentRenderMode callerSpecifiedRenderMode, Nullable`1 parentComponentId)
[chromium]    at Microsoft.AspNetCore.Components.RenderTree.Renderer.InstantiateChildComponentOnFrame(RenderTreeFrame[] frames, Int32 frameIndex, Int32 parentComponentId)
[chromium]    at Microsoft.AspNetCore.Components.RenderTree.RenderTreeDiffBuilder.InitializeNewComponentFrame(DiffContext& diffContext, Int32 frameIndex)
[chromium]    at Microsoft.AspNetCore.Components.RenderTree.RenderTreeDiffBuilder.InitializeNewSubtree(DiffContext& diffContext, Int32 frameIndex)
[chromium]    at Microsoft.AspNetCore.Components.RenderTree.RenderTreeDiffBuilder.InsertNewFrame(DiffContext& diffContext, Int32 newFrameIndex)
[chromium]    at Microsoft.AspNetCore.Components.RenderTree.RenderTreeDiffBuilder.AppendDiffEntriesForRange(DiffContext& diffContext, Int32 oldStartIndex, Int32 oldEndIndexExcl, Int32 newStartIndex, Int32 newEndIndexExcl)
[chromium]    at Microsoft.AspNetCore.Components.RenderTree.RenderTreeDiffBuilder.ComputeDiff(Renderer renderer, RenderBatchBuilder batchBuilder, Int32 componentId, ArrayRange`1 oldTree, ArrayRange`1 newTree)
[chromium]    at Microsoft.AspNetCore.Components.Rendering.ComponentState.RenderIntoBatch(RenderBatchBuilder batchBuilder, RenderFragment renderFragment, Exception& renderFragmentException)
[chromium]    at Microsoft.AspNetCore.Components.RenderTree.Renderer.RenderInExistingBatch(RenderQueueEntry renderQueueEntry)
[chromium]    at Microsoft.AspNetCore.Components.RenderTree.Renderer.ProcessRenderQueue()", source: https://0.0.0.0/_framework/blazor.webview.js (1)
[GPUAUX] [AUX]GuiExtAuxCheckAuxPath:663: Null anb

Result: Does not work on MAUI Android App. with above configuration.

Workaround/Perhaps right way to create channel specific to MAUI

After a little research and guidance from a reply of @JamesNK, I figured out that correct way to create a channel on MAUI app should be.

if (OperatingSystem.IsAndroid())
{
    var httpHandler = new GrpcWebSocketBridgeHandler(true); //Works with both true/false
    channel = GrpcChannel.ForAddress(baseUri, new GrpcChannelOptions
    {
        HttpClient = new HttpClient(httpHandler),   //Observe that we are passing HttpClient with GrpcWebSocketBridgeHandler
        MaxReceiveMessageSize = 20 * 1024 * 1024, // 10 MB
        MaxSendMessageSize = 10 * 1024 * 1024, // 10 MB
    });
}

I belive current build 1.3 has fixed the existing issues and the issue specific to MAUI relates to the way we need to provision the channel is specific to Android.

Thanks for such great tools and libraries.

Love from New Delhi!

mayuki commented 7 months ago

I have forwarded it to a separate Issue because it is a different problem than this one that has been reported.