Skip to content

Non-Deterministic missing libraries in artifacts for same codebase (no changes) #52294

@humble-codes

Description

@humble-codes

Description

During random builds generated through GitHub actions on Amazon EKS, upon running these specific builds always get ReflectionTypeLoadException thrown saying - Could not load file or assembly 'SomeAssemblyName, Version=VersionNumber, Culture=neutral, PublicKeyToken=SomeToken'. The system cannot find the file specified.

Project Details

  1. It is a multi-project based solution (API, Infra, Core, etc. )
  2. API project is the start-up project
  3. The solution is based upon dotnet 8
  4. We use GitHub actions with self hosted runner to build and deploy the project and later run it on lambda.

Necessary files and commands used

  • API project's .csproj
<Project Sdk="Microsoft.NET.Sdk.Web">
  <PropertyGroup>
    <OutputType>Exe</OutputType>
    <TargetFramework>net8.0</TargetFramework>
    <ImplicitUsings>enable</ImplicitUsings>
    <Nullable>enable</Nullable>
    <GenerateRuntimeConfigurationFiles>true</GenerateRuntimeConfigurationFiles>
    <AWSProjectType>Lambda</AWSProjectType>
    <CopyLocalLockFileAssemblies>true</CopyLocalLockFileAssemblies>
    <PublishReadyToRun>false</PublishReadyToRun>
  </PropertyGroup>
  <ItemGroup>
    <PackageReference Include="Amazon.Lambda.AspNetCoreServer" Version="9.0.0" />
    <PackageReference Include="Amazon.Lambda.AspNetCoreServer.Hosting" Version="1.7.0" />
    <PackageReference Include="CsvHelper" Version="33.1.0" />
    <PackageReference Include="MediatR.Contracts" Version="2.0.1" />
    <PackageReference Include="Microsoft.AspNetCore.Authentication.JwtBearer" Version="6.0.16" />
    <PackageReference Include="Microsoft.AspNetCore.Mvc.Versioning" Version="5.1.0" />
    <PackageReference Include="Microsoft.Data.SqlClient" Version="5.1.5" />
    <PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="8.0.0" />
    <PackageReference Include="Newtonsoft.Json" Version="13.0.3" />
    <PackageReference Include="Swashbuckle.AspNetCore" Version="6.7.0" />
  </ItemGroup>
  <ItemGroup>
    <ProjectReference Include="..\platform-jjz-service-core\platform-jjz-service-core.csproj" />
    <ProjectReference Include="..\platform-jjz-service-domain\platform-jjz-service-domain.csproj" />
    <ProjectReference Include="..\platform-jjz-service-Infrastructure\platform-jjz-service-Infrastructure.csproj" />
  </ItemGroup>
  <ItemGroup>
    <Content Update="appsettings.json">
      <CopyToOutputDirectory>Always</CopyToOutputDirectory>
    </Content>
  </ItemGroup>
</Project>
  • dotnet CLI commands used in GitHub pipeline to publish project/artifact
  dotnet restore
  dotnet publish -c Release -r linux-x64 --self-contained false -o ./publish

Expected Behaviour

When running the code via dotnet cli, the published artifact should run without any errors for every build created using same codebase on lambda (given that there are no other errors in codebase).

Actual Behaviour

When lambda tries to run the published artifacts, some artifacts (that were generated via same codebase) always throw ReflectionTypeLoadException exception. Only after couple of attempts to re-publishing same code, this issue gets resolved on its own.
I get the same error as the lambda error, if I try running the GitHub artifact on my local windows machine using dotnet platform_jjz_service_api.dll

Stack trace for runtime exception

Unhandled exception. System.Reflection.ReflectionTypeLoadException: Unable to load one or more of the requested types.
Could not load file or assembly 'Microsoft.Data.SqlClient, Version=5.0.0.0, Culture=neutral, PublicKeyToken=23ec7fc2d6eaa4a5'. The system cannot find the file specified.
   at System.Reflection.RuntimeModule.GetTypes(RuntimeModule module)
   at MediatR.Registration.ServiceRegistrar.<>c.<ConnectImplementationsToTypesClosing>b__1_0(Assembly a)
   at System.Linq.Enumerable.SelectManySingleSelectorIterator`2.MoveNext()
   at System.Linq.Enumerable.WhereEnumerableIterator`1.MoveNext()
   at System.Linq.Enumerable.WhereEnumerableIterator`1.MoveNext()
   at MediatR.Registration.ServiceRegistrar.ConnectImplementationsToTypesClosing(Type openRequestInterface, IServiceCollection services, IEnumerable`1 assembliesToScan, Boolean addIfAlreadyExists, MediatRServiceConfiguration configuration)
   at MediatR.Registration.ServiceRegistrar.AddMediatRClasses(IServiceCollection services, IEnumerable`1 assembliesToScan, MediatRServiceConfiguration configuration)
   at MediatR.ServiceCollectionExtensions.AddMediatR(IServiceCollection services, IEnumerable`1 assemblies, Action`1 configuration)
   at MediatR.ServiceCollectionExtensions.AddMediatR(IServiceCollection services, Assembly[] assemblies)
   at platform_jjz_service_api.Startup.ConfigureServices(IServiceCollection services) in /runner/_work/clin/clin/platform_jjz_service_api/Startup.cs:line 292
   at System.RuntimeMethodHandle.InvokeMethod(Object target, Void** arguments, Signature sig, Boolean isConstructor)
   at System.Reflection.MethodBaseInvoker.InvokeDirectByRefWithFewArgs(Object obj, Span`1 copyOfArgs, BindingFlags invokeAttr)
   at System.Reflection.MethodBaseInvoker.InvokeWithOneArg(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture)
   at Microsoft.AspNetCore.Hosting.ConfigureServicesBuilder.InvokeCore(Object instance, IServiceCollection services)
   at Microsoft.AspNetCore.Hosting.GenericWebHostBuilder.UseStartup(Type startupType, HostBuilderContext context, IServiceCollection services, Object instance)
   at Microsoft.AspNetCore.Hosting.GenericWebHostBuilder.<>c__DisplayClass7_0.<UseStartup>b__0(HostBuilderContext context, IServiceCollection services)
   at Microsoft.Extensions.Hosting.HostBuilder.InitializeServiceProvider()
   at Microsoft.Extensions.Hosting.HostBuilder.Build()
   at platform_jjz_service_api.LocalEntryPoint.Main(String[] args) in /runner/_work/clin/clin/platform_jjz_service_api/LocalEntryPoint.cs:line 10
System.IO.FileNotFoundException: Could not load file or assembly 'Microsoft.Data.SqlClient, Version=5.0.0.0, Culture=neutral, PublicKeyToken=23ec7fc2d6eaa4a5'. The system cannot find the file specified.
File name: 'Microsoft.Data.SqlClient, Version=5.0.0.0, Culture=neutral, PublicKeyToken=23ec7fc2d6eaa4a5'

My findings

  1. This is a non-deterministic error, which randomly occurs (only some builds throw this runtime exception, other builds for same code work totally fine)
  2. When compared an erroneous artifact with a correct artifact, noticed that number of libraries in platform_jjz_service_api.deps.json is lesser in erroneous artifact.
  3. Changed the dotnet commands slightly to publish the release artifact, but no luck there :
  dotnet nuget locals all --clear
  dotnet restore --locked-mode
  dotnet publish -c Release -r linux-x64 --self-contained false -o ./publish

Known Workarounds

Keep re-deploying the same code mutiple times, eventually in one of the deployments, this issue goes away

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions