DapperLib / DapperLib/Dapper

Stackoverflow when using async methods

Open
#1,475 5 comments 0 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

I'm using Dapper in a .net core app to connect to a postgres database using npgsql. I'm using Entity Framework to manage the schema, but querying exclusively with Dapper. I have two repository classes that use the same DbContext. Both are using DbContext.Database.GetDbConnection() to get a reference to the connection. I'm trying to use Dapper's ExecuteScalarAsync<T> but in one of the repository classes, I'm getting a stack overflow. The other works correctly every time. It appears that if I remove the Async and call the synchronous version, the problem goes away. The same problem occurs if I change the method to QueryAsync<T> vs Query<T> - I get a stackoverflow from QueryAsync but not from Query.

Code example:

internal class MyRepository
{
  private readonly MyContext DB;
  private IDbConnection DbConnection => this.DB.Database.GetDbConnection();

  public async Task Insert(MyModel model) {
    var sql = @"
      INSERT INTO public.mytable (someString, someNumber)
      VALUES (@string, @number)
      SELECT currval(pg_get_serial_sequence('mytable', 'idcolumn'))";

    var newId = await DbConnection.ExecuteScalarAsync<int>(
      sql, 
      new { string = model.String, number = model.Number }
    );

    // ...
  }
}

The call to ExecuteScalarAsync results in a stackoverflow exception.

I've seen issues with nested models before, but the model here doesn't have nested object properties, and anyway there's no actual model mapping going on here. I also don't really suspect a connection management issue because I have other working classes that handle their db connections the same way.

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 with the MyRepository example and the DbConnection property, then compare ExecuteScalarAsync and its synchronous counterpart; the report also mentions QueryAsync and Query. Reproduce the stack overflow with the PostgreSQL connection and determine what distinguishes the failing repository from the working one; done means the async call no longer overflows and the behavior is covered by a regression test.

Written by the indexing model from the issue text.

Assessment

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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.