Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Connection timeout if MySqlDataReader isn't disposed #306

bgrainger opened this issue Aug 7, 2017 · 2 comments

Connection timeout if MySqlDataReader isn't disposed #306

bgrainger opened this issue Aug 7, 2017 · 2 comments


Copy link

The following code will eventually throw a MySqlConnection "Connect Timeout expired" and leave the pool full of idle but unusable sessions.

while (true)
	var connection = new MySqlConnection(csb.ConnectionString);
	var cmd = connection.CreateCommand();
	cmd.CommandText = "SELECT 1";
	var reader = cmd.ExecuteReader();
        while (reader.Read()) { }

        GC.Collect(); // to try to force objects to be cleaned up

In MySqlConnector, this happens because there's a strong reference from MySqlSession to MySqlCommand and MySqlDataReader, and from those to MySqlConnection, so the WeakReference-based code never works.

This also fails in MySql.Data, so it's not a new bug. However, it would be nice to fix it so that "invalid" use of the library doesn't make connection pools unusable.

Copy link

nvivo commented Aug 7, 2017

This looks a lot like what is happening here. A some points during the day, I have these sudden bursts of mysql exceptions saying "Connect Timeout expired". I thought they were due to more connections being open with real async and the idle connections filling up the pool, but if that is the case, a GC happening at a very specific time could be cause this too.

bgrainger added a commit that referenced this issue Aug 7, 2017
The code to recover leaked sessions depends on MySqlConnection being GCed. If MySqlSession holds strong references to MySqlConnection (via MySqlCommand or MySqlDataReader), then failure to dispose a MySqlDataReader will leak the connection.
@bgrainger bgrainger self-assigned this Aug 7, 2017
Copy link
Member Author

Fixed in 0.24.1.

@nvivo Please test your scenario again with this new build (once it's available on NuGet, hopefully in a few minutes).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

No branches or pull requests

2 participants