Arresto del processo SQL Agent a causa di errore interno del runtime .NET

Recentemente ho avuto modo di operare su un sistema con Windows Server 2012 R2 e SQL Server 2014 entrambi aggiornati con tutte le hotfix pubblicate su Microsoft Update su cui saltuariamente all’avvio de sistema si verificava un arresto anomalo del servizio SQL Agent (eseguito con account virtuale locale NT SERVICE\SQLSERVERAGENT) con registrazione del seguente errore:

Nome registro: Application
Origine:       .NET Runtime
ID evento:     1023
Categoria attività:Nessuna
Livello:       Errore
Parole chiave: Classico
Descrizione:
Applicazione: SQLAGENT.EXE
Versione framework: v4.0.30319
Descrizione: il processo è stato terminato a causa di un errore interno del runtime .NET all’IP 00007FFA5668EEE5 (00007FFA56650000) con codice di uscita 80131506.

Avviando poi il servizio SQL Agent manualmente questo veniva eseguito senza problemi e senza più arrestarsi in modo anomalo.

Questo errore può verificarsi per varie cause anche il versioni precedenti di SQL Server e del sistema operativo.

Una causa dell’errore può essere la mancanza di un profilo utente per l’account con cui viene eseguito SQL Agent come descritto nella KB913384 – HOTFIX: A .NET Framework 2.0 application that runs under a user account context when no user profile is associated with the user account context may crash, or you may receive an access violation error message e nella KB2640103 – FIX: “Faulting module name: clr.dll” error message when you run a Microsoft .NET Framework 4-based application. Le KB indicate offrono anche specifiche hotfix per la risoluzione del problema in alternativa è possibile eseguire SQL Agent tramite un account locale o di dominio a seconda delle necessità dedicato.

Nel mio caso il problema è invece molto probabilmente dovuto ad un malfunzionamento del Garbage Collector descritto nella KB2679415 – ExecutionEngineException occurs during Garbage Collection:

“This bug can be encountered when the Garbage Collector is freeing and compacting memory. The error can happen when the Concurrent Garbage Collection is enabled and a certain combination of foreground Garbage Collection and background Garbage Collection occurs. When this situation happens you will see the same call stack over and over. On the heap you will see one free object and before it ends you will see another free object corrupting the heap.”

E’ plausibile che questo problema venga scatenato in fase di avvio dalla funzionalità Dynamic Memory abilitata sulla macchina virtuale su cui era installato il sistema in esame.

Per risolvere il problema ho creato una semplice script PowerShell Start-SqlServerAgent.ps1 che controlla lo stato del servizio SQL Agent e se non in esecuzione lo avvia:

$service = Get-Service -Name sqlserveragent
if ($service.Status -ne “Started”){
Start-Service $service
}

Quindi ho schedulato un’operazione pianificata che richiama il file Start-SqlServerAgent.cmd che non fa altro che eseguire lo script Start-SqlServerAgent.ps1 quindici minuti dopo l’avvio del sistema e per maggior sicurezza anche ogni ora:

%SystemRoot%\system32\WindowsPowerShell\v1.0\powershell.exe C:\Scripts\Start-SqlServerAgent.ps1

image