Skip to content

Close() or Dispose() throws if Close() has already been called #620

Closed

Description

@Timovzl

When the data reader is disposed or closed after it has already been closed, it throws. This is undesirable and sometimes even problematic.

A disposable object should always (generally) be disposed. If one part of the process deems it correct to close the reader, but disposing it is not its responsibility, then another part of the process must be able to dispose it.

The above appears in particular when ExecuteReader(CommandBehavior.CloseConnection) is used, where disposing the reader closes the connection. The connection should still be disposed, but particularly if a class receives a reader through its constructor (as is my case), it cannot know if that reader will close its connection, so it has to dispose it.

Activity

  1. bgrainger commented on Mar 19, 2019

    @bgrainger
    Member

    Do you have sample code to reproduce the problem? I tried the following, without success:

    using (var connection = new MySqlConnection("..."))
    {
    	connection.Open();
    	using (var command = new MySqlCommand("SELECT 1;", connection))
    	using (var reader = command.ExecuteReader(CommandBehavior.CloseConnection))
    	{
    		while (reader.Read()) { }
    		reader.Close();
    		reader.Dispose();
    		reader.Close();
    	}
    }
  2. Timovzl commented on Mar 20, 2019

    @Timovzl
    Author

    Bugger, it must be more obscure.

    I am investigating. In doing so, I have found an issue where the connection should have been closed, but is still open. I am not entirely sure if this is the core of the original issue. My guess is it's not, and I'm still looking for that, but it might be.

    See the comments where I OpenAsync() the second time. This throws "Cannot Open when State is Open".

    using (TransactionScope outerScope = null) //CreateTransactionScope()) // Same effect as when using inner scope below
    using (var connection = new MySqlConnection("Server=ip;Uid=uid;Pwd=pwd;TreatTinyAsBoolean=False;UseAffectedRows=True;SslMode=None;UseXaTransactions=False;"))
    {
    	using (var innerScope = CreateTransactionScope())
    	{
    		using (var command1 = new MySqlCommand("SELECT 1"))
    		using (var command2 = new MySqlCommand("SELECT 2"))
    		{
    			command1.Connection = connection;
    			command2.Connection = connection;
    
    			await connection.OpenAsync();
    			using (var reader = await command1.ExecuteReaderAsync(CommandBehavior.CloseConnection, CancellationToken.None))
    			{
    				while (await reader.ReadAsync())
    				{
    					Console.WriteLine(reader.GetValue(0));
    				}
    			} // Disposal should close connection (at least from a functional perspective)
    
    			await connection.OpenAsync(); // Should be able to "reopen" connection (technically the same underlying open connection, because of non-XA)
    			using (var reader = await command2.ExecuteReaderAsync(CommandBehavior.CloseConnection, CancellationToken.None))
    			{
    				while (await reader.ReadAsync())
    				{
    					Console.WriteLine(reader.GetValue(0));
    				}
    			}
    		}
    	}
    }
  3. Timovzl commented on Mar 21, 2019

    @Timovzl
    Author

    I forgot to mention that the original exception on disposal was "Expected this MySqlConnection to have no ServerSession, but it was already attached". I have not been able to reproduce it yet, and will wait for what you discover based on my previous post.

  4. bgrainger commented on Mar 21, 2019

    @bgrainger
    Member

    Thanks; I can reproduce the problem now. I agree with your assessment that "Disposal should close connection (at least from a functional perspective)".

  5. Timovzl commented on Mar 29, 2019

    @Timovzl
    Author

    Works like a charm. :)

  6. bgrainger commented on Mar 29, 2019

    @bgrainger
    Member

    Fixed in 0.51.0.

  7. added theissue type on Jan 18, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions