Dapper icon indicating copy to clipboard operation
Dapper copied to clipboard

InvalidProgramException: "Common Language Runtime detected an invalid program. (Dapper version:1.60.6)

Open shajiltetherfi opened this issue 1 year ago • 15 comments

2024-10-17 13:54:41,154 [39] INFO Core.ORM.MySqlAdapter - Connection Opened: 2024-10-17 13:54:41,155 [39] INFO Core.ORM.MySqlAdapter - Start Connection Execute with: CREATE TEMPORARY TABLE TmpIn_CMM_Role_Pages_6541bc52679842279c38e65b978d250b SELECT RoleID , PageName , USER_ACCESS , ADD_ACCESS , EDIT_ACCESS , DELETE_ACCESS , EXPORT_ACCESS , LAST_CHANGED_BY , LAST_CHANGED_ON FROM CMM_Role_Pages target LIMIT 0;: 2024-10-17 13:54:41,160 [39] ERROR Core.ORM.MySqlAdapter - Catch 2: System.InvalidProgramException: Common Language Runtime detected an invalid program. at MySqlConnector.MySqlCommand.ExecuteNonQueryAsync(IOBehavior ioBehavior, CancellationToken cancellationToken) at MySqlConnector.MySqlCommand.ExecuteNonQuery() in /_/src/MySqlConnector/MySqlCommand.cs:line 108 at Dapper.SqlMapper.ExecuteCommand(IDbConnection cnn, CommandDefinition& command, Action2 paramReader) in C:\projects\dapper\Dapper\SqlMapper.cs:line 2827 at Dapper.SqlMapper.ExecuteImpl(IDbConnection cnn, CommandDefinition& command) in C:\projects\dapper\Dapper\SqlMapper.cs:line 570 at Core.ORM.MySqlAdapter.BulkUpsert[T](IDbConnection connection, IDbTransaction transaction, IEnumerable1 data, String tableName, Type type, Int32 bulkCopyTimeout, Nullable1 commandTimeout): 2024-10-17 13:54:41,161 [39] ERROR OCM.Utilities.SqlDataAccess - ExecuteBulkUpsertQuery:System.InvalidProgramException: Common Language Runtime detected an invalid program. at MySqlConnector.MySqlCommand.ExecuteNonQueryAsync(IOBehavior ioBehavior, CancellationToken cancellationToken) at MySqlConnector.MySqlCommand.ExecuteNonQuery() in /_/src/MySqlConnector/MySqlCommand.cs:line 108 at Dapper.SqlMapper.ExecuteCommand(IDbConnection cnn, CommandDefinition& command, Action2 paramReader) in C:\projects\dapper\Dapper\SqlMapper.cs:line 2827 at Dapper.SqlMapper.ExecuteImpl(IDbConnection cnn, CommandDefinition& command) in C:\projects\dapper\Dapper\SqlMapper.cs:line 570 at Core.ORM.MySqlAdapter.BulkUpsert[T](IDbConnection connection, IDbTransaction transaction, IEnumerable1 data, String tableName, Type type, Int32 bulkCopyTimeout, Nullable1 commandTimeout) at Core.ORM.DapperExtensions.BulkUpsert[T](IDbConnection connection, IEnumerable1 data, IDbTransaction transaction, Int32 batchSize, Int32 bulkCopyTimeout, Nullable1 commandTimeout) at OCM.Utilities.SqlDataAccess.ExecuteBulkUpsertQuery[T](IEnumerable`1 obj)

shajiltetherfi avatar Oct 16 '24 16:10 shajiltetherfi

Unless you're talking about DapperAOT, regular vanilla Dapper doesn't have a bulk upsert feature; are you using "Dapper Plus" or similar? (Dapper Plus is a completely separate project with a different team, although Dapper Plus does help by sponsoring Dapper)

If not: can you show what code you're using that I might investigate this?

mgravell avatar Oct 16 '24 16:10 mgravell

image

https://www.nuget.org/packages/Dapper/1.60.6

We are using the .net 6 web application, and I am using .netstandard dapper 1.60.6

shajiltetherfi avatar Oct 16 '24 16:10 shajiltetherfi

Again, Dapper doesn't have bulk upsert features. And you haven't yet shown any code that shows what API in Dapper you're using. Please show some code that gives me a clue what method you're using in Dapper. Also, please post code and stack-traces as text, not images.

mgravell avatar Oct 16 '24 17:10 mgravell

bulk upsert internal method, getting the issue in the Execute method from Dapper public void BulkUpsert<T>(IDbConnection connection, IDbTransaction transaction, IEnumerable<T> data, string tableName, Type type, int bulkCopyTimeout) { var allProperties = PropertiesCache.TypePropertiesCache(type); var keyProperties = PropertiesCache.KeyPropertiesCache(type); var computedProperties = PropertiesCache.ComputedPropertiesCache(type); var columns = PropertiesCache.GetColumnNamesCache(type);

var allPropertiesExceptComputed = allProperties.Except(computedProperties).ToList();
var tempToBeInserted = $"TempInsert_{tableName}_{Guid.NewGuid().ToString("N")}".Replace(".", string.Empty);
var allPropertiesExceptKeyAndComputed = allProperties.Except(keyProperties.Union(computedProperties)).ToList();
var mergingProperties = PropertiesCache.MergeConditionPropertiesCache(type);

string tempSql = $@"CREATE TEMPORARY TABLE  {tempToBeInserted} SELECT { GetColumnsStringSqlServer(allPropertiesExceptComputed, columns)} FROM {tableName} target LIMIT 0;";


var con = new MySqlConnection(connection.ConnectionString);

if (!con.ConnectionString.ToLower().Replace(" ","").Contains("allowloadlocalinfile=true"))
{
    con.ConnectionString += ";allowloadlocalinfile = true;";
}

var bulkCopy = new MySqlBulkCopy(con, transaction as MySqlTransaction);

try
{
    con.Open();
    con.Execute(tempSql, null, transaction);
   .....
   ......

It is intermittent; I am trying to replicate and get logs with the full stack trace.

shajiltetherfi avatar Oct 16 '24 20:10 shajiltetherfi

Yep, the full stacktrace would be really helpful here. The only line I can see that involves Dapper here is the one:

con.Execute(tempSql, null, transaction);

but since this isn't using parameters, there's not actually anything for it to do re ref-emit, so: I'm not sure that's a likely cause.

mgravell avatar Oct 16 '24 20:10 mgravell

I have added the stack trace , below is the result

2024-10-17 13:54:41,154 [39] INFO Core.ORM.MySqlAdapter - Connection Opened: 2024-10-17 13:54:41,155 [39] INFO Core.ORM.MySqlAdapter - Start Connection Execute with: CREATE TEMPORARY TABLE TmpIn_CMM_Role_Pages_6541bc52679842279c38e65b978d250b SELECT RoleID , PageName , USER_ACCESS , ADD_ACCESS , EDIT_ACCESS , DELETE_ACCESS , EXPORT_ACCESS , LAST_CHANGED_BY , LAST_CHANGED_ON FROM CMM_Role_Pages target LIMIT 0;: 2024-10-17 13:54:41,160 [39] ERROR Core.ORM.MySqlAdapter - Catch 2: System.InvalidProgramException: Common Language Runtime detected an invalid program. at MySqlConnector.MySqlCommand.ExecuteNonQueryAsync(IOBehavior ioBehavior, CancellationToken cancellationToken) at MySqlConnector.MySqlCommand.ExecuteNonQuery() in /_/src/MySqlConnector/MySqlCommand.cs:line 108 at Dapper.SqlMapper.ExecuteCommand(IDbConnection cnn, CommandDefinition& command, Action2 paramReader) in C:\projects\dapper\Dapper\SqlMapper.cs:line 2827 at Dapper.SqlMapper.ExecuteImpl(IDbConnection cnn, CommandDefinition& command) in C:\projects\dapper\Dapper\SqlMapper.cs:line 570 at Core.ORM.MySqlAdapter.BulkUpsert[T](IDbConnection connection, IDbTransaction transaction, IEnumerable1 data, String tableName, Type type, Int32 bulkCopyTimeout, Nullable1 commandTimeout): 2024-10-17 13:54:41,161 [39] ERROR OCM.Utilities.SqlDataAccess - ExecuteBulkUpsertQuery:System.InvalidProgramException: Common Language Runtime detected an invalid program. at MySqlConnector.MySqlCommand.ExecuteNonQueryAsync(IOBehavior ioBehavior, CancellationToken cancellationToken) at MySqlConnector.MySqlCommand.ExecuteNonQuery() in /_/src/MySqlConnector/MySqlCommand.cs:line 108 at Dapper.SqlMapper.ExecuteCommand(IDbConnection cnn, CommandDefinition& command, Action2 paramReader) in C:\projects\dapper\Dapper\SqlMapper.cs:line 2827 at Dapper.SqlMapper.ExecuteImpl(IDbConnection cnn, CommandDefinition& command) in C:\projects\dapper\Dapper\SqlMapper.cs:line 570 at Core.ORM.MySqlAdapter.BulkUpsert[T](IDbConnection connection, IDbTransaction transaction, IEnumerable1 data, String tableName, Type type, Int32 bulkCopyTimeout, Nullable1 commandTimeout) at Core.ORM.DapperExtensions.BulkUpsert[T](IDbConnection connection, IEnumerable1 data, IDbTransaction transaction, Int32 batchSize, Int32 bulkCopyTimeout, Nullable1 commandTimeout) at OCM.Utilities.SqlDataAccess.ExecuteBulkUpsertQuery[T](IEnumerable`1 obj)

shajiltetherfi avatar Oct 17 '24 07:10 shajiltetherfi

OK, so: whatever Core.ORM.MySqlAdapter.BulkUpsert is, it does seem to be calling through Dapper. Does this happen constantly, or only very occasionally. What would I need to reproduce this? Any minimal repro would be hugely helpful.

mgravell avatar Oct 17 '24 08:10 mgravell

yes, it is happening regularly, the sad part is it is happening only to specify Server (EC2) we are connecting AWS RDS. Other instances like local, dev,QA not able to reproduce this . that was very strange so reached for support .. it is happening only dapper and other DLLs are ok no such issue.. also it happens only in this method .. all other dapper methods we don't see

shajiltetherfi avatar Oct 17 '24 08:10 shajiltetherfi

Checking your screenshot: you're using a 5-and-a-half year old library version. It is possible that you're hitting a bug that has been fixed for years. Your first step here is to update the library version. If there's still a problem that needs fixing, we'd only be fixing it against "current" anyway, so: you'd need to do that to get any update. I suspect, however, that the problem will just go away.

mgravell avatar Oct 17 '24 10:10 mgravell

let me try your suggestion , it was on our road map but due to another internal dependency, we could not do that. let me push this suggestion and hopefully it is solves then well and good.

shajiltetherfi avatar Oct 17 '24 10:10 shajiltetherfi

I have upgraded the latest stable Dapper and mysqlconnector Nuget packages which did not help. I am experiencing the same issue.

shajiltetherfi avatar Oct 18 '24 05:10 shajiltetherfi

OK. Now; if I wanted to reproduce this in a minimal way - what would that look like. I can't guess all the code to make this happen.

mgravell avatar Oct 18 '24 08:10 mgravell

Option 1: Yes, I understand, I will take that method also and deploy it from my side and replicate it, then share you a repo so you can try

Option 2: Some screen share session to review our code possible for you ..

shajiltetherfi avatar Oct 18 '24 08:10 shajiltetherfi

A minimal standalone repro ala "option 1" is hugely preferable - then we can investigate in isolation, without any PII or infosec concerns, and can leave an integration test as an outcome, so it never recurs.

mgravell avatar Oct 18 '24 08:10 mgravell

This error was resolved by replacing the EC2 image that the code was running on: https://github.com/mysql-net/MySqlConnector/issues/1519#issuecomment-2440527328

Seems highly likely to be environmental.

bgrainger avatar Oct 28 '24 13:10 bgrainger