runtime: Regression on static initialization using STJ

Can be reproduced on the build after these changes: https://github.com/dotnet/runtime/compare/2d552be28782f3ff7e50c90d272c1b77d5c7db16...7efce05745322d722e3effac4f0859c554ad7ec0

With this code in a static class:

private readonly static uint _jsonPayloadSize = (uint)JsonSerializer.SerializeToUtf8Bytes(new JsonMessage { message = "Hello, World!" }, SerializerOptions).Length;

The following exception is thrown at runtime:

Unhandled exception. System.TypeInitializationException: The type initializer for 'PlatformBenchmarks.BenchmarkApplication' threw an exception.
 ---> System.TypeInitializationException: The type initializer for 'System.Text.Json.JsonSerializerOptions' threw an exception.
 ---> System.InvalidOperationException: Could not execute the method because either the method itself or the containing type is not fully instantiated.
   at System.Text.Json.Serialization.JsonConverter`1.get_TypeToConvert()
   at System.Text.Json.JsonSerializerOptions.GetDefaultSimpleConverters()
   at System.Text.Json.JsonSerializerOptions..cctor()
   --- End of inner exception stack trace ---
   at System.Text.Json.JsonSerializerOptions.GetConverter(Type typeToConvert)
   at System.Text.Json.JsonSerializerOptions.DetermineConverter(Type parentClassType, Type runtimePropertyType, MemberInfo memberInfo)
   at System.Text.Json.JsonClassInfo.GetConverter(Type type, Type parentClassType, MemberInfo memberInfo, Type& runtimeType, JsonSerializerOptions options)
   at System.Text.Json.JsonClassInfo..ctor(Type type, JsonSerializerOptions options)
   at System.Text.Json.JsonSerializerOptions.GetOrAddClass(Type type)
   at System.Text.Json.JsonSerializerOptions.GetOrAddClassForRootType(Type type)
   at System.Text.Json.WriteStack.Initialize(Type type, JsonSerializerOptions options, Boolean supportContinuation)
   at System.Text.Json.JsonSerializer.WriteCore[TValue](Utf8JsonWriter writer, TValue& value, Type inputType, JsonSerializerOptions options)
   at System.Text.Json.JsonSerializer.WriteCoreBytes[TValue](TValue& value, Type inputType, JsonSerializerOptions options)
   at System.Text.Json.JsonSerializer.SerializeToUtf8Bytes[TValue](TValue value, JsonSerializerOptions options)
   at PlatformBenchmarks.BenchmarkApplication..cctor() in /tmp/benchmarks-agent/benchmarks-server-1/2yox3r1x.25e/benchmarks/src/BenchmarksApps/Kestrel/PlatformBenchmarks/BenchmarkApplication.Json.cs:line 12

/cc @eerhardt

About this issue

  • Original URL
  • State: closed
  • Created 3 years ago
  • Comments: 16 (16 by maintainers)

Commits related to this issue

Most upvoted comments

Well, I believe we’ve actually only been running libraries tests on top of Crossgen2 framework on ad hoc basis, in fact the CI infrastructure doesn’t currently even support that unless I’m mistaken, the framework CG2 change only affects the installer build, not library test legs. I agree that’s something we should improve on but I don’t think anything changed in contrast to Crossgen1. One way or another this looks similar to the bug I was fixing in

https://github.com/dotnet/runtime/pull/43054

I’ll look into the issue in about an hour once I’m out of meetings.

Looking…

I confirm the latest build, which includes the fix, fixes all scenarios that were broken. Thanks!