Bug #111916 Call ExecuteReaderAsync in an async Task and get StackOverflowException
Submitted: 28 Jul 2023 22:29 Modified: 10 Nov 2023 12:58
Reporter: Jason Hughes Email Updates:
Status: No Feedback Impact on me:
None 
Category:Connector / NET Severity:S1 (Critical)
Version:8.1.0 OS:Any
Assigned to: MySQL Verification Team CPU Architecture:Any

[28 Jul 2023 22:29] Jason Hughes
Description:
This function snippet works fine in 8.0.31 and probably 8.0.33, but I upgraded to 8.1.0 and it gives me a StackOverflowException.  Does not matter whether I call this task and await it or if I .GetAwaiter().GetResult(), the StackOverflowException happens regardless.  If I simply change this function to use ExecuteReader it works fine.  Something is busted in 8.1.0 version of the nuget package.

How to repeat:
private const string kCreateTableQuery  = $"CREATE TABLE IF NOT EXISTS {IStorage.kTableName} (privateId VARCHAR(255) CHARACTER SET binary NOT NULL PRIMARY KEY, cas INT, blobSize INT, {IStorage.kColumnName} MEDIUMBLOB NOT NULL);";

private const string kSelectQuery       = $"SELECT cas,blobSize,{IStorage.kColumnName} FROM {IStorage.kTableName} WHERE privateId = @pid;";

//**** somewhere you have to create the table first:
MySqlHelper.ExecuteNonQuery(_mysqlConnectionString, kCreateTableQuery);

public async Task<T> GetById<T>(string pid)
{
	MySqlParameter[] Parameters = new MySqlParameter[1];
        Parameters[0] = new MySqlParameter("@pid", pid.ToString());
        MySqlDataReader reader = await MySqlHelper.ExecuteReaderAsync(_mysqlConnectionString, kSelectQuery, Parameters);
[28 Jul 2023 22:42] Jason Hughes
This is working in 8.0.32 but broken in 8.0.33, just verified it.
[29 Jul 2023 17:49] Bradley Grainger
This is a bug on this line: https://github.com/mysql/mysql-connector-net/blob/8.0.33/MySQL.Data/src/MySqlHelper.cs#L26...

The method calls itself in an infinite loop until it crashes with a StackOverflowException.

Jason, you should be able to work around it by calling the overload that takes a CancellationToken:

MySqlDataReader reader = await MySqlHelper.ExecuteReaderAsync(_mysqlConnectionString, kSelectQuery, CancellationToken.None, Parameters);
[10 Oct 2023 12:58] MySQL Verification Team
Hello Jason,

Thank you for the bug report.
Please try the workaround suggested by Bradley Grainger and let us know if it is working. Thanks.

Regards,
Ashwini Patil
[10 Oct 2023 13:56] Bradley Grainger
I gave Jason the workaround as something to use in the meantime (until the bug is fixed), not as a permanent solution.

This bug's status should be set to "Verified" and scheduled to be fixed. The public API currently contains methods that crash when invoked.
[11 Nov 2023 1:00] Bugs System
No feedback was provided for this bug for over a month, so it is
being suspended automatically. If you are able to provide the
information that was originally requested, please do so and change
the status of the bug back to "Open".