Runtime: "An item with the same key has already been added" when calling Assembly.GetCustomAttributes using a custom attribute

Created on 5 May 2020  路  3Comments  路  Source: dotnet/runtime

A few days ago @JimBobSquarePants has reported an issue in BenchmarkDotNet repo https://github.com/dotnet/BenchmarkDotNet/issues/1439

I was able to isolate it and reproduce on .NET Core 2.2, 3.0 and 5.0. It works fine one 2.1!

For some reason System.Reflection.CustomAttributeExtensions.GetCustomAttributes(Assembly element) throws when we call it for the second time for the zlib.managed assembly.

Repro:

<Project Sdk="Microsoft.NET.Sdk">
  <PropertyGroup>
    <OutputType>Exe</OutputType>
    <TargetFramework>netcoreapp3.1</TargetFramework>
  </PropertyGroup>
  <ItemGroup>
    <PackageReference Include="zlib.managed" Version="1.1.4" />
  </ItemGroup>
</Project>
using System;
using System.Diagnostics;
using System.Linq;
using System.Reflection;

namespace ZlibRepro
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine(typeof(Elskom.Generic.Libs.ZOutputStream).Assembly);
            Console.WriteLine(GetDebuggableAttribute(typeof(Elskom.Generic.Libs.ZOutputStream).Assembly));
            Console.WriteLine(GetDebuggableAttribute(typeof(Elskom.Generic.Libs.ZOutputStream).Assembly));
        }

        private static DebuggableAttribute GetDebuggableAttribute(Assembly assembly)
            => assembly?.GetCustomAttributes()
                .OfType<DebuggableAttribute>()
                .FirstOrDefault();
    }
}

zlib.managed, Version=1.1.4.0, Culture=neutral, PublicKeyToken=0371adb4a80cedf9
System.Diagnostics.DebuggableAttribute
Unhandled exception. System.ArgumentException: An item with the same key has already been added. Key: zlib.managed, Version=1.1.4.0, Culture=neutral, PublicKeyToken=0371adb4a80cedf9
   at System.Collections.Generic.Dictionary`2.TryInsert(TKey key, TValue value, InsertionBehavior behavior)
   at System.Collections.Generic.Dictionary`2.Add(TKey key, TValue value)
   at System.Runtime.InteropServices.GitInformation..ctor(String headdesc, String commit, String branchname, Assembly assembly)
   at System.Runtime.InteropServices.GitInformationAttribute..ctor(String headdesc, String commit, String branchname, Type assemblyType)
   at System.Reflection.CustomAttribute._CreateCaObject(RuntimeModule pModule, RuntimeType type, IRuntimeMethodInfo pCtor, Byte** ppBlob, Byte* pEndBlob, Int32* pcNamedArgs)
   at System.Reflection.CustomAttribute.CreateCaObject(RuntimeModule module, RuntimeType type, IRuntimeMethodInfo ctor, IntPtr& blob, IntPtr blobEnd, Int32& namedArgs)
   at System.Reflection.CustomAttribute.AddCustomAttributes(ListBuilder`1& attributes, RuntimeModule decoratedModule, Int32 decoratedMetadataToken, RuntimeType attributeFilterType, Boolean mustBeInheritable, ListBuilder`1 derivedAttributes)
   at System.Reflection.CustomAttribute.GetCustomAttributes(RuntimeModule decoratedModule, Int32 decoratedMetadataToken, Int32 pcaCount, RuntimeType attributeFilterType)
   at System.Reflection.CustomAttribute.GetCustomAttributes(RuntimeAssembly assembly, RuntimeType caType)
   at System.Reflection.RuntimeAssembly.GetCustomAttributes(Type attributeType, Boolean inherit)
   at System.Attribute.GetCustomAttributes(Assembly element, Boolean inherit)
   at System.Reflection.CustomAttributeExtensions.GetCustomAttributes(Assembly element)
   at ZlibRepro.Program.GetDebuggableAttribute(Assembly assembly) in C:\Users\adsitnik\source\repos\ZlibRepro\ZlibRepro\Program.cs:line 18
   at ZlibRepro.Program.Main(String[] args) in C:\Users\adsitnik\source\repos\ZlibRepro\ZlibRepro\Program.cs:line 14
area-System.Reflection

Most helpful comment

   at System.Runtime.InteropServices.GitInformation..ctor(String headdesc, String commit, String branchname, Assembly assembly)

This is running the custom attribute constructor. Despite the namespace, this is not our custom attribute.

Most likely a bug in this repo: https://github.com/AraHaan/GitInformation/blob/408423341b7df8b1f7d17e25f21b4047600bff0e/src/GitInformation/AraHaan.GitInformation/GitInformationAttribute.cs

It seems like it's trying to update a bunch of static dictionaries.

All 3 comments

What is System.Runtime.InteropServices.GitInformationAttribute?

   at System.Runtime.InteropServices.GitInformation..ctor(String headdesc, String commit, String branchname, Assembly assembly)

This is running the custom attribute constructor. Despite the namespace, this is not our custom attribute.

Most likely a bug in this repo: https://github.com/AraHaan/GitInformation/blob/408423341b7df8b1f7d17e25f21b4047600bff0e/src/GitInformation/AraHaan.GitInformation/GitInformationAttribute.cs

It seems like it's trying to update a bunch of static dictionaries.

I should not be opening issues before having my first coffee...

I've sent a fix to solve the actual problem: https://github.com/AraHaan/GitInformation/pull/39

Was this page helpful?
0 / 5 - 0 ratings