Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Graph SDK 5 HTTP request failed with System.IO.InvalidDataException "invalid data while decoding" #2205

Closed
kyzhangZao opened this issue Nov 9, 2023 · 7 comments

Comments

@kyzhangZao
Copy link

To test the graph sdk upgrade change in our testing environment, all the graph calls failed with System.IO.InvalidDataException "invalid data while decoding"

One sample full traces on our end looks like:

[Exception:System.IO.InvalidDataException: Found invalid data while decoding.
at System.IO.Compression.DeflateStream.EndRead(IAsyncResult asyncResult)
at System.Net.Http.StreamToStreamCopy.StartRead()
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at Microsoft.Kiota.Http.HttpClientLibrary.HttpClientRequestAdapter.d__34.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at Microsoft.Kiota.Http.HttpClientLibrary.HttpClientRequestAdapter.d__201.MoveNext() --- End of stack trace from previous location where exception was thrown --- at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) at Microsoft.Graph.Subscriptions.SubscriptionsRequestBuilder.<PostAsync>d__5.MoveNext() --- End of stack trace from previous location where exception was thrown --- at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() at Polly.Retry.AsyncRetryEngine.<ImplementationAsync>d__01.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at Polly.AsyncPolicy.d__211.MoveNext() --- End of stack trace from previous location where exception was thrown --- at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) at System.Runtime.CompilerServices.ConfiguredTaskAwaitable1.ConfiguredTaskAwaiter.GetResult()

This error message is not super helpful, and we're not able to understand what's the exactly issue here. But it seems related with the Kiota HTTP dependency being introduced in the upgrade.
As making http request being mentioned https://github.com/microsoftgraph/msgraph-sdk-dotnet/blob/feature/5.0/docs/upgrade-to-v5.md#removal-of-request-from-the-fluent-api, I'm following this guidance. Hence need some hints of how to fix this issue.

@andrueastman
Copy link
Member

Thanks for raising this @kyzhangZao

Any chance you can provide some more information on the SDK version you are using and the sample code of the request you are making as well as how you are initializing the client?

@kyzhangZao
Copy link
Author

Hi @andrueastman , I'm using graph sdk Version="5.32.0"

A sample request of fetching group is like var group = await graphServiceClient.Groups[groupId].GetAsync().ConfigureAwait(false);

And for graph client initialization
We have AadAppAuthAccessTokenProvider that implements IAccessTokenProvider interface, and then use BaseBearerTokenAuthenticationProvider with the above access token as parameter.
We also have some customized handlers and create pipeline with those handlers. A HttpClient is being generated with the pipeline as one of the parameter.
We use this constructor
public GraphServiceClient( HttpClient httpClient, IAuthenticationProvider authenticationProvider = null, string baseUrl = null):this(new BaseGraphRequestAdapter(authenticationProvider ?? new AnonymousAuthenticationProvider(), graphClientOptions, httpClient: httpClient),baseUrl) { }

We're a 1p App, so the flow should be within each graph call, we'll include the bearer token issued by msGraph with the preauth permission we've requested.
Open to be reached out on Teams if that's easier for debugging.

@andrueastman
Copy link
Member

@kyzhangZao
Among your custom handlers, do you by any chance have customized compression handling? From the stack trace, it looks like the error is occurring due to the compression handler. Are you by any chance able to share how the HttpClient is configured as well?

@kyzhangZao
Copy link
Author

Hi @andrueastman , No we have no customization of compression handling.

var handlers = GraphClientFactory.CreateDefaultHandlers();
var redirectHandler = handlers.Single(handler => handler is RedirectHandler);
handlers.Remove(redirectHandler);
handlers.Add(new PollyHandler(this, traceableData));

These are the only modification we had on the handler.

@kyzhangZao
Copy link
Author

kyzhangZao commented Nov 10, 2023

The compression handler seems to be introduced in the sdk itself from its dependency Kiota. I just tried, directly remove the compression handler within our service setup actually fixed this issue. But would like to check if this is something we should do/would there be any consequences if we remove this handler?

@andrueastman
Copy link
Member

Hey @kyzhangZao,

Any chance you can confirm(or share a reference) if the PollyHandler is also trying to do some request compression/decompression or read the response content? If it does, it may mean that the error occurs due to it trying to be done twice by both handlers or the handler trying to read compressed content.

But would like to check if this is something we should do/would there be any consequences if we remove this handler?

Removing the CompressionHandler will result in the AcceptEncoding header not being sent so the API will not respond with compressed content. Therefore, you will lose the optimization but the SDK should still work.

Copy link
Contributor

This issue has been automatically marked as stale because it has been marked as requiring author feedback but has not had any activity for 4 days. It will be closed if no further activity occurs within 3 days of this comment.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants