DapperLib / DapperLib/Dapper

SqlMapper AddTypeMap/RemoveTypeMap not thread safe

Open
#1,439 0 comments 2 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
C#
Stars
18.4k
Forks
3.7k
Avg merge
5h 8m
Merged PRs (30d)
1

Description

AddTypeMap:
https://github.com/StackExchange/Dapper/blob/master/Dapper/SqlMapper.cs#L230

RemoveTypeMap:
https://github.com/StackExchange/Dapper/blob/master/Dapper/SqlMapper.cs#L244

Both methods make snapshots of the type map before they update it, however they do not ensure the reference has not been modified before they replace it (as might be implemented with Interlocked.CompareExchange)
This can cause one thread to overwrite another's change.

Eg Thread A calls AddTypeMap(XType, DbType) and Thread B calls AddTypeMap(YType, DbType), one possible execution path is:

A takes a reference to the current typeMap and verifies it does not contain XType
B takes a reference to the current typeMap and verifies it does not contain YType
A creates a copy of typeMap
B creates a copy of typeMap
A inserts XType into its copy of typeMap
A sets the real typeMap variable to use its copy
B inserts YType into its copy of typeMap
B sets the real typeMap variable to use its copy <- B's copy does not contain XType, so it effectively erases A's changes here.

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start in Dapper/SqlMapper.cs at AddTypeMap around line 230 and RemoveTypeMap around line 244. Trace how each method snapshots and replaces the type map, then verify concurrent updates do not overwrite one another. Done means changes from simultaneous AddTypeMap or RemoveTypeMap calls are all preserved.

Written by the indexing model from the issue text.

Assessment

Tech stack
csharp
Domain
database
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Clearly specified
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.