DapperLib / DapperLib/Dapper

QueryAsync sometimes returns data for a different request than the current one

Open
#2,159 5 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

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

Description

I have an application in ASP.NET MVC + ASP.NET API (.NET 9, OS Windows Server 2022, MS SQL 2022), where I use the Dapper library (2.1.66) to call stored procedures (other queries are handled by EF 9.0.4).

Recently, I noticed that with a high volume of requests to call the same procedure but with different parameters, sometimes I receive data from the result of a different request's execution. It seems that both the SQL and C# code are correct. Additionally, I have been monitoring the database queries and intentionally added an OUTPUT parameter (FakeParameter) with a random GUID to make sure this is not a SQL code issue. Importantly, this error occurs occasionally for different input parameters, with no apparent pattern, except that both requests need to come almost simultaneously. In such cases, Request 1 will receive its correct data in response, but Request 2 will receive the same data as Request 1 instead of its own separate data.

Below are code examples:

List<SP_Result> data = null;
var args = new DynamicParameters(new
{
AccountId = model.AccountId,
SortField = model.OrderColumnNo,
SortDirection = model.OrderDirection,
});
args.Add("RecordsTotal", dbType: DbType.Int32, direction: ParameterDirection.Output);
args.Add("FakeParam", dbType: DbType.Guid, direction: ParameterDirection.Output);

using (var con = new SqlConnection(DBContext.Database.GetConnectionString()))
{
data = (await con.QueryAsync<SP_Result>("SPName",
args, commandType: CommandType.StoredProcedure)).ToList();
}

I am certain (through logging data to the logs) that the GUID value in FakeParam for Request 2 matches what was returned by the stored procedure call for the same request, but after calling ToList, the data returned is from Request 1.

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

The report names QueryAsync and shows the ASP.NET call site, but it does not identify a Dapper source file or failing test. Start by reproducing the behavior with concurrent calls using distinct parameters, then trace QueryAsync through result materialization and output-parameter handling. Done means isolating the cause and capturing it in a regression test that keeps each request's results separate.

Written by the indexing model from the issue text.

Assessment

Tech stack
csharp, sql
Domain
backend, database
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
30/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.