You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
SQLite: Make async methods call Task.Yield() #28279
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:
privateasyncTaskAddProduct(){_message="Adding product...";//this message is showing inside Blazor componentawaitTask.Yield();//I have to call this all the time before call dbContext's pseudo-async methods to properly update UIawaitDbContext.Products.AddAsync(product);awaitDbContext.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.
Reacted by Bernd Hirschmann, Alex, Kurren Nischal, Vasily Sidorov, Philipp Wagner, Pespego, Wuxriff, BrensNina and Brycen G
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.
@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.
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.
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.
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?
Reacted by Burak Büyükatlı, kaykaul, Philipp Wagner and BrensNina
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. :-)
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.
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?
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.
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.
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
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
Activity
ajcvickers commentedon Jun 29, 2022
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.
rodion-m commentedon Jun 29, 2022
@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.
msschl commentedon Feb 10, 2023
Wow that’s a bummer. I didn‘t know that those async methods are not really async under the cover.
bricelam commentedon Apr 4, 2023
See also Async limitations in the docs
bricelam commentedon Apr 4, 2023
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.
roji commentedon Apr 4, 2023
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.
roji commentedon Apr 4, 2023
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?
ajcvickers commentedon Apr 5, 2023
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. :-)
AderitoSilva commentedon Nov 16, 2024
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.bazzilic commentedon Nov 16, 2024
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?
bricelam commentedon Nov 16, 2024
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.
AderitoSilva commentedon Nov 16, 2024
@bazzilic
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.
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
NotSupportedExceptionand fail fast.bricelam commentedon Nov 16, 2024
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.
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.
bricelam commentedon Nov 16, 2024
From the SQLite team:
Daniel15 commentedon Jan 19, 2025
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-sqlite3BrensNina commentedon Jun 6, 2026
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
[-]SQLite: Make async methods real asynchronous or at least add await Task.Yield() calling to them[/-][+]SQLite: Make async methods call Task.Yield()[/+]