Skip to main content

Fixed: "Server shutdown in progress" in EF Core with MySQL

· 4 min read
Jagdish Kumawat
Founder @ Dewiride

MySQL was restarting, and a statement in the middle of running failed with MySqlException: Server shutdown in progress. That's error 1053, it's temporary, and the server will be back in seconds. The fix is one number in the retry list.

What I Was Doing​

My .NET C# Web API uses EF Core 10.0.14 and MySql.Data 26.10.0. An ExecuteUpdateAsync was running while MySQL restarted:

Terminal
docker compose restart mysql

What I Expected​

A short wait, and then an answer.

What I Got​

Output (status, and how long it took)
500 1.106149s
API log
MySql.Data.MySqlClient.MySqlException (0x80004005): Server shutdown in progress

The request answering 500 after 1.1 seconds, with the log line MySqlException: Server shutdown in progress.

What I Checked​

The number. MySQL's server error reference lists it as error 1053, ER_SERVER_SHUTDOWN, with the message Server shutdown in progress.

Whether it was retried. No: it wasn't in my errorNumbersToAdd, and Oracle's provider retries nothing else.

Why It Happens​

When MySQL stops, it ends the statements that are running, and this is what it tells their connections. If the connection is cut before the message arrives, you get Reading from the stream has failed instead, so a restart can produce either one.

The Fix​

Add 1053 to the errors you retry:

src/Dewiride.Inventory.Data/MySqlRetryStrategy.cs (excerpt)
protected override bool ShouldRetryOn(Exception exception) =>
exception is MySqlException { Number: 0, InnerException: not null }
or MySqlException
{
Number:
1040 or // Too many connections.
1042 or // Unable to connect to any of the specified MySQL hosts.
1053 or // Server shutdown in progress.
1213 // Deadlock found: MySQL rolled the transaction back.
};

With EnableRetryOnFailure, that's errorNumbersToAdd: [1053], along with whichever others you need.

After the Fix​

Output (status, and how long it took)
{"changed":1}
curl exit 0, 5.502409 s, status 200
API log
A transient exception occurred during execution. The operation will be retried after 0ms.
A transient exception occurred during execution. The operation will be retried after 1044ms.
A transient exception occurred during execution. The operation will be retried after 3273ms.

The request answering 200 after 5.5 seconds, with three retries in the log while MySQL restarted.

It took 5.5 seconds, and it answered 200. The client just saw a slow request.

Common Mistakes​

MistakeWhat happensDo this instead
Listing only 1053The next attempts fail differently while MySQL is downList 0 and 1042 too
Retrying non-repeatable workA repeat can apply it twiceAdd a key
Too few retriesThe restart outlasts themAnswer 503 when they run out

Troubleshooting​

SymptomLikely causeFix
Unable to connect to any of the specified MySQL hostsMySQL is down nowThe fix
NullReferenceException during the restartA retry on a broken connectionThe fix
The maximum number of retries (3) was exceededThe restart took longer than the retriesThe fix

Conclusion​

Server shutdown in progress is error 1053: MySQL is stopping, and it'll be back. Retry it, with the other errors a restart causes.

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.