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

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

Open
shajiltetherfi opened this issue Oct 16, 2024 · 14 comments

Comments

@shajiltetherfi
Copy link

shajiltetherfi commented Oct 16, 2024

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)

@mgravell
Copy link
Member

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?

@shajiltetherfi
Copy link
Author

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

@mgravell
Copy link
Member

mgravell commented Oct 16, 2024

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.

@shajiltetherfi
Copy link
Author

shajiltetherfi commented Oct 16, 2024

bulk upsert internal method, getting the issue in the Execute method from Dapper
public void BulkUpsert(IDbConnection connection, IDbTransaction transaction, IEnumerable 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.

@mgravell
Copy link
Member

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.

@shajiltetherfi
Copy link
Author

shajiltetherfi commented Oct 17, 2024

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)

@mgravell
Copy link
Member

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.

@shajiltetherfi
Copy link
Author

shajiltetherfi commented Oct 17, 2024

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

@mgravell
Copy link
Member

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.

@shajiltetherfi
Copy link
Author

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
Copy link
Author

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

@mgravell
Copy link
Member

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.

@shajiltetherfi
Copy link
Author

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 ..

@mgravell
Copy link
Member

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants