Bug #90086 MySqlDataReader is closed by an unrelated command disposal
Submitted: 15 Mar 2018 10:10 Modified: 10 Aug 2022 18:43
Reporter: Frédéric Delaporte Email Updates:
Status: Duplicate Impact on me:
None 
Category:Connector / NET Severity:S3 (Non-critical)
Version:6.10.6 OS:Any
Assigned to: CPU Architecture:Any
Tags: DataReader, Dispose, MySqlCommand, MySqlDataReaded, ResetReader

[15 Mar 2018 10:10] Frédéric Delaporte
Description:
When disposing of a command, a data reader opened with another command is closed. This is quite unexpected, and this was not occurring with 6.9 version.

The offending change is https://github.com/mysql/mysql-connector-net/commit/ae13ac2dc3fd0da24c158b9a40a6e3362d27f0...

This bug has the same root cause than #89159

How to repeat:
- Open a command, do stuff with it but do not dispose of it yet.
- Open another command and open a reader from it.
- Dispose of the first command.
- Try using the reader.
- Blow with "reader is closed" error.

Suggested fix:
Do not close unrelated reader when disposing of a command.
[15 Mar 2018 11:11] Frédéric Delaporte
A simingly workaround would be to always close previous commands before opening a reader, or to not close previous commands before having finished working with an opened reader. But this forces to handle a somewhat undue coupling between command lifecycle and reader lifecycle.

Concrete case where this was discovered: polymorphic query with NHibernate returned as an Enumerable with deferred execution of reads on DataReader. Such a query results in as many commands as there are implementations of the queried entity, each command opening a reader with a safeguard in NHibernate enabled for databases not supporting multiple opened reader ensuring previously opened reader are read into memory and closed prior to the new reader opening. But the command of the reader transferred into memory is not disposed of at that moment due to separation of responsibilities concerns, but only a bit later, while another reader has already been opened.

For more information on the context in which this bug has been found and why this creates coupling issues to have to account for this bug, you may read this comment:
https://github.com/nhibernate/nhibernate-core/pull/1523#issuecomment-373338135
[16 Mar 2018 11:40] Chiranjeevi Battula
Hello FRÉDÉRIC,

Thank you for the bug report.
In order to submit contributions you must first sign the Oracle Contribution Agreement (OCA). For additional information please check http://www.oracle.com/technetwork/community/oca-486395.html.
If you have any questions, please contact the MySQL community team.

Thanks,
Chiranjeevi.
[7 Aug 2020 4:01] Bradley Grainger
This no longer reproduces in 8.0.21. 

It was probably fixed in 8.0.20 by https://github.com/mysql/mysql-connector-net/commit/32e2faa1a515801cab8fbf05e77e78827f13c7....
[10 Aug 2022 18:43] Daniel Valdez
Duplicated bug (Bug #89159)