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

[Bug Report] ModuleClient.CreateFromEnvironmentAsync throws an error running inside a .Net 5 ASP.NET Core app #1804

Closed
shaeussler opened this issue Feb 25, 2021 · 2 comments · Fixed by #1816
Assignees
Labels
bug Something isn't working. IoTSDK Tracks all IoT SDK issues across the board

Comments

@shaeussler
Copy link

Context

  • OS, version, SKU and CPU architecture used: Windows 10 1809 Desktop x64
  • Application's .NET Target Framework : net5.0
  • Device: Azure Win10 1809 VM
  • SDK version used: Microsoft.Azure.Devices.Client" Version 1.35.0

Description of the issue

Running a ASP.NET Core app inside Azure IoT Edge:
ModuleClient.CreateFromEnvironmentAsync throws an error running inside a .Net 5 ASP.NET Core app

Code sample exhibiting the issue

   public class Program
   {
      public static async Task Main(string[] args)
      {
         var host = CreateHostBuilder(args).Build();
         var transportSetting = new MqttTransportSettings(TransportType.Mqtt_Tcp_Only);

         ITransportSettings[] ts = { transportSetting };
         var client = await ModuleClient.CreateFromEnvironmentAsync();

         var twin = client.GetTwinAsync().GetAwaiter().GetResult();

         Console.WriteLine($"Twin.Properties.Desired.ToJson : {twin.Properties.Desired.ToJson()}");

         await host.RunAsync();
      }

      public static IHostBuilder CreateHostBuilder(string[] args) =>
          Host.CreateDefaultBuilder(args)
              .ConfigureWebHostDefaults(webBuilder =>
              {
                 webBuilder.UseStartup<Startup>();
              });
   }

<Project Sdk="Microsoft.NET.Sdk.Web">

  <PropertyGroup>
    <TargetFramework>net5.0</TargetFramework>
    <UserSecretsId>ed801301-1e44-4b8f-b1af-149350b5f235</UserSecretsId>
    <DockerDefaultTargetOS>Windows</DockerDefaultTargetOS>
  </PropertyGroup>

  <ItemGroup>
    <PackageReference Include="Microsoft.Azure.Devices.Client" Version="1.35.0" />
    <PackageReference Include="Microsoft.VisualStudio.Azure.Containers.Tools.Targets" Version="1.10.9" />
    <PackageReference Include="Swashbuckle.AspNetCore" Version="6.0.7" />
  </ItemGroup>

</Project>

Console log of the issue

Unhandled exception. System.Net.Sockets.SocketException (10014): The system detected an invalid pointer address in attempting to use a pointer argument in a call.
at System.Net.Sockets.Socket.AwaitableSocketAsyncEventArgs.CreateException(SocketError error, Boolean forAsyncThrow)
at System.Net.Sockets.Socket.AwaitableSocketAsyncEventArgs.ConnectAsync(Socket socket)
at System.Net.Sockets.Socket.ConnectAsync(EndPoint remoteEP, CancellationToken cancellationToken)
at System.Net.Sockets.Socket.ConnectAsync(EndPoint remoteEP)
at System.Net.Sockets.SocketTaskExtensions.ConnectAsync(Socket socket, EndPoint remoteEP)
at Microsoft.Azure.Devices.Client.HsmAuthentication.Transport.HttpUdsMessageHandler.GetConnectedSocketAsync()
at System.Runtime.CompilerServices.AsyncMethodBuilderCore.Start[TStateMachine](TStateMachine& stateMachine)
at Microsoft.Azure.Devices.Client.HsmAuthentication.Transport.HttpUdsMessageHandler.GetConnectedSocketAsync()
at Microsoft.Azure.Devices.Client.HsmAuthentication.Transport.HttpUdsMessageHandler.SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
at System.Runtime.CompilerServices.AsyncMethodBuilderCore.Start[TStateMachine](TStateMachine& stateMachine)
at Microsoft.Azure.Devices.Client.HsmAuthentication.Transport.HttpUdsMessageHandler.SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
at System.Net.Http.HttpMessageInvoker.SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
at System.Net.Http.HttpClient.SendAsyncCore(HttpRequestMessage request, HttpCompletionOption completionOption, Boolean async, Boolean emitTelemetryStartStop, CancellationToken cancellationToken)
at System.Runtime.CompilerServices.AsyncMethodBuilderCore.Start[TStateMachine](TStateMachine& stateMachine)
at System.Net.Http.HttpClient.SendAsyncCore(HttpRequestMessage request, HttpCompletionOption completionOption, Boolean async, Boolean emitTelemetryStartStop, CancellationToken cancellationToken)
at System.Net.Http.HttpClient.SendAsync(HttpRequestMessage request, HttpCompletionOption completionOption, CancellationToken cancellationToken)
at Microsoft.Azure.Devices.Client.HsmAuthentication.GeneratedCode.HttpHsmClient.TrustBundleAsync(String api_version, CancellationToken cancellationToken)
at System.Runtime.CompilerServices.AsyncMethodBuilderCore.Start[TStateMachine](TStateMachine& stateMachine)
at Microsoft.Azure.Devices.Client.HsmAuthentication.GeneratedCode.HttpHsmClient.TrustBundleAsync(String api_version, CancellationToken cancellationToken)
at Microsoft.Azure.Devices.Client.HsmAuthentication.GeneratedCode.HttpHsmClient.TrustBundleAsync(String api_version)
at Microsoft.Azure.Devices.Client.Edge.TrustBundleProvider.<>c__DisplayClass3_0.b__0()
at Microsoft.Azure.Devices.Client.TransientFaultHandling.AsyncExecution1.ExecuteAsyncImpl(Task ignore) at Microsoft.Azure.Devices.Client.TransientFaultHandling.AsyncExecution1.ExecuteAsync()
at Microsoft.Azure.Devices.Client.TransientFaultHandling.RetryPolicy.ExecuteAsync[TResult](Func1 taskFunc, CancellationToken cancellationToken) at Microsoft.Azure.Devices.Client.TransientFaultHandling.RetryPolicy.ExecuteAsync[TResult](Func1 taskFunc)
at Microsoft.Azure.Devices.Client.Edge.TrustBundleProvider.GetTrustBundleWithRetryAsync(HttpHsmClient hsmHttpClient, String apiVersion)
at System.Runtime.CompilerServices.AsyncMethodBuilderCore.Start[TStateMachine](TStateMachine& stateMachine)
at Microsoft.Azure.Devices.Client.Edge.TrustBundleProvider.GetTrustBundleWithRetryAsync(HttpHsmClient hsmHttpClient, String apiVersion)
at Microsoft.Azure.Devices.Client.Edge.TrustBundleProvider.GetTrustBundleAsync(Uri providerUri, String apiVersion)
at System.Runtime.CompilerServices.AsyncMethodBuilderCore.Start[TStateMachine](TStateMachine& stateMachine)
at Microsoft.Azure.Devices.Client.Edge.TrustBundleProvider.GetTrustBundleAsync(Uri providerUri, String apiVersion)
at Microsoft.Azure.Devices.Client.Edge.EdgeModuleClientFactory.CreateInternalClientFromEnvironmentAsync()
at System.Runtime.CompilerServices.AsyncMethodBuilderCore.Start[TStateMachine](TStateMachine& stateMachine)
at Microsoft.Azure.Devices.Client.Edge.EdgeModuleClientFactory.CreateInternalClientFromEnvironmentAsync()
at Microsoft.Azure.Devices.Client.Edge.EdgeModuleClientFactory.CreateAsync()
at Microsoft.Azure.Devices.Client.ModuleClient.CreateFromEnvironmentAsync(ITransportSettings[] transportSettings, ClientOptions options)
at Microsoft.Azure.Devices.Client.ModuleClient.CreateFromEnvironmentAsync(TransportType transportType, ClientOptions options)
at Microsoft.Azure.Devices.Client.ModuleClient.CreateFromEnvironmentAsync(ClientOptions options)
at WebApplication4.Program.Main(String[] args) in C:\src\WebApplication4\Program.cs:line 24
at System.Runtime.CompilerServices.AsyncMethodBuilderCore.Start[TStateMachine](TStateMachine& stateMachine)
at WebApplication4.Program.Main(String[] args)
at WebApplication4.Program.

(String[] args)
--- End of stack trace from previous location ---
at Microsoft.Azure.Devices.Client.HsmAuthentication.Transport.HttpUdsMessageHandler.GetConnectedSocketAsync()
at Microsoft.Azure.Devices.Client.HsmAuthentication.Transport.HttpUdsMessageHandler.SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
at System.Net.Http.HttpClient.SendAsyncCore(HttpRequestMessage request, HttpCompletionOption completionOption, Boolean async, Boolean emitTelemetryStartStop, CancellationToken cancellationToken)
at Microsoft.Azure.Devices.Client.HsmAuthentication.GeneratedCode.HttpHsmClient.TrustBundleAsync(String api_version, CancellationToken cancellationToken)
at Microsoft.Azure.Devices.Client.Edge.TrustBundleProvider.GetTrustBundleWithRetryAsync(HttpHsmClient hsmHttpClient, String apiVersion)
at Microsoft.Azure.Devices.Client.Edge.TrustBundleProvider.GetTrustBundleAsync(Uri providerUri, String apiVersion)
at Microsoft.Azure.Devices.Client.Edge.EdgeModuleClientFactory.CreateInternalClientFromEnvironmentAsync()
at WebApplication4.Program.Main(String[] args) in C:\src\WebApplication4\Program.cs:line 24
at WebApplication4.Program.(String[] args)

@shaeussler shaeussler added the bug Something isn't working. label Feb 25, 2021
@github-actions github-actions bot added the IoTSDK Tracks all IoT SDK issues across the board label Feb 25, 2021
@barustum barustum self-assigned this Feb 25, 2021
@barustum
Copy link
Contributor

Hi @shaeussler

While I take a closer look at the issue, can you let me know if you can reproduce the problem with .NET Core 3.1? Trying to identify whether it's a .NET 5.0 specific issue, or whether it happens on different target frameworks.

@shaeussler
Copy link
Author

Hi @barustum
I moved a ASP.NET Core project from netcoreapp3.1 to net5.0. The project uses ModuleClient.CreateFromEnvironmentAsync.
With netcoreapp3.1 there is no error. I run the netcoreapp3.1 ASP.NET Core project under Windows 10 1809 and Linux Ubuntu 18.04. Also running the net5.0 ASP.NET Core project under Linux Ubuntu 18.04 shows no error.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working. IoTSDK Tracks all IoT SDK issues across the board
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants