Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4072 pages
Databases and Redis

MySqlException: Timeout expired in a .NET app

ErrorMySql.Data.MySqlClient.MySqlException: Timeout expired

Short answer

Timeout expired means a wait ended before it finished. Two waits matter in .NET: a command that runs longer than Default Command Timeout (30 seconds), and a connection request that waits for a pooled connection until Connection Timeout. Find which one fits, then fix the query or the connection use.

  • Applies to MySQL 8.4, MySqlConnector 2.x, .NET 8
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Fix it fast

  1. 1Find out which cause you have

    Look at the server's running statements while the error happens. Long-running queries and a pile of sleeping connections tell the two causes apart.

    SHOW FULL PROCESSLIST;
  2. 2Match what you see, then jump to its fix

    1. 1The failing statement shows in the process list as running for 30 seconds or moreThe query runs longer than the command timeoutFix
    2. 2The process list fills with many Sleep connections from the app and new opens waitConnections are leaked, so the pool runs outFix
    3. 3Timeouts appear only with parallel work or inside a reader loopOne connection is used by several operations at onceFix

What does the error look like?

  • MySql.Data.MySqlClient.MySqlException: Timeout expired

Cause 1 of 3

The query runs longer than the command timeout

1Check

Default Command Timeout is 30 seconds. After that the command is cancelled on the server. Zero turns the timeout off.

SHOW FULL PROCESSLIST;

2Fix

Fix the query first: add the missing index or cut the amount of data. Only if the work is legitimately slow, raise Default Command Timeout in the connection string.

Server=db.example.com;Database=app;User ID=app;Password=...;Default Command Timeout=120

3Verify

Run the slow operation again.

EXPLAIN SELECT ... ;

Confirmed by mysqlconnector.net

Did this fix it?

Cause 2 of 3

Connections are leaked, so the pool runs out

1Check

Pooling is on by default and the maximum is 100 connections (Maximum Pool Size in MySqlConnector). A connection that is not disposed never goes back to the pool.

SHOW STATUS LIKE 'Threads_connected';

2Fix

Create the connection in a using block for each operation instead of keeping a global connection that you open and close by hand. Dispose returns it to the pool.

using (var conn = new MySqlConnection(connectionString))
{
    conn.Open();
    // run commands here
}

3Verify

The number of connections stays flat while the app is under load.

SHOW STATUS LIKE 'Threads_connected';

Confirmed by dev.mysql.com

Did this fix it?

Cause 3 of 3

One connection is used by several operations at once

1Check

A MySqlConnection runs one operation at a time. Parallel calls on one connection, or a new command inside a reader.Read() loop, are not allowed.

grep -R -n "Task.WhenAll" --include=*.cs .

2Fix

Open a separate connection per parallel operation, or await them in sequence. Read the values into memory and close the reader before the next command.

await using var conn = new MySqlConnection(connectionString);
await conn.OpenAsync();

3Verify

Repeat the parallel workload.

dotnet test

Confirmed by mysqlconnector.net

Did this fix it?

How do I stop it coming back?

  • Dispose every connection and reader with using or await using.
  • Watch Threads_connected on the server so a leak shows before the pool is empty.
  • Fix slow queries with an index or less data before raising any timeout.

Sources

How was this page made?

I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.

Which stack is behind this page?

Projects of mine built on the same technologies:

Related fixes

Next step

Want this handled for good?

I look after servers, apps and releases end to end. Tell me what keeps breaking.