MySqlConnector
MySqlConnector copied to clipboard
Excessive lock contention and CPU usage in TimerQueue
trafficstars
Software versions MySqlConnector version: 2.2.5 Server type (MySQL, MariaDB, Aurora, etc.) and version: Mysql 8 .NET version: 6 (Optional) ORM NuGet packages and versions:
Describe the bug A clear and concise description of what the bug is. With frequent reads of many rows from many concurrent async "threads", profiling shows excessive lock contention and CPU usage in TimerQueue.Add/Remove. This has led to thread-blocking issues for us, causing delays in async scheduling.
Exception Full exception message and call stack (if applicable) See repro steps
Code sample
- Clone https://github.com/saintarian/mysqlconnector-timerqueue-repro
- Run
docker compose build - Run
docker compose up - Let it run for ~15m
- Navigate to http://localhost:4040 to see profiling results. Choose
db-load-generator.mutex_durationanddb-load-generator.cpuin the dropdown at the top.
/* A concise code sample to reproduce the bug */
Expected behavior Less lock contention and CPU usage.
Additional context TimerQueue.Remove+Add happens in 3 places as far as I can tell:
- MySqlCommand.cs: SetTImeout seems to be called each time a row is read, causing lots of TimerQueue churn.
- SocketByteHandler.cs: Add+Remove is done each time IO is done
- StreamByteHandler.cs: Add+Remove is done each time IO is done