Skip to main content

Fixed: "Timeout expired" on Every Connection with Connect Timeout=0 in MySql.Data

· 4 min read
Jagdish Kumawat
Founder @ Dewiride

Connect Timeout=0 looks as if it should mean "no limit", as it does for many timeouts. In MySql.Data, every pooled connection failed at once with Timeout expired, on a server with nothing else connected.

What I Was Doing​

I was looking for a way to stop the driver's connection timer, and tried a value of zero, on MySql.Data 26.10.0 and .NET 10:

Connection string
…;SslMode=Required;Connect Timeout=0
Terminal
dotnet run test-connection.cs

What I Expected​

A connection, with no time limit on connecting.

What I Got​

Output
Unhandled exception. MySql.Data.MySqlClient.MySqlException (0x80004005): error connecting: Timeout expired. The timeout period elapsed prior to obtaining a connection from the pool. This may have occurred because all pooled connections were in use and max pool size was reached

It failed in 75 milliseconds, without waiting for anything.

The test program stopping with error connecting: Timeout expired. The timeout period elapsed prior to obtaining a connection from the pool.

What I Checked​

Whether the pool was full. It wasn't. No other program was connected, and the same string without Connect Timeout=0 connected straight away.

Oracle's documentation describes Connect Timeout only as "The length of time (in seconds) to wait for a connection to the server before terminating the attempt and generating an error." It doesn't mention zero.

The driver's source, tag 26.10.0. The pool's wait is a loop on that number:

MySqlPool.cs
int fullTimeOut = (int)Settings.ConnectionTimeout * 1000;
int timeOut = fullTimeOut;
// ...
while (timeOut > 0)
{
Driver driver = await TryToGetDriverAsync(cancellationToken).ConfigureAwait(false);
if (driver != null) return driver;
// ...
}
throw new MySqlException(Resources.TimeoutGettingConnection);

With zero, the loop never runs, and it goes straight to the throw.

Why It Happens​

The value does two jobs in this driver: the time limit for connecting, and how long to wait for the pool. With a limit of zero, it never asks the pool for a connection at all.

The Fix​

Use a number. The default is 15. To fail faster when a server doesn't answer, pick something small:

Connection string
…;SslMode=Required;Connect Timeout=5

Pooling=false would also stop this error, because the pool's loop is skipped. Don't do that: every request would then pay for a new connection, as How to See the MySql.Data Connection Pool Working measures.

After the Fix​

Terminal
dotnet run test-connection.cs
Output
Connected as inventory_app@% to MySQL 9.7.4
TLS: TLSv1.2

The test program printing Connected as inventory_app@% with TLS: TLSv1.2, with a number in Connect Timeout.

Common Mistakes​

MistakeWhat happensDo this instead
Using 0 to mean "no limit"Every pooled connection failsUse a number
Reading this as a full poolYou raise Maximum Pool Size, and nothing changesCheck Connect Timeout first
Using a huge numberA dead server keeps requests waitingPick a sensible one

Troubleshooting​

SymptomLikely causeFix
The same message after waitingThe pool really is fullThe fix
The process ends with Unhandled exceptionA handshake was still running when the timer firedThe fix

Conclusion​

Connect Timeout=0 makes every pooled connection fail with Timeout expired. Use a number.

Additional Resources​

Stay Updated

Subscribe to our newsletter for the latest tutorials, tech insights, and developer news.

By subscribing, you agree to our privacy policy. Unsubscribe at any time.