Skip to content

Instantly share code, notes, and snippets.

@alex-jitbit
Last active June 11, 2024 21:37
Show Gist options
  • Save alex-jitbit/1eca9a1f014e036691bdc35cd852c726 to your computer and use it in GitHub Desktop.
Save alex-jitbit/1eca9a1f014e036691bdc35cd852c726 to your computer and use it in GitHub Desktop.

This is regarding dotnet/SqlClient#2378

using Microsoft.Data.SqlClient;

namespace SqlTest
{
	internal class Program
	{
		private static string _connectionString = "user id=MyUser;data source=172.24.144.1,1433;initial catalog=MYDATABASE;password=PASSWORD;Encrypt=false";

		static async Task Main(string[] args)
		{
			Console.WriteLine("Let's go");

			var taskList = new List<Task>();

			for (int i = 0; i < 50; i++)
			{
				taskList.Add(Task.Run(OpenConn));
			}
			await Task.WhenAll(taskList);

			Console.WriteLine("done, press enter to close");
			Console.ReadLine();
		}

		private static async Task OpenConn()
		{
			using (var cn = new SqlConnection(_connectionString))
			{
				cn.Open();
				await Task.Delay(100);
			}
		}
	}
}

The above code when used with Microsoft.Data.SqlClient 5.2.1 fails ON LINUX but runs fine ON WINDOWS under .NET 8 default empty Console project.

The bug is reproducable under WSL2 on Windows (the IP address is the Windows host-machine IP visible from WSL2, can be obtained from /etc/resolv.conf), make sure to select "WSL" in Visual Studio and press F5. In that case the exception thrown is System.InvalidOperationException: Timeout expired. The timeout period elapsed prior to obtaining a connection from the pool. Increasing the pool size to 250 in the connection string does not help, it still fails on 50 iterations.

On windows everything works fine & fast.

The above code works fine on both Windows and Linux when using Microsoft.Data.SqlClient 5.1.5

P.S. I'm intentionally using Task.Run here since I need the code to "emulate" ASP.NET bahavior, where requests run on thread pool

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment