Dapper cancelling only ExecuteReaderAsync not Reader.ReadAsync with MultiMap
Open
Nobody has claimed this yet.
v3.0
- Dominant language
- C#
- Stars
- 18.4k
- Forks
- 3.7k
- Avg merge
- 5h 8m
- Merged PRs (30d)
- 1
Description
Dapper will fail with such Test
async Task<IEnumerable<Product>> Test()
{
CancellationTokenSource cancel = new CancellationTokenSource();
try
{
const string sql = @"select 1 as id, 'abc' as name, 2 as id, 'def' as name
union all select 1 as id, 'abc' as name, 2 as id, 'def' as name";
var productQuery = await connection.QueryAsync<Product, Category, Product>(new CommandDefinition(sql, cancellationToken: cancel.Token), (p, c) => {
p.Category = c;
//we cancel it in the middle of reading
cancel.Cancel();
return p;
}).ConfigureAwait(false);
return productQuery;
}
catch (Exception agg)
{
//should be SqlException with message canceled
}
return null;
}
public async Task TestCancelInReaderReadAsync()
{
var res = await Test();
res.IsEqualTo(null);
}
solution:
replace all code like(in SQlMapper.Async.cs):
using (var cmd = (DbCommand)command.SetupCommand(cnn, info.ParamReader))
{
/*...*/
}
with
using (var cmd = (DbCommand)command.SetupCommand(cnn, info.ParamReader))
{
using (var reg = command.CancellationToken.Register(() => cmd.Cancel()))
{
/*...*/
}
}
solution is simillar to this answer on SO but he is doing it at wrong place (and not dissposing the registration) ...
Contributor guide
No contributing guide indexed for this repository
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start in SqlMapper.Async.cs, locating the asynchronous command and reader paths represented by the issue's using block. Reproduce the shown multi-map query with cancellation during mapping, then verify that cancellation reaches the reader and produces the expected cancelled-operation behavior without leaving registrations undisposed.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- csharp, sql
- Domain
- backend, database
- Issue type
- Bug
- Difficulty
- 3/5
- Estimated time
- 1-2 days
- Activity status
- Stale
- Clarity
- Clearly specified
- Newbie friendliness
- 35/100