Open janseris opened 2 years ago
@janseris thanks for bringing this up. We currently are busy with our preview release tasks, We will resume on this after the release. Thanks for your patience.
@JRahnama Hi, has the time come now? Thanks I am still exploring how SqlCommands and SqlConnections are cancelled and what are the possibilities and it cancelling a SqlCommand requires a roundtrip to the database etc. Do you maybe have a link to any documentation explaining this? I cannot find a lot of details other than by trial and error method such as how this bug/unexpected behavior was found.
I just checked with the team. This is a bug, and will need to do some investigation to see why the CancellationToken
is not passed into the correct context where it can stop the execution. Once we have a fix, we'll get back to you soon.
@janseris Can you provide more details on scenario what are you doing when warmup DB? do you stop SQL Server service to make it unreachable in between?
Can you provide repro with console app and connection string that you are using?
@janseris Can you provide more details on scenario what are you doing when warmup DB? do you stop SQL Server service to make it unreachable in between?
do you stop SQL Server service to make it unreachable in between?
No, I don't touch SQL Server. I reproduce the "no connection situation" by simulating or turning off the internet connection on the client device (e.g. Android phone in this case). If I remember correctly, turning off the internet connection is sufficient to reproduce this bug.
Edit: I reviewed my original post and in this scenario, the "warmup db call" is just a normal DB call.
Imagine that it is SELECT * FROM Users
and that it is called GetAllUsers()
(the actual code there is (SELECT 1 FROM Users
).
Can you provide connection string that you are using?
Connection string is casual: server address, db name, SQL login, SQL password. Nothing else.
Can you provide repro with console app?
I do not have a repro console app. Shouldn't be hard to create. Some IServiceCollection.AddDbContextFactory
etc.
In the call using (var db = DB)
, DB
is just a shortcut to dbContextFactory.CreateDbContext()
Can you provide repro with console app and connection string that you are using?
I am sorry it's been almost a year and I do not have that test app anymore.
@janseris I tried this with MAUI App with sqlclient directly against cloud server by turning internet OFF on client side as you mentioned. I am not seeing this happening. My code is attached. I tried the step as you specified, a warmup call and following call with token cancel request and I am only seeing expected exceptions depending when internet goes off on second call.
Rollback Exception Type: Microsoft.Data.SqlClient.SqlException
[DOTNET] Message: A transport-level error has occurred when receiving results from the server. (provider: TCP Provider, error: 35 - An internal exception was caught)
Thread finished:
Describe the bug
I am using a MAUI Blazor application. This occurs on Windows as well as on Android and I think it is not MAUI specific - it probably happens on any other platform (Console/WinForms, ...) The issue is that when DB server is unreachable (internet connection is OFF or the connection string is wrong), applying
CancellationToken
does nothing to stop/cancel connecting to the server. TheCancellationToken
is OK - I have tried debugging into the Data Access Layer EF Core function and the originalCancellationToken
is passed in as expected. The task itself is cancelled, but in the background,SqlClient
is spamming for approximately 20 seconds until it gives up.Code:
The code above calls this method in the end, deep in the layered architecture:
Output:
To reproduce
Call FirstOrDefaultAsync on DbContext with CancellationToken.
Expected behavior
CancellationToken cancels the database connection attempts.
Further technical details
latest previews Additional context Add any other context about the problem here.