Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

await dbContext.Database.SqlQuery<*>(....).ToListAsync() hang #32906

Closed
TheoVC opened this issue Jan 24, 2024 · 3 comments
Closed

await dbContext.Database.SqlQuery<*>(....).ToListAsync() hang #32906

TheoVC opened this issue Jan 24, 2024 · 3 comments
Labels
closed-no-further-action The issue is closed and no further action is planned. customer-reported

Comments

@TheoVC
Copy link

TheoVC commented Jan 24, 2024

I am transforming some functions to their asynchronous version, I am using the SqlQuery method, but these asynchronous versions hang. without throwing a exception... the application is simply closed.

I made the tests in Entity Framework 6 and 8 with a simple console application with the same result

static async Task MyNewAsyncTest()
{
    try
    {
        using (var dbContext = new DbContext())
        {
           // Sincronous version, These works fine!
           var data = dbContext.Database.SqlQuery<string>($"select cod_empresa from si_empresa").ToList();
           var data = dbContext.Database.SqlQuery<Persona>($"select grado_discapacidad_persona, nombres_persona from rh_persona").ToList();

           // The same but async version hang up, with primitive types or clases
           var data = await dbContext.Database.SqlQuery<string?>($"select cod_empresa from si_empresa").ToListAsync();
           var data = await dbContext.Database.SqlQuery<Persona>($"select grado_discapacidad_persona, nombres_persona from rh_persona").ToListAsync();

           // even weirder, if I dont apply await inmediately, but wait 2 secs then it works !!! 
           var Get1 = dbContext.Database.SqlQuery<string>("select cod_empresa from si_empresa").ToListAsync();
           Thread.Sleep(2000);
           var data = await Get1;

            foreach (var i in data)
                Console.WriteLine(i);
        }
    }
    catch (Exception ex)
    {
        Console.WriteLine(ex.ToString());
    }
    Console.ReadLine();
}
public class Persona
{
    public string nombres_persona { get; set; }

    public int? grado_discapacidad_persona { get; set; }
}
/*==============================================================*/
/* Table: RH_PERSONA                                            */
/*==============================================================*/
create table RH_PERSONA (
   NOMBRES_PERSONA      varchar(30)          not null,
   GRADO_DISCAPACIDAD_PERSONA int                  null
)

EF Core version: 6 and 8
Database provider: (Microsoft.EntityFrameworkCore.SqlServer)
Target framework: (.Net Framework 4.8 and .NET Core 8.0)
Operating system: Windows 11
IDE: (Visual Studio 2022 17.8.5)

@roji
Copy link
Member

roji commented Jan 24, 2024

If the data being loaded is big (e.g. big strings or binary data), then this is probably dotnet/SqlClient#593.

If not, then we need a minimal, runnable repro to investigate this further - a self-contained console program, including the database schema and some data. Basically something we can use to see the bug happening.

@TheoVC
Copy link
Author

TheoVC commented Jan 24, 2024

I attach a zip with a simple ConsoleApp and the database used (or use the script)

ConsoleAppAsyncHang.zip

@TheoVC
Copy link
Author

TheoVC commented Jan 24, 2024

Sorry, I discovered it's because the main function is asyncronous, so the main() terminates the execution before the job is done.

@TheoVC TheoVC closed this as completed Jan 24, 2024
@roji roji closed this as not planned Won't fix, can't repro, duplicate, stale Jan 24, 2024
@roji roji added the closed-no-further-action The issue is closed and no further action is planned. label Jan 24, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
closed-no-further-action The issue is closed and no further action is planned. customer-reported
Projects
None yet
Development

No branches or pull requests

2 participants