Skip to content

Conversation

Copy link
Contributor

Copilot AI commented Jan 27, 2026

NullabilityInfoContext uses a non-concurrent dictionary for internal caching, causing InvalidOperationException when Create methods are called concurrently. This wasn't documented anywhere. See dotnet/runtime#100254 for the concurrency failure.

Changes:

  • Added <threadsafe> section to type documentation specifying non-thread-safe behavior and recommending external synchronization for multi-threaded scenarios
  • Added <threadsafe> sections to each of the four Create method overloads (EventInfo, FieldInfo, ParameterInfo, PropertyInfo) stating "This method is not thread safe"
  • Uses "thread safe" (without hyphen) consistently with other System.Reflection types

Context:

// This throws InvalidOperationException
var ctx = new NullabilityInfoContext();
Parallel.ForEach(properties, prop => ctx.Create(prop));

// Use external synchronization for multi-threaded scenarios
var ctx = new NullabilityInfoContext();
var lockObj = new object();
Parallel.ForEach(properties, prop => {
    lock (lockObj) {
        var info = ctx.Create(prop);
    }
});
Original prompt

This section details on the original issue you should resolve

<issue_title>Not thread-safety should be explicitly documented.</issue_title>
<issue_description>### Type of issue

Missing information

Description

I believe this issue contains some valuable details which deserve presence in the documentation or even in xmldoc comments of the Create method.

Page URL

https://learn.microsoft.com/en-us/dotnet/api/system.reflection.nullabilityinfocontext.create?view=net-10.0

Content source URL

https://github.com/dotnet/dotnet-api-docs/blob/main/xml/System.Reflection/NullabilityInfoContext.xml

Document Version Independent Id

a6845bae-0825-8996-cbfe-71285933a25a

Platform Id

847cd8f7-3db2-2288-932d-b1aeea925814

Article author

@dotnet-bot</issue_description>

Comments on the Issue (you are @copilot in this section)


💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more Copilot coding agent tips in the docs.

Co-authored-by: gewarren <24882762+gewarren@users.noreply.github.com>
Copilot AI changed the title [WIP] Document thread-safety concerns for Create method Document NullabilityInfoContext thread-safety constraints Jan 27, 2026
Copilot AI requested a review from gewarren January 27, 2026 01:17
@gewarren gewarren marked this pull request as ready for review January 27, 2026 04:14
@gewarren gewarren requested a review from a team as a code owner January 27, 2026 04:14
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Documents the thread-safety limitations of System.Reflection.NullabilityInfoContext, clarifying that concurrent calls to Create can fail due to internal non-concurrent caching.

Changes:

  • Added a <threadsafe> section to the NullabilityInfoContext type docs describing non-thread-safe behavior and recommended mitigations.
  • Added non-thread-safe warnings to the <remarks> of the four Create overloads (EventInfo, FieldInfo, ParameterInfo, PropertyInfo).

Co-authored-by: eiriktsarpalis <2813363+eiriktsarpalis@users.noreply.github.com>
Copy link
Member

@BillWagner BillWagner left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This LGTM

Co-authored-by: gewarren <24882762+gewarren@users.noreply.github.com>
Co-authored-by: gewarren <24882762+gewarren@users.noreply.github.com>
@gewarren gewarren enabled auto-merge (squash) January 27, 2026 22:21
@gewarren gewarren merged commit e49195c into main Jan 27, 2026
6 checks passed
@gewarren gewarren deleted the copilot/document-thread-safety-issue branch January 27, 2026 22:39
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Not thread-safety should be explicitly documented.

4 participants