Skip to content

Simplify GlobalTypeMapper locking and fix races#6538

Merged
NinoFloris merged 2 commits into
npgsql:mainfrom
NinoFloris:fix-globaltypemapper-locking
Apr 16, 2026
Merged

Simplify GlobalTypeMapper locking and fix races#6538
NinoFloris merged 2 commits into
npgsql:mainfrom
NinoFloris:fix-globaltypemapper-locking

Conversation

@NinoFloris

Copy link
Copy Markdown
Member

Fixes some races that are theoretically there but practically don't get hit. This code has not really needed to change (or had any issues related to it) since we removed the complexity from the global mapper in 8.0. Either way it seems good to get this into a 'stupid simple good' state for future reference of the desired behavior, during our deliberately long obsoletion window.

At some point we can just take the global mapper internal as-is for its only remaining use, static/global parameter type inference.

Copilot AI review requested due to automatic review settings April 10, 2026 14:36
Comment thread src/Npgsql/TypeMapping/GlobalTypeMapper.cs

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Pull request overview

This PR simplifies thread-safety in GlobalTypeMapper by replacing ReaderWriterLockSlim-based locking with a single monitor lock and by restructuring the cached type-mapping options construction/invalidation to reduce race windows.

Changes:

  • Replaced ReaderWriterLockSlim usage with a single lock (_sync) for all mutable global-mapper state.
  • Simplified cache invalidation by directly nulling _typeMappingOptions in mutation paths.
  • Refactored type-mapping options creation into BuildTypeMappingOptions() with a lock-guarded initialization path.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread src/Npgsql/TypeMapping/GlobalTypeMapper.cs
Comment thread src/Npgsql/TypeMapping/GlobalTypeMapper.cs

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Pull request overview

Copilot reviewed 1 out of 1 changed files in this pull request and generated 1 comment.


💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread src/Npgsql/TypeMapping/GlobalTypeMapper.cs
@NinoFloris
NinoFloris merged commit 44f1394 into npgsql:main Apr 16, 2026
16 checks passed
@NinoFloris
NinoFloris deleted the fix-globaltypemapper-locking branch April 16, 2026 10:44
NinoFloris added a commit that referenced this pull request Apr 20, 2026
NinoFloris added a commit that referenced this pull request Apr 20, 2026
NinoFloris added a commit that referenced this pull request Apr 20, 2026
NinoFloris added a commit that referenced this pull request Apr 20, 2026
@NinoFloris

Copy link
Copy Markdown
Member Author

Backported to 10.0.3 via 14e994a, 9.0.6 via d812cc5, and 8.0.10 via 4a55f7c

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.

3 participants