Excel crashes and job fails when runner runs Excel related XUnit tests

Intro

We’re developing an Excel Add-In and want to set up automated builds/tests. We know that it is not recommended to automate Office products, but since our product is directly related to Excel we have no choice.

Setup used

We’re using GitLab.com and running the runner on a Windows VM.

Windows version: Win 10 Pro, Version 1903, OS build 18362.1139
Processor: Intel® Xeon® CPU E5-2673 v4 @ 2.30GHz, 2295 Mhz, 1 Core(s), 2 Logical Processor(s)
Memory: 8 GB
Runner version: 13.5.0 (ece86343)
Excel version: 2010 (Build 13328.20292 Click-to-Run)
XUnit console runner version: 2.4.1 (.NET 4.7.2)

When runner ran on my local machine:

Windows version: Win 10 Pro, Version 20H2, OS build 19042.630
Processor: Intel® Core™ i7-8665U CPU @ 1.90GHz, 2112 Mhz, 4 Core(s), 8 Logical Processor(s)
Memory: 16 GB
Runner version: 13.5.0 (ece86343)
Excel version: 2010 (Build 13328.20292 Click-to-Run)
XUnit console runner version: 2.4.1 (.NET 4.7.2)

Description

We have three stages: build, test, deploy, each with a corresponding job. The build and deploy jobs are successful, but the test job fails.

The test job consists of running XUnit from the console (shell) on a couple of DLLs built from XUnit test projects. Example:

- xunit.console.exe .\xyz.Excel.Tests\bin\Release\xyz.Excel.Tests.dll -verbose

All tests are successful, except the Excel tests. They crash with an error:

System.Runtime.InteropServices.COMException : The remote procedure call failed. (Exception from HRESULT: 0x800706BE)

The following is a minimal reproducible sample of a test that will crash, having a reference to Microsoft.Office.Interop.Excel (Version 15.0.0.0, Runtime Version v2.0.50727):

using Xunit;

namespace xyz.Excel.Tests
{
    public class ExcelOpenTest
    {
        [Fact]
        public void OpensExcel()
        {
            var app = new Microsoft.Office.Interop.Excel.Application
            { 
                Interactive = false 
            };
            var wb = app.Workbooks;
            var test = wb.Add();
            Assert.True(true);
        }
    }
}

The test will always fail on the last line interacting with Excel (in this case, line var test = wb.Add();).
NOTE: When the tests are run from Visual Studio or manually from CMD/Powershell, they are passing.

After checking the Event Viewer we noticed the following event happened at the time of the crash:

Also, after running the job a couple of times we noticed that the Faulting module name was either ntdll.dll (like in the image), clr.dll or KERNELBASE.dll with the same Exception code.

Upon inspection of the .dmp file generated from the Event we found the Exception information:

The thread tried to read from or write to a virtual address for which it does not have the appropriate access.

To fix this issue we tried

  • running the Runner as a Local System account with Allow service to interact with desktop checked,
  • changing the registry key to allow interactive services with Runner set as Local System account and Allowing service to interact with desktop,
  • disabling all Add-Ins,
  • repairing Office installation,
  • sfc /scannow,
  • turning off Windows Defender,
  • setting DCom Config properties of Excel Application (Authentication Level to None, change Launch and Activation Permissions, change Access Permissions, change identity to The interactive user),

but nothing worked.

Now, the interesting part: as a last resort I set up Gitlab Runner to process the job on my local machine and it worked, the Excel tests were passing. We double checked the relevant settings (Service settings, DCom Config, Excel version) on my local machine and the VM and they were the same, so this is truly mind-boggling. The only differences between the VM and my local machine are the Windows version, the Processor and Memory (listed in Setup used section).

We have no idea what is causing this issue, but it might be due to the Windows version or maybe even related to CPU/Memory usage on the VM(CPU usage is 100% when XUnit and Excel are running and Memory consumption is high).

Any help/idea to fix this issue is greatly appreciated!