Skip to content

SqlMapper.AddTypeHandler not thread safe #1815

Closed
@StephanMoeller

Description

@StephanMoeller

SqlMapper.SetTypeMap is thread safe. This is achievede by:

lock (_typeMaps)
{
...
}

But SqlMapper.AddTypeHandler is not thread safe.

I think it would be valuable to make AddTypehandler thread safe as it enables different data access to be self contained instead of having a central place for registering all custom types.

Scenario:

Using 3rd party package A and another third party package B.
These both use Dapper. No problem, it is the same version.

These two packages each register some custom type handlers lazily, the first time they need them.

In AddTypeHandler there is no locking, the flow is as follows:

...
var snapshot = typeHandlers;
... // <= In this time span, multiple simultanious calls will trigger lost updates.
typeHandlers = newCopy;

I felt this the hard way in production yesterday.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions

      SqlMapper.AddTypeHandler not thread safe · Issue #1815 · DapperLib/Dapper