SqliteCache icon indicating copy to clipboard operation
SqliteCache copied to clipboard

Internal use of async

Open AntonyCorbett opened this issue 3 years ago • 5 comments
trafficstars

What are your thoughts on async in Microsoft.Data.Sqlite.Core? I think the following advice is still relevant:

https://docs.microsoft.com/en-us/dotnet/standard/data/sqlite/async

It would be difficult to remove the public Async method signatures on SqliteCache, but perhaps the underlying database operations could be changed to use sync methods and so minimize the async overhead. It's only a very minor consideration.

AntonyCorbett avatar Mar 23 '22 17:03 AntonyCorbett

Thanks for opening this issue. I think this is a fair observation and certainly something doable. We can't remove the async wrapper methods even if we wanted to as they're required by the IDistributedCache interface we implement, but we can at least minimize the impact internally.

One question is how the sync api (in practice) handles cancellation as compared to the async api. Does the cancellation token for the async SQLite API basically do nothing?

mqudsi avatar Apr 06 '22 17:04 mqudsi

One question is how the sync api (in practice) handles cancellation as compared to the async api. Does the cancellation token for the async SQLite API basically do nothing?

Yes, it looks like we'd get Task.FromCanceled<> if the token cancellation is already requested on entry to the API, But subsequent cancellation is ignored.

AntonyCorbett avatar Apr 11 '22 09:04 AntonyCorbett

The real question is if there are any cases (not just the straightforward one) where subsequent cancellation works in the asynchronous API (since we already know that the async api is just a wrapper around the synchronous one) that don't work in the synchronous one.

As an aside, I'm very surprised that subsequent cancellation in the async API is completely ignored since sqlite3_exec takes a callback parameter and any sane application using the sqlite3 C/FFI API will avail itself of that to offer an option to abort long-running queries. I haven't dug into the Microsoft.Data.Sqlite.Core code, but I imagine it installs such a callback?

mqudsi avatar Apr 11 '22 22:04 mqudsi

I'm very surprised that subsequent cancellation in the async API is completely ignored

You're right - CancellationTokenRegistration is used to register a callback that cancels the execution.

AntonyCorbett avatar Apr 13 '22 07:04 AntonyCorbett

The observation that the cancellation token can be used to abort long-running statements makes the MS advice to avoid async Sqlite calls a little simplistic.

AntonyCorbett avatar Apr 18 '22 11:04 AntonyCorbett