MySqlConnector icon indicating copy to clipboard operation
MySqlConnector copied to clipboard

Excessive lock contention and CPU usage in TimerQueue

Open saintarian opened this issue 2 years ago • 6 comments
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_duration and db-load-generator.cpu in 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

saintarian avatar Mar 09 '23 15:03 saintarian