Skip to content

SQLite: Make async methods call Task.Yield() #28279

Description

@rodion-m

It's not good that SQLite's async methods are not real async (tested on v 6.0.6), since they don't yield back to the current context. These methods just work as synchronous methods. It's not good for tests and for Blazor apps that are using SQLite as a db. In Blazor with real async methods UI updates fine, but with methods from SQLite UI doesn't update as it's exptected, so I had to call extra Task.Yield() method:

private async Task AddProduct()
{
	_message = "Adding product..."; //this message is showing inside Blazor component
	await Task.Yield(); //I have to call this all the time before call dbContext's pseudo-async methods to properly update UI
	await DbContext.Products.AddAsync(product);
	await DbContext.SaveChangesAsync();
	_message = "Product is added";
}

I suggest to add properly async IO support, or at least add call of await Task.Yield() inside all pseudo-async SQLite-methods just for asynchronously yield back to the current context when awaited as a real async method does.

Activity

  1. ajcvickers commented on Jun 29, 2022

    @ajcvickers
    Contributor

    Notes from triage: this is something that we could consider, but it would require a lot of validation that it did not break or regress performance for existing scenarios. (Many uses of SQLite are not coupled with a U.I.) It might be better to do this, or have an explicit background thread, at the application layer.

  2. added this to the Backlog milestone on Jun 29, 2022
  3. rodion-m commented on Jun 29, 2022

    @rodion-m
    Author

    @ajcvickers thanks for the reply!
    But we are talking about async methods - so if a developer had called it, he would have had to know that it could a little bit slow the application, right?
    At least, we can make the behavior of SQLite's async methods configurable.

  4. msschl commented on Feb 10, 2023

    @msschl

    Wow that’s a bummer. I didn‘t know that those async methods are not really async under the cover.

  5. bricelam commented on Apr 4, 2023

    @bricelam
    Contributor

    See also Async limitations in the docs

  6. bricelam commented on Apr 4, 2023

    @bricelam
    Contributor

    Wait, wouldn't this lead to concurrency issues? For example, if the user clicks really fast, the same connection would be used on multiple background threads at the same time, right? (See #22330) In other words, we'd have to serialize all calls to be handled by a single background thread per connection. Like @ajcvickers said, this would be significantly worse than just completing synchronously for certain scenarios since.

    I think we should keep things as-is including our guidance: If you encounter UI responsiveness issues, offload that specific work to a background thread. It can be better optimized at the higher application-level anyway.

  7. removed this from the Backlog milestone on Apr 4, 2023
  8. roji commented on Apr 4, 2023

    @roji
    Member

    Wait, wouldn't this lead to concurrency issues? For example, if the user clicks really fast, the same connection would be used on multiple background threads at the same time, right?

    Isn't that disallowed, like for other ADO.NET providers? In other words, if I'm a user using SQL Server, it's already my responsibility to make sure that clicking twice doesn't execute twice on the same connection etc. (there are various ways to do this, but it's a higher-level UI problem). So I'm not sure this would introduce any problem for SQLite that doesn't already exist for all other providers (and maybe also for SQLite since #22330?).

    I do think it's not ideal that SQLite is problematic with UI responsiveness, but I think this could lead to various forms of TP starvation. For example, in a thundering herd scenario, all TP threads would very quickly be blocked on SQLite native calls, and the system would be unresponsive as threads are gradually added (hill climbing). Though to be fair, assuming requests are handled on the TP (the usual way), this is already the case.

    We may want to consult with some .NET folks about what's the best behavior here.

  9. roji commented on Apr 4, 2023

    @roji
    Member

    BTW I'm seeing some mention of actual async wrappers for SQLite (e.g. this, but also this page); is it maybe now somehow possible to do actual async, so that the thread isn't blocked for the disk I/O?

  10. ajcvickers commented on Apr 5, 2023

    @ajcvickers
    Contributor

    Agree with Shay that this should not cause concurrency issues. The thread may already yield at any point without the application requesting it. That being said, I was always in favor of doing nothing here. :-)

  11. added this to the Backlog milestone on Apr 5, 2023
  12. removed their assignment
    on Jul 8, 2023
  13. AderitoSilva commented on Nov 16, 2024

    @AderitoSilva

    If the current EF Core version's SQLite provider still uses "fake" async, I would suggest adding explicit documentation for this in MS Docs and wherever possible, and maybe with some workaround examples. That's because one would never suspect this, unless they are already aware that the underlying ADO.NET provider also uses "fake" async and that SQLite is synchronous-only by design.

    I believe that having "fake" async methods can be worse than having those methods throw NotSupportedException.

  14. bazzilic commented on Nov 16, 2024

    @bazzilic

    I believe that having "fake" async methods can be worse than having those methods throw NotSupportedException.

    Having fake asyncs for SQLite allows for simpler switching between database engines in many cases. In some cases, it even is possible to only change the configuration and leave all data manipulation code intact.

    Can you elaborate how fake asyncs could be detrimental?

  15. bricelam commented on Nov 16, 2024

    @bricelam
    Contributor

    Can you elaborate how fake asyncs could be detrimental?

    Instead of improving performance and throughput by leveraging asynchronous I/O calls as expected, it will degrade them by hogging additional threads.

    In other words, you'll get the responsiveness of async, but at the cost of using additional resources.

  16. AderitoSilva commented on Nov 16, 2024

    @AderitoSilva

    @bazzilic

    Having fake asyncs for SQLite allows for simpler switching between database engines in many cases.

    I'm aware of that, but I'm honestly not in favor of the idea of having a change in behavior (from async to sync code) this way. Depending on the situation and on the application, I might need to execute a big query in a separate thread instead of just awaiting a call to the EF Core async method directly.

    Can you elaborate how fake asyncs could be detrimental?

    I'm expecting the async method to not block, but it might actually block. That could get bad and cause a lot of debugging headaches if the dev doesn't know that the async method is actually sync. I think all async methods should be async, or else it is an antipattern. If the async method cannot be implemented asynchronously, and if there is a sync version of the method, I would rather have the async method throw a NotSupportedException and fail fast.

  17. bricelam commented on Nov 16, 2024

    @bricelam
    Contributor

    I think all async methods should be async

    Note that many async methods inside the .NET runtime also have the possibility of executing synchronously when called; it's not uncommon. But yes, I agree these async methods do feel like a bit of an antipattern because it's reasonable to expect database queries to run asynchronously. Unfortunately, the native SQLite driver simply doesn't support it.

    I would rather have the async method throw a NotSupportedException

    This would break polymorphism in ADO.NET. Every upstream library would have to special case SQLite because it threw or just flat out not support it.

  18. bricelam commented on Nov 16, 2024

    @bricelam
    Contributor

    From the SQLite team:

    The use of WAL mode largely obviates the need for this asynchronous I/O module. Hence, this module is no longer supported.

  19. Daniel15 commented on Jan 19, 2025

    @Daniel15

    The native SQLite API is synchronous so I'm not sure how it'd be possible to resolve this issue. Probably the best thing that could be done is synchronous API calls on a thread pool thread, but I'm not convinced it'd actually be useful or help performance in any way. This is also why the best SQLite API for Node.js, better-sqlite, is synchronous. There's a note explaining their rationale in the readme: https://github.com/WiseLibs/better-sqlite3?tab=readme-ov-file#why-should-i-use-this-instead-of-node-sqlite3

  20. added theissue type on Jun 15, 2025
  21. BrensNina commented on Jun 6, 2026

    @BrensNina

    At least, this should be in EFCore docs like a note: "NOTE: If you are using efcoresqlite provider async methods are really sync and just exists for plug and play reasons for other providers. Read more link" for someone who waste a lot of time debugging when i was a newbie, that can be really helpful. Just my two cents

  22. changed the title [-]SQLite: Make async methods real asynchronous or at least add await Task.Yield() calling to them[/-] [+]SQLite: Make async methods call Task.Yield()[/+] on Aug 6, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions