2nd connection to mirrored server with timeout==0 hangs forever.
Customer reported a bug for hanging connection to mirrored SQL Server. When customer creates and opens 2 connections one after another to mirrored server with connection timeout set to 0, the 2nd connection hangs infinitely.
Why does the 2nd connection hang?
The issue happened because native networking code called 'Native SNI' does not provide session handle, and just returns null. Here is the reason. After debugging, I noticed timeout==0 value is handed over to Native SNI. timeout==0 means 'infinity for connection time' in SqlClient side. But in Native SNI, timeout==0 means 'No time remained for connection time'. timeout==-1 is recognized as 'infinity' in Native SNI.
For this reason, timeout==0 is usually translated to timeout==-1 when the value is forwarded to Native SNI in non-mirroring case. However, in mirroring environment, timeout==0 is not just simply translated to timeout==-1 because we don't want to make SqlClient keep trying to connect to primary SQL server instance infinitely when the primary is not available. We want SqlClient to try to connect primary instance first, and then switch over to secondary instance when the primary has problem. In that case, timeout==0 is translated to appropriate positive values (depending on number of attempts) for the remaining time to connect the primary instance or minor instances. The timeout value translation is based on an algorithm that calculates appropriate connection time in mirroring environment.
There is a bug in the timeout calculation procedure. The bug truncates the calculation result to 0, and it is only triggered when timeout==0 (timeout.IsInfinite) value is given.
https://github.com/dotnet/corefx/blob/a27929935d2782cdc3e7be21653e8c4530274c6f/src/System.Data.SqlClient/src/System/Data/SqlClient/SqlInternalConnectionTds.cs#L1416-L1421
Since timeout==0 is translated to timeout==0 due to the truncation, Native SNI receives timeout==0, and it just return null because it thinks there is no time remained.
Because SqlClient failed to connect primary (as Native SNI returns null), it tries to connect to secondary instance. But the same thing happens again, and it failed to connect the secondary instance as well. SqlClient keeps trying to connect to the primary or minor servers in finite loop since timeout==0 is set by user.
https://github.com/dotnet/corefx/blob/a27929935d2782cdc3e7be21653e8c4530274c6f/src/System.Data.SqlClient/src/System/Data/SqlClient/SqlInternalConnectionTds.cs#L1429-L1444
As a result of this behavior, the 2nd connection hangs.
Then, why didn’t the 1st connection hang?
In the first connection attempt, regardless the server is actually being mirrored or not, SqlClient assumes there is no mirroring going on unless there is indication in connection string by failover partner keyword. Therefore, even if the server is actually being mirrored, SqlClient just translates timeout==0 to timeout==-1 for Native SNI when user does not specify failover partnet in connection string. Native SNI knows timeout==-1 means infinite time remained for connection, and it returns session handle correctly for the first connection attempt even in mirroring environment.
After 1st connection is created successfully, SqlClient caches the database information, and gets to know the database is being mirrored. Then it starts to use the timeout value calculation for translation instead of just translating to timeout==-1 for 2nd connection because SqlClient now knows the database is being mirrored. For this reason the 1st connection didn't hang.
For more information: https://docs.microsoft.com/en-us/dotnet/framework/data/adonet/sql/database-mirroring-in-sql-server
Fix merged to main branch.