When server is unreachable, CancellationToken does not cancel the automatical retries
- Dominant language
- C#
- Stars
- 989
- Forks
- 340
- Avg merge
- 4d 19h
- Merged PRs (30d)
- 72
Description
### 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.
The `CancellationToken` is OK - I have tried debugging into the Data Access Layer EF Core function and the original `CancellationToken` 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.
```
A network-related or instance-specific error occurred while establishing a connection to SQL Server. The server was not found or was not accessible. Verify that the instance name is correct and that SQL Server is configured to allow remote connections. (provider: Named Pipes Provider, error: 40 - Could not open a connection to SQL Server)
Exception thrown: 'Microsoft.Data.SqlClient.SqlException' in Microsoft.Data.SqlClient.dll
```
Code:
```
async Task RunAsyncDbCall()
{
int limitMilliseconds = 3000;
cts = new CancellationTokenSource(limitMilliseconds /* ms */);
Loading = true;
var token = cts.Token;
var DBCall = DBWarmUpService.TryWarmupDB;
try
{
Trace.WriteLine($"-----[{DateTime.Now:HH:mm:ss.fff}] Trying WarmUp DB call with task cancellation timeout {limitMilliseconds} ms");
await DBCall(token);
Print($"---The task was completed [{DateTime.Now:HH:mm:ss.fff}]");
}
catch (OperationCanceledException)
{
Print($"---The task was cancelled [{DateTime.Now:HH:mm:ss.fff}]");
}
catch (Exception ex)
{
Trace.WriteLine(ExceptionHelper.GetExAndInnerExInfo(ex));
}
Loading = false; //in any case
}
```
The code above calls this method in the end, deep in the layered architecture:
```
public async Task DBWarmUpCall(CancellationToken token = default)
{
using (var db = DB)
{
var query =
from user in db.USER
select 1;
var result = await query.FirstOrDefaultAsync(token);
return result;
}
}
```
Output:
```internet connection status changed
thread handling the ConnectivityChanged event: 1
Internet connection profiles:
Network Access: Unknown
-----[01:55:55.162] Trying WarmUp DB call with task cancellation timeout 3000 ms
'Tempus.GUI.Mobile.MAUIBlazor.exe' (CoreCLR: clrhost): Loaded 'C:\Tempus.GUI.Mobile\Tempus.GUI.Mobile.Blazor\bin\Debug\net6.0-windows10.0.19041.0\win10-x64\AppX\System.Linq.Queryable.dll'.
'Tempus.GUI.Mobile.MAUIBlazor.exe' (CoreCLR: clrhost): Loaded 'C:\Tempus.GUI.Mobile\Tempus.GUI.Mobile.Blazor\bin\Debug\net6.0-windows10.0.19041.0\win10-x64\AppX\Microsoft.EntityFrameworkCore.SqlServer.dll'.
'Tempus.GUI.Mobile.MAUIBlazor.exe' (CoreCLR: clrhost): Loaded 'C:\Tempus.GUI.Mobile\Tempus.GUI.Mobile.Blazor\bin\Debug\net6.0-windows10.0.19041.0\win10-x64\AppX\System.Collections.Immutable.dll'.
'Tempus.GUI.Mobile.MAUIBlazor.exe' (CoreCLR: clrhost): Loaded 'C:\Tempus.GUI.Mobile\Tempus.GUI.Mobile.Blazor\bin\Debug\net6.0-windows10.0.19041.0\win10-x64\AppX\Microsoft.EntityFrameworkCore.Relational.dll'.
'Tempus.GUI.Mobile.MAUIBlazor.exe' (CoreCLR: clrhost): Loaded 'C:\Tempus.GUI.Mobile\Tempus.GUI.Mobile.Blazor\bin\Debug\net6.0-windows10.0.19041.0\win10-x64\AppX\Microsoft.EntityFrameworkCore.Abstractions.dll'.
'Tempus.GUI.Mobile.MAUIBlazor.exe' (CoreCLR: clrhost): Loaded 'C:\Tempus.GUI.Mobile\Tempus.GUI.Mobile.Blazor\bin\Debug\net6.0-windows10.0.19041.0\win10-x64\AppX\System.ComponentModel.Primitives.dll'.
'Tempus.GUI.Mobile.MAUIBlazor.exe' (CoreCLR: clrhost): Loaded 'C:\Tempus.GUI.Mobile\Tempus.GUI.Mobile.Blazor\bin\Debug\net6.0-windows10.0.19041.0\win10-x64\AppX\Microsoft.Extensions.Caching.Abstractions.dll'.
'Tempus.GUI.Mobile.MAUIBlazor.exe' (CoreCLR: clrhost): Loaded 'C:\Tempus.GUI.Mobile\Tempus.GUI.Mobile.Blazor\bin\Debug\net6.0-windows10.0.19041.0\win10-x64\AppX\System.Diagnostics.DiagnosticSource.dll'.
'Tempus.GUI.Mobile.MAUIBlazor.exe' (CoreCLR: clrhost): Loaded 'C:\Tempus.GUI.Mobile\Tempus.GUI.Mobile.Blazor\bin\Debug\net6.0-windows10.0.19041.0\win10-x64\AppX\System.Transactions.Local.dll'.
'Tempus.GUI.Mobile.MAUIBlazor.exe' (CoreCLR: clrhost): Loaded 'Anonymously Hosted DynamicMethods Assembly'.
'Tempus.GUI.Mobile.MAUIBlazor.exe' (CoreCLR: clrhost): Loaded 'C:\Tempus.GUI.Mobile\Tempus.GUI.Mobile.Blazor\bin\Debug\net6.0-windows10.0.19041.0\win10-x64\AppX\Microsoft.Extensions.Caching.Memory.dll'.
'Tempus.GUI.Mobile.MAUIBlazor.exe' (CoreCLR: clrhost): Loaded 'C:\Tempus.GUI.Mobile\Tempus.GUI.Mobile.Blazor\bin\Debug\net6.0-windows10.0.19041.0\win10-x64\AppX\System.ComponentModel.Annotations.dll'.
'Tempus.GUI.Mobile.MAUIBlazor.exe' (CoreCLR: clrhost): Loaded 'C:\Tempus.GUI.Mobile\Tempus.GUI.Mobile.Blazor\bin\Debug\net6.0-windows10.0.19041.0\win10-x64\AppX\System.Net.NetworkInformation.dll'.
'Tempus.GUI.Mobile.MAUIBlazor.exe' (CoreCLR: clrhost): Loaded 'C:\Tempus.GUI.Mobile\Tempus.GUI.Mobile.Blazor\bin\Debug\net6.0-windows10.0.19041.0\win10-x64\AppX\System.Runtime.Extensions.dll'.
'Tempus.GUI.Mobile.MAUIBlazor.exe' (CoreCLR: clrhost): Loaded 'C:\Tempus.GUI.Mobile\Tempus.GUI.Mobile.Blazor\bin\Debug\net6.0-windows10.0.19041.0\win10-x64\AppX\System.Threading.Tasks.dll'.
'Tempus.GUI.Mobile.MAUIBlazor.exe' (CoreCLR: clrhost): Loaded 'C:\Tempus.GUI.Mobile\Tempus.GUI.Mobile.Blazor\bin\Debug\net6.0-windows10.0.19041.0\win10-x64\AppX\System.Threading.Timer.dll'.
'Tempus.GUI.Mobile.MAUIBlazor.exe' (CoreCLR: clrhost): Loaded 'C:\Tempus.GUI.Mobile\Tempus.GUI.Mobile.Blazor\bin\Debug\net6.0-windows10.0.19041.0\win10-x64\AppX\System.Resources.ResourceManager.dll'.
'Tempus.GUI.Mobile.MAUIBlazor.exe' (CoreCLR: clrhost): Loaded 'C:\Tempus.GUI.Mobile\Tempus.GUI.Mobile.Blazor\bin\Debug\net6.0-windows10.0.19041.0\win10-x64\AppX\System.Configuration.ConfigurationManager.dll'.
'Tempus.GUI.Mobile.MAUIBlazor.exe' (CoreCLR: clrhost): Loaded 'C:\Tempus.GUI.Mobile\Tempus.GUI.Mobile.Blazor\bin\Debug\net6.0-windows10.0.19041.0\win10-x64\AppX\System.Collections.Specialized.dll'.
'Tempus.GUI.Mobile.MAUIBlazor.exe' (CoreCLR: clrhost): Loaded 'C:\Tempus.GUI.Mobile\Tempus.GUI.Mobile.Blazor\bin\Debug\net6.0-windows10.0.19041.0\win10-x64\AppX\System.Net.WebClient.dll'.
'Tempus.GUI.Mobile.MAUIBlazor.exe' (CoreCLR: clrhost): Loaded 'C:\Tempus.GUI.Mobile\Tempus.GUI.Mobile.Blazor\bin\Debug\net6.0-windows10.0.19041.0\win10-x64\AppX\System.ComponentModel.EventBasedAsync.dll'.
'Tempus.GUI.Mobile.MAUIBlazor.exe' (CoreCLR: clrhost): Loaded 'C:\Tempus.GUI.Mobile\Tempus.GUI.Mobile.Blazor\bin\Debug\net6.0-windows10.0.19041.0\win10-x64\AppX\Microsoft.Identity.Client.dll'.
'Tempus.GUI.Mobile.MAUIBlazor.exe' (CoreCLR: clrhost): Loaded 'C:\Tempus.GUI.Mobile\Tempus.GUI.Mobile.Blazor\bin\Debug\net6.0-windows10.0.19041.0\win10-x64\AppX\System.Text.Encoding.CodePages.dll'.
'Tempus.GUI.Mobile.MAUIBlazor.exe' (CoreCLR: clrhost): Loaded 'C:\Tempus.GUI.Mobile\Tempus.GUI.Mobile.Blazor\bin\Debug\net6.0-windows10.0.19041.0\win10-x64\AppX\Microsoft.Win32.Primitives.dll'.
Exception thrown: 'Microsoft.Data.SqlClient.SqlException' in Microsoft.Data.SqlClient.dll
A network-related or instance-specific error occurred while establishing a connection to SQL Server. The server was not found or was not accessible. Verify that the instance name is correct and that SQL Server is configured to allow remote connections. (provider: Named Pipes Provider, error: 40 - Could not open a connection to SQL Server)
Exception thrown: 'Microsoft.Data.SqlClient.SqlException' in Microsoft.Data.SqlClient.dll
A network-related or instance-specific error occurred while establishing a connection to SQL Server. The server was not found or was not accessible. Verify that the instance name is correct and that SQL Server is configured to allow remote connections. (provider: Named Pipes Provider, error: 40 - Could not open a connection to SQL Server)
Exception thrown: 'Microsoft.Data.SqlClient.SqlException' in Microsoft.Data.SqlClient.dll
A network-related or instance-specific error occurred while establishing a connection to SQL Server. The server was not found or was not accessible. Verify that the instance name is correct and that SQL Server is configured to allow remote connections. (provider: Named Pipes Provider, error: 40 - Could not open a connection to SQL Server)
Exception thrown: 'System.Threading.Tasks.TaskCanceledException' in System.Private.CoreLib.dll
A task was canceled.
Exception thrown: 'System.Threading.Tasks.TaskCanceledException' in System.Private.CoreLib.dll
A task was canceled.
Exception thrown: 'Microsoft.Data.SqlClient.SqlException' in Microsoft.Data.SqlClient.dll
A network-related or instance-specific error occurred while establishing a connection to SQL Server. The server was not found or was not accessible. Verify that the instance name is correct and that SQL Server is configured to allow remote connections. (provider: Named Pipes Provider, error: 40 - Could not open a connection to SQL Server)
Exception thrown: 'System.Threading.Tasks.TaskCanceledException' in System.Private.CoreLib.dll
A task was canceled.
Exception thrown: 'System.Threading.Tasks.TaskCanceledException' in System.Private.CoreLib.dll
A task was canceled.
Exception thrown: 'System.Threading.Tasks.TaskCanceledException' in System.Private.CoreLib.dll
A task was canceled.
Exception thrown: 'System.Threading.Tasks.TaskCanceledException' in System.Private.CoreLib.dll
A task was canceled.
Exception thrown: 'System.Threading.Tasks.TaskCanceledException' in System.Private.CoreLib.dll
A task was canceled.
Exception thrown: 'System.Threading.Tasks.TaskCanceledException' in Microsoft.EntityFrameworkCore.Relational.dll
A task was canceled.
Exception thrown: 'System.Threading.Tasks.TaskCanceledException' in System.Private.CoreLib.dll
A task was canceled.
Exception thrown: 'System.Threading.Tasks.TaskCanceledException' in System.Private.CoreLib.dll
A task was canceled.
Exception thrown: 'System.Threading.Tasks.TaskCanceledException' in System.Private.CoreLib.dll
A task was canceled.
Exception thrown: 'System.Threading.Tasks.TaskCanceledException' in System.Private.CoreLib.dll
A task was canceled.
Exception thrown: 'System.Threading.Tasks.TaskCanceledException' in System.Private.CoreLib.dll
A task was canceled.
Exception thrown: 'System.Threading.Tasks.TaskCanceledException' in System.Private.CoreLib.dll
A task was canceled.
---The task was cancelled [01:55:59.130]
Exception thrown: 'Microsoft.Data.SqlClient.SqlException' in Microsoft.Data.SqlClient.dll
A network-related or instance-specific error occurred while establishing a connection to SQL Server. The server was not found or was not accessible. Verify that the instance name is correct and that SQL Server is configured to allow remote connections. (provider: Named Pipes Provider, error: 40 - Could not open a connection to SQL Server)
Exception thrown: 'Microsoft.Data.SqlClient.SqlException' in Microsoft.Data.SqlClient.dll
A network-related or instance-specific error occurred while establishing a connection to SQL Server. The server was not found or was not accessible. Verify that the instance name is correct and that SQL Server is configured to allow remote connections. (provider: Named Pipes Provider, error: 40 - Could not open a connection to SQL Server)
Exception thrown: 'Microsoft.Data.SqlClient.SqlException' in Microsoft.Data.SqlClient.dll
A network-related or instance-specific error occurred while establishing a connection to SQL Server. The server was not found or was not accessible. Verify that the instance name is correct and that SQL Server is configured to allow remote connections. (provider: Named Pipes Provider, error: 40 - Could not open a connection to SQL Server)
Exception thrown: 'Microsoft.Data.SqlClient.SqlException' in Microsoft.Data.SqlClient.dll
A network-related or instance-specific error occurred while establishing a connection to SQL Server. The server was not found or was not accessible. Verify that the instance name is correct and that SQL Server is configured to allow remote connections. (provider: Named Pipes Provider, error: 40 - Could not open a connection to SQL Server)
Exception thrown: 'Microsoft.Data.SqlClient.SqlException' in Microsoft.Data.SqlClient.dll
A network-related or instance-specific error occurred while establishing a connection to SQL Server. The server was not found or was not accessible. Verify that the instance name is correct and that SQL Server is configured to allow remote connections. (provider: Named Pipes Provider, error: 40 - Could not open a connection to SQL Server)
Exception thrown: 'Microsoft.Data.SqlClient.SqlException' in Microsoft.Data.SqlClient.dll
A network-related or instance-specific error occurred while establishing a connection to SQL Server. The server was not found or was not accessible. Verify that the instance name is correct and that SQL Server is configured to allow remote connections. (provider: Named Pipes Provider, error: 40 - Could not open a connection to SQL Server)
Exception thrown: 'Microsoft.Data.SqlClient.SqlException' in Microsoft.Data.SqlClient.dll
A network-related or instance-specific error occurred while establishing a connection to SQL Server. The server was not found or was not accessible. Verify that the instance name is correct and that SQL Server is configured to allow remote connections. (provider: Named Pipes Provider, error: 40 - Could not open a connection to SQL Server)
Exception thrown: 'Microsoft.Data.SqlClient.SqlException' in Microsoft.Data.SqlClient.dll
A network-related or instance-specific error occurred while establishing a connection to SQL Server. The server was not found or was not accessible. Verify that the instance name is correct and that SQL Server is configured to allow remote connections. (provider: Named Pipes Provider, error: 40 - Could not open a connection to SQL Server)
The thread 0xdcc has exited with code 0 (0x0).
The thread 0x4118 has exited with code 0 (0x0).
Exception thrown: 'Microsoft.Data.SqlClient.SqlException' in Microsoft.Data.SqlClient.dll
A network-related or instance-specific error occurred while establishing a connection to SQL Server. The server was not found or was not accessible. Verify that the instance name is correct and that SQL Server is configured to allow remote connections. (provider: Named Pipes Provider, error: 40 - Could not open a connection to SQL Server)
Exception thrown: 'Microsoft.Data.SqlClient.SqlException' in Microsoft.Data.SqlClient.dll
A network-related or instance-specific error occurred while establishing a connection to SQL Server. The server was not found or was not accessible. Verify that the instance name is correct and that SQL Server is configured to allow remote connections. (provider: Named Pipes Provider, error: 40 - Could not open a connection to SQL Server)
Exception thrown: 'Microsoft.Data.SqlClient.SqlException' in Microsoft.Data.SqlClient.dll
A network-related or instance-specific error occurred while establishing a connection to SQL Server. The server was not found or was not accessible. Verify that the instance name is correct and that SQL Server is configured to allow remote connections. (provider: Named Pipes Provider, error: 40 - Could not open a connection to SQL Server)
Exception thrown: 'Microsoft.Data.SqlClient.SqlException' in Microsoft.Data.SqlClient.dll
A network-related or instance-specific error occurred while establishing a connection to SQL Server. The server was not found or was not accessible. Verify that the instance name is correct and that SQL Server is configured to allow remote connections. (provider: Named Pipes Provider, error: 40 - Could not open a connection to SQL Server)
Exception thrown: 'Microsoft.Data.SqlClient.SqlException' in Microsoft.Data.SqlClient.dll
A network-related or instance-specific error occurred while establishing a connection to SQL Server. The server was not found or was not accessible. Verify that the instance name is correct and that SQL Server is configured to allow remote connections. (provider: Named Pipes Provider, error: 40 - Could not open a connection to SQL Server)
Exception thrown: 'Microsoft.Data.SqlClient.SqlException' in Microsoft.Data.SqlClient.dll
A network-related or instance-specific error occurred while establishing a connection to SQL Server. The server was not found or was not accessible. Verify that the instance name is correct and that SQL Server is configured to allow remote connections. (provider: Named Pipes Provider, error: 40 - Could not open a connection to SQL Server)
Exception thrown: 'Microsoft.Data.SqlClient.SqlException' in Microsoft.Data.SqlClient.dll
A network-related or instance-specific error occurred while establishing a connection to SQL Server. The server was not found or was not accessible. Verify that the instance name is correct and that SQL Server is configured to allow remote connections. (provider: Named Pipes Provider, error: 40 - Could not open a connection to SQL Server)
Exception thrown: 'Microsoft.Data.SqlClient.SqlException' in Microsoft.Data.SqlClient.dll
A network-related or instance-specific error occurred while establishing a connection to SQL Server. The server was not found or was not accessible. Verify that the instance name is correct and that SQL Server is configured to allow remote connections. (provider: Named Pipes Provider, error: 40 - Could not open a connection to SQL Server)
Exception thrown: 'Microsoft.Data.SqlClient.SqlException' in Microsoft.Data.SqlClient.dll
A network-related or instance-specific error occurred while establishing a connection to SQL Server. The server was not found or was not accessible. Verify that the instance name is correct and that SQL Server is configured to allow remote connections. (provider: Named Pipes Provider, error: 40 - Could not open a connection to SQL Server)
Exception thrown: 'Microsoft.Data.SqlClient.SqlException' in Microsoft.Data.SqlClient.dll
A network-related or instance-specific error occurred while establishing a connection to SQL Server. The server was not found or was not accessible. Verify that the instance name is correct and that SQL Server is configured to allow remote connections. (provider: Named Pipes Provider, error: 40 - Could not open a connection to SQL Server)```
```
### 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.
Contributor guide
Assessment
This issue has not been assessed yet.