c# - What to do when AccessViolationException in .NET 4 app defies MSDN documented behavior? -
i have .net 4 application written windows service, includes unmanaged components, , printing accessviolationexception log4net logs, not crashing application. issue sounds impossible. below think happening, , wondering next steps developer should be? issue not happening in parallel replay environment same data feed production.
- the memory corruption issue manifests accessviolationexception, stack trace blaming ssis runtime “dts”.
- dataloaderwinsvc.exe .net 4 application, registered/running windows local service.
- microsoft changed clr in .net 4 accessviolationexception cause application crash default.
- dataloaderwinsvc.exe, however, not crash. led me investigate how possibly be?
- there 1 way let developers handle exception in .net 4, dataloaderwinsvc.exe code not using [handleprocesscorruptedstateexceptions] technique. https://msdn.microsoft.com/en-us/library/system.accessviolationexception(v=vs.110).aspx#anchor_6:
starting .net framework 4,
accessviolationexception
exceptions thrown common language runtime not handled catch statement in structured exception handler if exception occurs outside of memory reserved common language runtime. handle suchaccessviolationexception
exception, should applyhandleprocesscorruptedstateexceptionsattribute
attribute method in exception thrown.
- based on dataloaderwinsvc.txt log4net logs, clear dataloaderwinsvc.exe able handle exception despite running .net framework 4 application, otherwise wouldn’t have logging statements , crash. there no crash, @ all, however.
- by using process explorer.exe, checked see if maybe somehow version of code deployed wasn't using .net 4 framework. - start seeing suspicious! turns out ssis runtime dll, d:\program files (x86)\microsoft sql server\120\sdk\assemblies\microsoft.sqlserver.manageddts.dll, .net 2.0 assembly.
- it seems like, based on process explorer.exe, while microsoft.sqlserver.manageddts.dll calling .net 4 assembly, delegating work clr v2.0.50727 assembly native image (ngen’d assembly).
- if accessviolationexception happening in clr v2.0.50727 assembly, how able catch supposedly uncatchable exception.
here anonymized stack trace:
2016-08-16 20:58:38,207 error: orchestration: initialization system.accessviolationexception: attempted read or write protected memory. indication other memory corrupt.
@ microsoft.sqlserver.dts.runtime.package.execute() @ dataloader.orchestration.dowork() in d:\sourcecode\dataloader\mainline\core\orchestration.cs:line 198 system.accessviolationexception: attempted read or write protected memory. indication other memory corrupt.
@ microsoft.sqlserver.dts.runtime.package.execute() @ dataloader.orchestration.dowork() in d:\sourcecode\dataloader\mainline\core\orchestration.cs:line 198
and here snippet approximating code doing:
public class orchestration { private application m_dtsapplication; private package m_dtspackage; public intradayorchestration() { initializedtspackage(); } private void initiatlizedtspackage() { m_dtspackagelocation = configurationmanager.appsettings["dtspackagelocation"].tostring(); m_dtspackageconfiglocation = configurationmanager.appsettings["dtspackageconfiglocation"].tostring(); m_dtsapplication = new application(); m_dtsapplication.packagepassword = configurationmanager.appsettings["dtspackagepassword"]; m_dtspackage = m_dtsapplication.loadpackage(m_dtspackagelocation, null); m_dtspackage.importconfigurationfile(m_dtspackageconfiglocation); } private void dowork() { try { stopwatch sw = stopwatch.startnew(); while (true) { sw.start(); dtsexecresult result = m_dtspackage.execute(); sw.stop(); logger.reportinfo(datetime.now.tostring() + "time taken dts package= " + sw.elapsedmilliseconds.tostring()); // check retuns status , check errors on job execution if (result != dtsexecresult.success || m_dtspackage.errors != null) { if (m_dtspackage.errors != null) { foreach (dtserror error in m_dtspackage.errors) { string errormesg = string.format("orchestration: ssis package [{0}] error: code[{1}] source[{2}] component[{3}] description[{4}]", m_dtspackage.name, error.errorcode, error.source, error.subcomponent, error.description); logger.reporterror(errormesg, new applicationexception(errormesg)); } } else { string errormesg = string.format("orchestration: ssis package [{0}] did not complete successfully. return status [{1}]", m_dtspackage.name, result.tostring()); logger.reporterror(errormesg, new applicationexception(errormesg)); } } else { logger.reportinfo( string.format("orchestration: ssis package [{0}] completed successfully", m_dtspackage.name)); } } catch (exception ex) { logger.reporterror( string.format("orchestration: ssis package [{0}] completed successfully", m_dtspackage.name)); } } }
the root cause in fact "air space" due crossing runtime environment .net 4 .net 2.
also, turns out sql server 2008 r2 ssis runtime has double free memory bug in sql logging subcomponent.
Comments
Post a Comment